python里怎么查看数据类型-Python教程

资源魔 34 0

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里怎样查看数据类型的具体内容,更多请存眷资源魔其它相干文章!

标签: Python python教程 python编程 python使用问题

抱歉,评论功能暂时关闭!