【发布时间】:2012-10-16 11:06:26
【问题描述】:
我正在使用 Python 2.7 中的元类。所以我创建了一个如下所示的代码:
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
不知何故,类方法 __ new __ 和 __ init __ 被覆盖了,所以解释器只是跳过它们。谁能解释一下这些东西?
感谢您的帮助。
【问题讨论】: