python - How can I call the metaclass's __call__? -
python - How can I call the metaclass's __call__? -
given next illustration on instance of x
class:
class x(): def __call__(self, incoming_list): print incoming_list x()([x x in range(10)])
how can obtain same output using __call__
magic method class instead of instance? example:
x([x x in range(10)])
calling directly, if passing __init__
. but, before calls __call__
calls __new__
passes arguments __init__
. how can access "metaclass __call__
" ? possible?
just create easier understand, gives me same output there:
class x: def __call__(self, incoming_list): print incoming_list x().__call__([x x in range(10)])
i want this:
class x: def x.__call__(incoming_list): # syntax error print incoming_list x.__call__([x x in range(10)])
i think think complicated.
probably want like
class x: def __init__(self, incoming_list): self.data = incoming_list # maintain them later, if needed print incoming_list x([x x in range(10)])
everything without meta class, on definition of class.
if need meta class, can like
class mc(type): def __call__(self, *a, **k): super(mc, self).__call print a, k r = super(mc, self).__call__(*a, **k) print "r", r homecoming r class x(object): __metaclass__ = mc def __init__(self, x): print "init", x
using with
>>> x(1) (1,) {} init 1 r <__main__.x object @ 0x00000000022907b8> <__main__.x object @ 0x00000000022907b8>
shows meta-__call__
called, which, in turn, calls __init__
.
but sure don't need , want __init__
.
python class call metaclass
Comments
Post a Comment