Describe
The Isinstance () function determines whether an object is a known type, similar to type ().
Isinstance () differs from type ():
Type () does not consider a subclass to be a parent class type, regardless of the inheritance relationship.
Isinstance () considers the subclass to be a type of parent class, considering the inheritance relationship.
It is recommended to use Isinstance () if you want to determine whether two types are the same.
Grammar
The following is the syntax for the Isinstance () method:
Isinstance(object, classinfo)
Parameters
- Object--instance objects.
- ClassInfo--can be either a direct or indirect class name, a base type, or a tuple that consists of them.
return value
Returns True if the type of the object is the same as the type of parameter two (ClassInfo), otherwise False is returned:
Instance
The following shows an example of using the Isinstance function:
>>>A=2>>>Isinstance (A,Int) True >>> isinstance (a,str false >> > isinstance ( a, (str ,int,list< Span class= "Hl-brackets" >) ) # is a return in a tuple True true Type () differs from isinstance ():Class A:Pass Class B(A):Pass Isinstance(A(),A) # returns True Type(A() = = A< Span class= "Hl-code" > # returns true Isinstance (b ( a) Span class= "Hl-code" > # returns true type< Span class= "Hl-brackets" > (b () = = a # returns False
Python isinstance () function python built-in function python built-in function