元编程
元编程概念来自LISP 和smalltalk
我们写程序是直接写代码, 是否能够用代码来生成未来我们需要的代码吗? 这就是元编程.
python中
所有非object类都继承自object类
所有类的类型包括type类都是type
type类继承自object类, object类的类型也是type类
type类
type构建类
type(object) -> the object’s type 返回对象的类型, 例如type(10)
type(name, bases, dict) -> a new type 返回一个新类型
def __init__(self):
self.x = 1000
def show(self):
print(self.__dict__)
x = type("Mytype", (object,),{
"a":100, 'b':'string', '__init__':__init__, 'show':show}) # 字典是类属性
print(x)
print