metaclasses元类:就像对象是类的实例一样,类是它的元类的实例。调用元类可以创建类。

metaclass使用type来创建类,type可以被继承生成新的元类。

这个和C#的反射很相似。

下面是一个通过元类创建类的事例:

def __init__(self):
self.message='hello world'
def say(self):
print(self.message)

attrs={'__init__':__init__,'say':say}
bases=(object,)
haha=type('haha',bases,attrs)
print(haha)

a=haha()
a.say()
print(type(a))

 

引用:http://www.cnblogs.com/coderzh/archive/2008/12/07/1349735.html

相关文章:

  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2022-01-07
  • 2021-12-15
  • 2021-06-12
猜你喜欢
  • 2021-12-01
  • 2021-07-24
  • 2021-06-21
  • 2021-11-21
  • 2021-06-15
  • 2022-01-10
相关资源
相似解决方案