python里怎么查看数据类型?
python里可以通过type()函数来查看数据类型。
Python 内置函数 Python 内置函数
Python type() 函数如果你只有第一个参数则返回对象的类型,三个参数返回新的类型对象。
1
2
3
isinstance() 与 type() 区别: type() 不会认为子类是一种父类类型,不考虑继承关系。 isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
以下是 type() 方法的语法:
1
2
type(object) type(name, bases, dict)
参数
name:类的名称。
bases:基类的元组。
dict:字典,类内定义的命名空间变量。
返回值
一个参数返回对象类型, 三个参数,返回新的类型对象。
实例
以下展示了使用 type 函数的实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 一个参数实例>>> type(1)<type 'int'>>>> type('school')<type 'str'>>>> type([2])<type 'list'>>>> type({0:'zero'})<type 'dict'>>>> x = 1>>> type( x ) == int # 判断类型是否相等True# 三个参数>>> class X(object):... a = 1...>>> X = type('X', (object,), dict(a=1)) # 产生一个新的类型 X>>> X<class '__main__.X'>
type() 与 isinstance()区别:
1
2
3
4
5
6
7
8
class A:passclass B(A):passisinstance(A(), A) # returns Truetype(A()) == A # returns Trueisinstance(B(), A) # returns Truetype(B()) == A # returns False
以上就是python里怎么查看数据类型的详细内容。
如果大家如果在学习中遇到困难,想找一个Python学习交流环境,可以加入我们的Python学习圈,点击我加入吧,会节约很多时间,减少很多遇到的难题。
本文介绍Python中如何使用type()和isinstance()函数检查数据类型,包括它们的区别和使用实例。type()函数返回对象的类型,而isinstance()考虑继承关系。
833





