What does isinstance mean in python?

青灯夜游
Release: 2020-08-29 15:13:19
Original
48359 people have browsed it

In python, isinstance means "determining type"; isinstance() is a built-in function used to determine whether an object is a known type, similar to type().

What does isinstance mean in python?

isinstance() function to determine whether an object is a known type, similar to type().

The difference between isinstance() and type():

  • type() does not consider the subclass to be a parent class type and does not consider the inheritance relationship.

  • isinstance() will consider the subclass to be a parent class type and consider the inheritance relationship.

If you want to determine whether two types are the same, it is recommended to use isinstance().

Syntax

isinstance(object, classinfo)
Copy after login

Parameters

  • object -- Instance object.

  • classinfo -- Can be a direct or indirect class name, a primitive type, or a tuple consisting of them.

Return value

If the type of the object is the same as the type of parameter two (classinfo), it returns True, otherwise it returns False.

Example:

>>>a = 2
>>> isinstance (a,int)
True
>>> isinstance (a,str)
False
>>> isinstance (a,(str,int,list))    # 是元组中的一个返回 True
True
Copy after login

Recommended learning:Python video tutorial

The above is the detailed content of What does isinstance mean in python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!