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 with their constituent groups.
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) # is a return truetrue in tuples
Type () differs from isinstance ():
Class A: Pass Class B (a): Pass Isinstance (A (), a) # returns Truetype (A ()) = = A # returns Trueisinstance (b (), a) # returns Truetype (B ()) = = A # returns False
Python isinstance () function