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 < Span class= "Hl-builtin" > >>> isinstance (a,str false < Span class= "Hl-identifier" >>>> Isinstance (a , (str, Int,list) # is a return truetrue
Python isinstance () function