Introduction to Python isinstance functions _python

Source: Internet
Author: User

Isinstance (object, ClassInfo)

To determine whether an instance is this class or object

object is a variable
ClassInfo is a type (tuple,dict,int,float)
To determine whether a variable is this type

Copy Code code 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 results:
Copy Code code as follows:

True
True
True


not only that, you can also use the Isinstance function to determine whether an object is a known type.
The isinstance description is as follows:
Copy Code code as follows:

Isinstance (object, class-or-type-or-tuple)-> bool

Return whether a instance a class or of a subclass thereof.
With a type as second argument, return whether this is the object ' s type.
The form using a tuple, isinstance (x, (A, B, ...)), is a shortcut for
Isinstance (x, A) or isinstance (x, B) or ... (etc.).

The first argument is an object, and the second is a list of type names or type names. Its return value is Boolean. Returns true if the type of the object is the same as the type of parameter two. If the argument two is a tuple, returns true if the object type is the same as one of the type names in the tuple.
Copy Code code as follows:

>>>isinstance (LST, list)
True

>>>isinstance (LST, (int, str, list))
True

In addition: Python can get the type of an object, using the type function: >>>lst = [1, 2, 3]>>>type (LST) <type ' list ' >

Related Article

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.