我正在玩Python 2.7中的元类。所以,我创建了一个看起来像这样的代码:具有自定义元类行为的Python元类
class M(type):
def __new__(meta, name, parents, attrs):
print 'In meta new'
return super(meta, meta).__new__(meta, name, parents, attrs)
def __init__(cls, *args, **kwargs):
print 'In meta init'
def __call__(cls, *attr, **val):
print 'In meta call'
return super(cls, cls).__new__(cls)
class A(object):
__metaclass__ = M
def __new__(cls):
print 'In class new'
return super(cls, cls).__new__(cls)
def __init__(self):
print 'In object init'
def __call__(self):
print 'In object call'
但产量混淆了我:
A()
In meta new
In meta init
In meta call
不知何故类方法__ __新和__ __的init被覆盖,所以解释只是跳过他们。任何人都可以解释这个东西
感谢您的帮助。
这是以其他方式工作,并非因此。 – alexvassel