Type specified in python. Throw exceptions

Error in class type judgment(Exception):
    def __init__(self,ideal,reality): super(Exception, self).__init__(str(ideal)+'Even though the shape was good'+str(reality)+'The mold was passed')
class type is:
    def __init__(self,Mold): self.Mold=Mold
Judgment whether it is def(self,instance):
        if isinstance(instance, self.Mold): return instance
        else:error in raise type judgment(self.Mold.__name__, type(instance).__name__)        

Write, foo = Determine if type is (hoge). (Var) If you write It will check if var is of type hoge. If var is of type hoge, do nothing, If it is not of type hoge, throw an exception.

Error in class type judgment(Exception):
    def __init__(self,ideal,reality): super(Exception, self).__init__(str(ideal)+'Even though the shape was good'+str(reality)+'The mold was passed')
class type is:
    def __init__(self,Mold): self.Mold=Mold
Judgment whether it is def(self,instance):
        if isinstance(instance, self.Mold): return instance
        else:error in raise type judgment(self.Mold.__name__, type(instance).__name__)        

class dog:
    pass
class cat:
    pass

def Put on the collar and go outside(Dog):
The type is(dog).Judgment(Dog)
    print('It's a walk!')

print("1st");Put on the collar and go outside(dog())
print("2nd");Put on the collar and go outside(Cat())

Is as follows.

1st
It's a walk!
2nd
Traceback (most recent call last):
  File "D:\sd_First half of 4 years\First half of 4 years\SUDAP3_-1\cgi-bin\Typer.py", line 19, in <module>
    print("2nd");Put on the collar and go outside(Cat())
  File "D:\sd_First half of 4 years\First half of 4 years\SUDAP3_-1\cgi-bin\Typer.py", line 15,in Put on the collar and go outside
The type is(dog).Judgment(Dog)
  File "D:\sd_First half of 4 years\First half of 4 years\SUDAP3_-1\cgi-bin\Typer.py", line 7,Judgment whether in
    else:error in raise type judgment(self.Mold.__name__, type(instance).__name__)
Error in type determination:The dog type was good, but the cat type was handed over

Recommended Posts

Type specified in python. Throw exceptions
Type annotations for Python2 in stub files!
Throw Incoming Webhooks to Mattermost in Python
[Python] Read the specified line in the file
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Python numeric type
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Python2 string type
Constant in python
Python # string type
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Type Python scripts to run in QGIS Processing
Get multiple maximum keys in Python dictionary type
Static type checking that starts loosely in Python
Sort list elements in a specified order in Python
How to handle datetime type in python sqlite3
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python