Python isinstance function introduction, pythonisinstance

Source: Internet
Author: User

Python isinstance function introduction, pythonisinstance

Isinstance (object, classinfo)

Determine whether the instance is of this class or object

Object is a variable.
Classinfo is a type (tuple, dict, int, float)
Determines whether the variable is of this type.
Copy codeThe Code is 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 result:
Copy codeThe Code is as follows:
True
True
True


In addition, you can use the isinstance function to determine whether an object is of a known type.
Isinstance description:
Copy codeThe Code is as follows:
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.
Copy codeThe Code is as follows:
>>> Isinstance (lst, list)
True

>>> Isinstance (lst, (int, str, list ))
True

In addition, Python can obtain an object type, using the type function >>> lst = [1, 2, 3] >>> type (lst) <type 'LIST'>

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.