python里怎样检察数据范例?
python里能够经由过程type()函数来检察数据范例。
Python 内置函数 Python 内置函数
Python type() 函数假如你只要第一个参数则返回对象的范例,三个参数返回新的范例对象。
isinstance() 与 type() 区分: type() 不会以为子类是一种父类范例,不斟酌继续关联。 isinstance() 会以为子类是一种父类范例,斟酌继续关联。
假如要推断两个范例是不是雷同引荐运用 isinstance()。
以下是 type() 要领的语法:
type(object) type(name, bases, dict)
参数
name:类的称号。
bases:基类的元组。
dict:字典,类内定义的定名空间变量。
返回值
一个参数返回对象范例, 三个参数,返回新的范例对象。
实例
以下展现了运用 type 函数的实例:
# 一个参数实例 >>> 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()区分:
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
引荐:《python教程》
以上就是python里怎样检察数据范例的细致内容,更多请关注ki4网别的相干文章!