Home  >  Article  >  Backend Development  >  How to check the type of a Python variable

How to check the type of a Python variable

silencement
silencementOriginal
2019-05-23 15:18:2117135browse

Methods to view Python variable types: 1. Use type(), the syntax "type(object)", to return the type of variable object; 2. Use isinstance(), the syntax "isinstance(object,classinfo)" , check whether the variable object is a known type.

How to check the type of a Python variable

How Python determines the type of a variable

There are two ways to determine the type of a variable in Python: type() and isinstance()

How to use

For basic data types, both have the same effect

type()

Return the type of object

How to check the type of a Python variable

isinstance()

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

How to check the type of a Python variable

The difference

The difference between isinstance() and type() is:
type() The subclass will not be considered to be a parent class type
isinstance() will consider the subclass to be a parent class type

How to check the type of a Python variable

The above is the detailed content of How to check the type of a Python variable. 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