Python 内建函数 - type(object)
2017-03-27 00:13
393 查看
参数形式
Manual
直译
实例
拓展阅读
type(name, bases, dict)
The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.
With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the __name__ attribute; the bases tuple itemizes the base classes and becomes the __bases__ attribute; and the dict dictionary is the namespace containing definitions for class body and is copied to a standard dictionary to become the __dict__ attribute. For example, the following two statements create identical type objects:
See also Type Objects.
对于测试对象类型,推荐isinstance()内建函数,因为它可以考虑到子类。
三个参数时返回一个新的类型对象,这本质上是类声明的动态形式。name字符串是类名,变成__name__属性;bases元组列出基类,变成__bases__属性;dict字典是包含类主体定义的命名空间,并拷贝到标准字典,变成__dict__属性。例如:以下两个声明创建了相同类型的对象:
详情见类型对象
类型对象
Manual
直译
实例
拓展阅读
参数形式
type(object)type(name, bases, dict)
Manual
With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__.The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.
With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the __name__ attribute; the bases tuple itemizes the base classes and becomes the __bases__ attribute; and the dict dictionary is the namespace containing definitions for class body and is copied to a standard dictionary to become the __dict__ attribute. For example, the following two statements create identical type objects:
>>> >>> class X: ... a = 1 ... >>> X = type('X', (object,), dict(a=1))
See also Type Objects.
直译
单参数时返回object的类型,返回值是一个类型对象,通常与object.__class__的返回对象相同。对于测试对象类型,推荐isinstance()内建函数,因为它可以考虑到子类。
三个参数时返回一个新的类型对象,这本质上是类声明的动态形式。name字符串是类名,变成__name__属性;bases元组列出基类,变成__bases__属性;dict字典是包含类主体定义的命名空间,并拷贝到标准字典,变成__dict__属性。例如:以下两个声明创建了相同类型的对象:
>>> >>> class X: ... a = 1 ... >>> X = type('X', (object,), dict(a=1))
详情见类型对象
实例
>>> type(object) <class 'type'> >>> type(type) <class 'type'> >>> type(dict) <class 'type'> >>> type(1) <class 'int'> >>> type(int) <class 'type'> >>> type('1') <class 'str'> >>> type(b'1') <class 'bytes'> >>> type(None) <class 'NoneType'> >>> type(True) <class 'bool'>
拓展阅读
isinstance()类型对象
相关文章推荐
- python socket send 函数 报错:TypeError: a bytes-like object is required, not 'str'
- Python 内建函数 - super([type[, object-or-type]])
- Python包导入(TypeError: 'module' object is not callable),及一定要记住Python成员函数第一个参数值是self。
- TypeError: 'module' object is not callable 原因分析(python模块导入注意事项)
- Python Type Class and Object的文章
- python __def__ Exception AttributeError: "'NoneType' object has no attribute
- python面向对象-理清object与type的关系
- python错误TypeError: 'module' object is not callable 的解决方法
- python import 包问题:TypeError: 'module' object is not callable
- [Python][转]Exception AttributeError: "'NoneType' object has no attribute
- 【Python】python "TypeError: 'NoneType' object is not iterable"错误分析
- 【Python】TypeError: 'module' object is not callable 原因分析
- python错误TypeError: 'module' object is not callable 的解决方法
- python Flask :TypeError: 'dict' object is not callable
- python中3个帮助函数help,dir,type的使用
- Python源码学习-PyTypeObject
- python面向对象-理清object与type的关系
- 【Python】TypeError: 'module' object is not callable 原因分析
- 【Python】TypeError: 'module' object is not callable 原因分析
- python(3)------The object type of python