Python: isinstance

Source: Internet
Author: User
Isinstance( Object, classinfo)
Return true if ObjectArgument is an instance of ClassinfoArgument, or of a (direct or indirect) subclass thereof. Also return true if
ClassinfoIs a type object and ObjectIs an object of that type. If
ObjectIs not a class instance or an object of the given type, the function always returns false. If
ClassinfoIs neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (Other sequence types are not accepted). If
ClassinfoIs not a class, type, or tuple of classes, types, and such tuples,
TypeerrorException is raised.
Changed in version 2.2: support for a tuple of type information was added.

Python can get an object type by using the type function:

>>> Lst = [1, 2, 3]
>>> Type (LST)
<Type 'LIST'>

In addition, you can use the isinstance function to determine whether an object is of a known type.

Isinstance description:

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.

>>> Isinstance (LST, list)
True

>>> Isinstance (LST, (INT, STR, list ))
True

>>> Isinstance (LST, (INT, STR, list ))
True

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.