Python isinstance () function python built-in function python built-in function

Source: Internet
Author: User
Tags builtin
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 that consists of them.
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< Span class= "Hl-brackets" >) ) # is a return in a tuple True true Type () differs from isinstance ():Class A:Pass Class B(A):Pass Isinstance(A(),A) # returns True Type(A() = = A< Span class= "Hl-code" > # returns true Isinstance (b ( a) Span class= "Hl-code" > # returns true type< Span class= "Hl-brackets" > (b () = = a # returns False

Python isinstance () function python built-in function python built-in function

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.