Isinstance usage in Python

Source: Internet
Author: User

Isinstance(

Object, classinfo)

Return true if
Object argument is an instance of
Classinfo argument, or of a (direct or indirect) subclass thereof. Also return true if
Classinfo is a type object and
Object is an object of that type. If
Object is not a class instance or an object of the given type, the function always returns false. If
Classinfo is 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 classinfo is not a class, type, or tuple of classes, types, and such tuples,
Typeerror exception is raised.
Changed in version 2.2: support for a tuple of type information was added.

Isinstance description::

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.

Instance:

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

>>> Lst = [1, 2, 3]
>>> Type (LST)
<Type 'LIST'>
The isinstance function is used to determine whether an object is of a known type.

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

>>> Isinstance ('ioo', (STR ))
True

>>> Isinstance (32434, (INT ))
True

>>> Isinstance ('safasdf ', (INT, long ))
False

Use CMP to compare the integer size

>>> CMP (3, 2)
1

>>> CMP (20, 30)
-1

 

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.