Python built-in functions (34) -- isinstance, pythonisinstance
English document:
isinstance(Object,Classinfo)
Return true ifObjectArgument is an instance ofClassinfoArgument, or of a (direct, indirect or virtual) subclass thereof. IfObjectIs not an object of the given type, the function always returns false. IfClassinfoIs a tuple of type objects (or recursively, other such tuples), return true ifObjectIs an instance of any of the types. IfClassinfoIs not a type or tuple of types and such tuples,TypeErrorException is raised.
Note:
1. The function is used to determine whether an object is an instance of a type object. The object parameter indicates the object to be checked, and the calssinfo parameter indicates the type object.
2. If the object parameter is an instance of the classinfo type object (or a direct, indirect, or virtual subclass of the classinfo Class object), True is returned.
>>> Isinstance (1, int) True >>> isinstance (1, str) False # define 3 categories: C inherits B, B inherits A >>> class: pass >>> class B (A): pass >>> class C (B): pass >>> a = A () >>>> B = B () >>> c = C () >>> isinstance (a, A) # direct instance True >>> isinstance (a, B) False >>> isinstance (B,) # subclass instance True >>> isinstance (c, A) # grandson class instance True
3. If the object parameter is a type object, False is always returned.
>>> isinstance(str,str)False>>> isinstance(bool,int)False
4. If a classinfo object is a combination of multiple types of objects, True is returned if the object is an instance of any type of object of the tuples. Otherwise, False is returned.
>>> isinstance(a,(B,C))False>>> isinstance(a,(A,B,C))True
5. If a classinfo object is not a type object or a tuples composed of multiple types of objects, a TypeError is returned ).
>>> isinstance(a,[A,B,C])Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> isinstance(a,[A,B,C])TypeError: isinstance() arg 2 must be a type or tuple of types