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 namestring 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.
#定义类型A
>>> class A:
name = 'defined in A'
#创建类型A实例a
>>> a = A()
#a.__class__属性
>>> a.__class__
<class '__main__.A'>
#type(a)返回a的类型
>>> type(a)
<class '__main__.A'>
#测试类型
>>> type(a) == A
True
#定义类型A,含有属性InfoA
>>> class A(object):
InfoA = 'some thing defined in A'
#定义类型B,含有属性InfoB
>>> class B(object):
InfoB = 'some thing defined in B'
#定义类型C,含有属性InfoC
>>> class C(A,B):
InfoC = 'some thing defined in C'
#使用type函数创建类型D,含有属性InfoD
>>> D = type('D',(A,B),dict(InfoD='some thing defined in D'))
#C、D的类型
>>> C
<class '__main__.C'>
>>> D
<class '__main__.D'>
#分别创建类型C、类型D的实例
>>> c = C()
>>> d = D()
#分别输出实例c、实例b的属性
>>> (c.InfoA,c.InfoB,c.InfoC)
('some thing defined in A', 'some thing defined in B', 'some thing defined in C')
>>> (d.InfoA,d.InfoB,d.InfoD)
('some thing defined in A', 'some thing defined in B', 'some thing defined in D')
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 namestring 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.
#定义类型A
>>> class A:
name = 'defined in A'
#创建类型A实例a
>>> a = A()
#a.__class__属性
>>> a.__class__
<class '__main__.A'>
#type(a)返回a的类型
>>> type(a)
<class '__main__.A'>
#测试类型
>>> type(a) == A
True
#定义类型A,含有属性InfoA
>>> class A(object):
InfoA = 'some thing defined in A'
#定义类型B,含有属性InfoB
>>> class B(object):
InfoB = 'some thing defined in B'
#定义类型C,含有属性InfoC
>>> class C(A,B):
InfoC = 'some thing defined in C'
#使用type函数创建类型D,含有属性InfoD
>>> D = type('D',(A,B),dict(InfoD='some thing defined in D'))
#C、D的类型
>>> C
<class '__main__.C'>
>>> D
<class '__main__.D'>
#分别创建类型C、类型D的实例
>>> c = C()
>>> d = D()
#分别输出实例c、实例b的属性
>>> (c.InfoA,c.InfoB,c.InfoC)
('some thing defined in A', 'some thing defined in B', 'some thing defined in C')
>>> (d.InfoA,d.InfoB,d.InfoD)
('some thing defined in A', 'some thing defined in B', 'some thing defined in D')