Python isinstance () function

Source: Internet
Author: User

Describe

The Isinstance () function determines whether an object is a known type, similar to type ().

Isinstance () differs from type ():

  • Type () does not consider a subclass to be a parent class type, regardless of the inheritance relationship.

  • Isinstance () considers the subclass to be a type of parent class, considering the inheritance relationship.

It is recommended to use Isinstance () if you want to determine whether two types are the same.

Grammar

The following is the syntax for the Isinstance () method:

Isinstance (object, ClassInfo)
Parameters
    • Object--instance objects.
    • ClassInfo--can be either a direct or indirect class name, a base type, or a tuple with their constituent groups.
return value

Returns True if the type of the object is the same as the type of parameter two (ClassInfo), otherwise False is returned:

Instance

The following shows an example of using the Isinstance function:

>>>a = 2>>> isinstance (a,int) true>>> isinstance (a,str) false>>> isinstance (A, ( Str,int,list)    # is a return truetrue in tuples
Type () differs from isinstance ():
Class A:    Pass Class B (a):    Pass Isinstance (A (), a)    # returns Truetype (A ()) = = A        # returns Trueisinstance (b (), a)    # returns Truetype (B ()) = = A        # returns False

Python isinstance () function

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.