python之type函数

python 的type 函数 的介绍的   下面就是此函数的参数   三个参数的意义

'''
type(class_name, base_class_tuple, attribute_dict)
class_name type创建类的名称,就是通常定义类的类名
base_class_tuple type创建类所继承类的元组,通常定义时继承的父类
attribute_dict type创建类的属性,不单纯指值属性,也可以是方法
'''

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def test_method(self): #这里要接受至少一个参数,作为类方法会默认传入self
    print 'test_method'

class A(object):
    def __init__(self, a):
        print 'call a __init__'
        self.a = a

B = type('B', (A,), {'b':1, 'test_method':test_method})
b1 = B(5)  #因为继承A类,初始化要提供一个参数给a,不能直接B()建实例
b2 = B(6)
print b1.b, ' | ' , b2.b  #运行结果 1 | 1
b2.b = 10
print b1.b, ' | ' , b2.b  #运行结果 1 | 10
b1.test_method()          #和通常类方法调用没有区别

运行结果:

call a __init__
call a __init__
1  |  1
1  |  10
test_method
原文地址:https://www.cnblogs.com/wanghuixi/p/10786782.html