1. type()
两种用法
a. 当传入参数为一个时,返回值为参数的类型
b. 当传入参数为三个时,type(name, bases, dict)
name: 类名
bases: 继承父类的元组,可继承也可不继承
dict: 构成类属性和方法的键值对(方法在外部定义,直接引用).
返回一个类对象
class TestBasic(object): @staticmethod def parent(): print "this is parent method" def child(self): print "this is child method" test_property = {"name": "heihei", "child": child} Test = type("Test", (TestBasic,), test_property) test = Test() print test.name test.child() test.parent()
运行结果:
heihei this is child method this is parent method