这里的代码胜于文字:
class MetaA(type):
def __new__(cls, name, bases, attrs):
print "MetaA"
return super(MetaA, cls).__new__(cls, name, bases, attrs)
class A(object):
__metaclass__ = MetaA
这将打印MetaA
class MetaB(MetaA):
def __new__(cls, name, bases, attrs):
print "MetaB"
return super(MetaB, cls).__new__(cls, name, bases, attrs)
B = type('B', (A, ), {'__metaclass__': MetaB})
这将再次打印MetaA
(?!)
我希望:
MetaB
MataA
问题是:
- 为什么我
MetaA
只得到? 如何更改代码以获得:
MetaB MataA