First, Isinstance ()
In Python, you can use the type () and isinstance () functions to determine the object type, while the isinstance () function is more convenient to use than type.
Copy the Code code as follows:
# Coding=utf-8
A = 10
def b ():
Pass
Print Isinstance (A, (INT,STR))
Print Isinstance (A, (FLOAT,STR))
Print isinstance (b, (Str,int))
Class C:
Pass
obj = C ()
Print isinstance (obj, (c,int))
Execution Result:
Copy the Code code as follows:
True
False
False
True
Ii. the difference between isinstance and type
The difference between isinstance and type is:
Copy the Code code as follows:
Class A:
Pass
Class B (A):
Pass
Isinstance (A (), a) # returns True
Type (A ()) = = a # returns True
Isinstance (B (), A) # returns True
Type (B ()) = = A # returns False
The difference is that the type of subclass is not available, so it is strongly recommended that you do not use type to determine object types.