In [1]:
print(isinstance('string', str))
In [2]:
print(isinstance(100, str))
In [3]:
print(isinstance(100, (int, str)))
In [4]:
def is_str(v):
return isinstance(v, str)
In [5]:
print(is_str('string'))
In [6]:
print(is_str(100))
In [7]:
print(is_str([0, 1, 2]))
In [8]:
def is_str_or_int(v):
return isinstance(v, (int, str))
In [9]:
print(is_str_or_int('string'))
In [10]:
print(is_str_or_int(100))
In [11]:
print(is_str_or_int([0, 1, 2]))
In [12]:
def type_condition(v):
if isinstance(v, str):
print('type is str')
elif isinstance(v, int):
print('type is int')
else:
print('type is not str or int')
In [13]:
type_condition('string')
In [14]:
type_condition(100)
In [15]:
type_condition([0, 1, 2])
In [16]:
class Base:
pass
class Derive(Base):
pass
In [17]:
base = Base()
print(type(base))
In [18]:
derive = Derive()
print(type(derive))
In [19]:
print(type(derive) is Derive)
In [20]:
print(type(derive) is Base)
In [21]:
print(isinstance(derive, Derive))
In [22]:
print(isinstance(derive, Base))
In [23]:
print(type(True))
In [24]:
print(type(True) is bool)
In [25]:
print(type(True) is int)
In [26]:
print(isinstance(True, bool))
In [27]:
print(isinstance(True, int))