python里怎么查看数据类型

您所在的位置:网站首页 python数据类型查看 python里怎么查看数据类型

python里怎么查看数据类型

2022-06-04 02:57| 来源: 网络整理| 查看: 265

python里怎么查看数据类型 发布时间:2020年11月03日 07:34:06 来源:环球青藤 点击量:88

【摘要】python里怎么查看数据类型?python里可以通过type()函数来查看数据类型。Python 内置函数 Python 内置函数Python type() 函数如果你只

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('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

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教程请关注环球青藤其它相关文章!

分享到: 编辑:wangmin

上一篇:怎么查看python版本? 下一篇:python怎么删除


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3