Python isinstance function introduction, pythonisinstance
Isinstance (object, classinfo)
Determine whether the instance is of this class or object
Object is a variable.
Classinfo is a type (tuple, dict, int, float)
Determines whether the variable is of this type.
Copy codeThe Code is 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
In addition, you can use the isinstance function to determine whether an object is of a known type.
Isinstance description:
Copy codeThe Code is as follows:
Isinstance (object, class-or-type-or-tuple)-> bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance (x, (A, B,...), is a temporary cut
Isinstance (x, A) or isinstance (x, B) or... (etc .).
The first parameter is the object, and the second is a list of type names or type names. The return value is boolean. Returns True if the object type is the same as that of parameter 2. If the two parameters are a single tuples, True is returned if the object type is the same as one of the type names in the tuples.
Copy codeThe Code is as follows:
>>> Isinstance (lst, list)
True
>>> Isinstance (lst, (int, str, list ))
True
In addition, Python can obtain an object type, using the type function >>> lst = [1, 2, 3] >>> type (lst) <type 'LIST'>