С следующим кодом, я ожидаю, что выход будет:Python метакласса использовать с объектом конкретизацией
# meta.__new__
# meta.__new__
# meta.__init__
# Before instantiation of Entity
# meta.__call__
# __new__
# __init__
# __call__
#
Вместо этого я получил следующий вывод:
# meta.__new__
# meta.__new__
# meta.__init__
# Before instantiation of Entity
# meta.__call__
# __new__
# __init__
# Traceback (most recent call last):
# File "test.py", line 39, in <module>
# e(3,4)
# TypeError: 'NoneType' object is not callable
class MyMetaClass(type):
def __new__(cls, name, bases, dct):
print 'meta.__new__'
return super(MyMetaClass, cls).__new__(cls, name, bases, dct)
def __init__(cls, name, bases, dct):
print 'meta.__init__'
super(MyMetaClass, cls).__init__(name, bases, dct)
def __call__(self, *more):
print 'meta.__call__'
super(MyMetaClass, self).__call__(*more)
class Entity(object):
__metaclass__ = MyMetaClass
def __new__(cls, *args, **kwargs):
print '__new__'
return super(Entity, cls).__new__(cls, *args, **kwargs)
def __init__(self, x, y):
print '__init__'
self.x, self.y = x, y
def __call__(self, x, y):
print '__call__'
self.x, self.y = x, y
if __name__ == '__main__':
print 'Before instantiation of Entity'
e = Entity(1,2)
e(3,4)
Что вы на самом деле хотите, чтобы метакласс был * сделать *? Зачем внедрять '__new__' и' __init__' и '__call__'? – jonrsharpe