Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report