Python built-in functions (34) -- isinstance, pythonisinstance

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.