Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the use of the isinstance () function in Python

Shulou Source: shulou.com Published: 2022-06-02 10:22:48 09月11日 Update

This article shows you the use of the isinstance () function in Python, which is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Description

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

The difference between isinstance () and type ():

Type () does not consider a subclass to be a parent type and does not consider inheritance relationships.

Isinstance () will consider the subclass as a parent class type and consider inheritance relationships.

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

Grammar

The following is the syntax of the isinstance () method:

Isinstance (object, classinfo) parameters

Object-instance object.

Classinfo-can be direct or indirect class names, base types, or tuples made up of them.

Return value

Returns True if the type of the object is the same as the type of parameter 2 (classinfo), otherwise returns False.

Example

The following shows an example of using the isinstance function:

> a = 2 > isinstance (apenint) True > isinstance (amemstr) False > isinstance (a, (str,int,list)) # is a tuple that returns TrueTruetype () and 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 what is the use of the isinstance () function in Python Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Type function instance object same content parameter subclass skill knowledge grammar concise concise two namely article method more article class name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi vpn MySQL NVidia Microsoft