Home > Backend Development > Python Tutorial > How to check the type of an object

How to check the type of an object

anonymity
Release: 2019-05-25 15:10:48
Original
4261 people have browsed it

There are two type judgment functions in Python, type() and isinstance().

How to check the type of an object

Use type()

First, we determine the object type and use the type() function:

Basic types can be judged by type():

>>> type(123)
<type &#39;int&#39;>
>>> type(&#39;str&#39;)
<type &#39;str&#39;>
>>> type(None)
<type &#39;NoneType&#39;>
Copy after login

If a variable points to a function or class, it can also be judged by type():

>>> type(abs)
<type &#39;builtin_function_or_method&#39;>
>>> type(a)
<class &#39;__main__.Animal&#39;>
Copy after login

But what does the type() function return? What about type? It returns type type.

Using isinstance()

For class inheritance relationships, it is very inconvenient to use type(). To determine the type of class, we can use the isinstance() function.

Let’s review the last example. If the inheritance relationship is:

object -> Animal -> Dog -> Husky
Copy after login

Then, isinstance() can tell us whether an object is of a certain type. First create three types of objects:

>>> a = Animal()
>>> d = Dog()
>>> h = Husky()
Copy after login

Then, judge:

>>> isinstance(h, Husky)
True
Copy after login

There is no problem, because the h variable points to the Husky object.

Rejudgment:

>>> isinstance(h, Dog)
True
Copy after login

Although h itself is of Husky type, since Husky is inherited from Dog, h is also of Dog type. In other words, isinstance() determines whether an object is of the type itself, or is on the parent inheritance chain of the type.

Therefore, we can be sure that h is still an Animal type:

>>> isinstance(h, Animal)
True
Copy after login

Similarly, d whose actual type is Dog is also an Animal type:

>>> isinstance(d, Dog) and isinstance(d, Animal)
True
Copy after login

However, d is not a Husky type :

>>> isinstance(d, Husky)
False
Copy after login

The basic type that can be judged by type() can also be judged by isinstance():

>>> isinstance(&#39;a&#39;, str)
True
>>> isinstance(u&#39;a&#39;, unicode)
True
>>> isinstance(&#39;a&#39;, unicode)
False
Copy after login

And it can also be judged whether a variable is one of certain types, such as the following You can use the code to determine whether it is str or unicode:

>>> isinstance(&#39;a&#39;, (str, unicode))
True
>>> isinstance(u&#39;a&#39;, (str, unicode))
True
Copy after login

Since both str and unicode are inherited from basestring, you can also simplify the above code to:

>>> isinstance(u&#39;a&#39;, basestring)
True
Copy after login

Since we have type() to determine the type, why is there isinstance()? One obvious difference is in judging subclasses. type() does not consider a subclass to be a parent type. isinstance() will consider the subclass to be a parent class type.

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