小白教程
所有教程
关于
Search
172.71.254.8
172.71.254.8
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python func type”的源代码
本页内容
上一节:
Python_func_property
下一节:
Python3_func_chr
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:type()}}[[Category:python3 built in functions|44]] = Python type() 函数 = [[python built in functions|Python 内置函数]] == 描述 == type() 函数如果你只有第一个参数则返回对象的类型,三个参数返回新的类型对象。 <blockquote>isinstance() 与 type() 区别: * type() 不会认为子类是一种父类类型,不考虑继承关系。 * isinstance() 会认为子类是一种父类类型,考虑继承关系。 如果要判断两个类型是否相同推荐使用 isinstance()。 </blockquote> === 语法 === 以下是 type() 方法的语法: <sample title="" desc="" lang="python" hererun="1"> type(object) type(name, bases, dict) </sample> === 参数 === * name -- 类的名称。 * bases -- 基类的元组。 * dict -- 字典,类内定义的命名空间变量。 === 返回值 === 一个参数返回对象类型, 三个参数,返回新的类型对象。 以下展示了使用 type 函数的实例: <sample title="" desc="" lang="python" hererun="1"> # 一个参数实例 >>> type(1) <type 'int'> >>> type('xiaobai') <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'> </sample> == type() 与 isinstance()区别: == <sample title="" desc="" lang="python" hererun="1"> 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 </sample> [[python built in functions|Python 内置函数]]
返回至“
Python func type
”。
上一节:
Python_func_property
下一节:
Python3_func_chr