Home>Article>Backend Development> How to check data type in python
How to check the data type in python?
In python, you can check the data type through the type() function.
Python built-in functions Python built-in functions
Python type() function returns the type of the object if you only have the first parameter, and the three parameters return the new type object.
isinstance() 与 type() 区别: type() 不会认为子类是一种父类类型,不考虑继承关系。 isinstance() 会认为子类是一种父类类型,考虑继承关系。
If you want to determine whether two types are the same, it is recommended to use isinstance().
The following is the syntax of the type() method:
type(object) type(name, bases, dict)
Parameters
name: The name of the class.
bases: Tuple of base classes.
dict: dictionary, namespace variable defined within the class.
Return value
One parameter returns the object type, and three parameters return the new type object.
Example
The following shows an example of using the type function:
# 一个参数实例 >>> type(1)>>> type('school') >>> type([2]) >>> type({0:'zero'}) >>> x = 1 >>> type( x ) == int # 判断类型是否相等 True # 三个参数 >>> class X(object): ... a = 1 ... >>> X = type('X', (object,), dict(a=1)) # 产生一个新的类型 X >>> X
The difference between type() and isinstance():
class A: pass class B(A): pass isinstance(A(), A) # returns True type(A()) == A # returns True isinstance(B(), A) # returns True type(B()) == A # returns False
Recommendation: "pythontutorial》
The above is the detailed content of How to check data type in python. For more information, please follow other related articles on the PHP Chinese website!