
Python的type()函数
描述
type() 如果只有第一个参数则返回对象的类型,三个参数返回新的类型对象。
语法
以下是 type() 方法的语法:
type(object)
type(name, bases, dict)
参数:
name -- 类的名称。
bases -- 基类的元组。
dict -- 字典,类内定义的命名空间变量。
例子
# 一个参数实例
>>> type(1)
<type 'int'>
>>> type('runoob')
<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'>
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果