Home >Backend Development >Python Tutorial >What does isinstance mean in python?

What does isinstance mean in python?

青灯夜游
青灯夜游Original
2020-08-29 15:13:1948489browse

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)

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

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!

Statement:
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