isinstance (object, ClassInfo)
Determine if the instance is this class or object
object is a variable
ClassInfo is a type (tuple,dict,int,float)
Determine if the variable is of this type
Copy the Code code as follows:
Class Obja:
Pass
A = Obja ()
B = ' A ', ' V '
C = ' A string '
Print isinstance (A, Obja)
Print isinstance (B, tuple)
Print isinstance (C, basestring)
Output Result:
Copy CodeThe code is as follows:
True
True
True
Furthermore , you can use the Isinstance function to determine whether an object is a known type.
Isinstance is described below:
Copy CodeThe code is as follows:
Isinstance (object, class-or-type-or-tuple), BOOL
Return whether an object are an instance of a class or of a subclass thereof.
With a type as second argument, return whether this is the object ' s type.
The form using a tuple, isinstance (x, (A, B, ...)), is a shortcut for
Isinstance (x, A) or isinstance (x, B) or ... (etc.).
Its first argument is an object, and the second is a list of type names or type names. Its return value is a Boolean type. Returns true if the type of the object is the same as the type of parameter two. If parameter two is a tuple, returns true if the object type is the same as one of the type names in the tuple.
Copy CodeThe code is as follows:
>>>isinstance (LST, list)
True
>>>isinstance (LST, (int, str, list))
True
In addition: Python can get the type of an object, taking advantage of the type function: >>>lst = [1, 2, 3]>>>type (LST)