Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56275

Overriding of the type.__call__() method in a metaclass

From Marco Buttu <marco.buttu@gmail.com>
Newsgroups comp.lang.python
Subject Overriding of the type.__call__() method in a metaclass
Date 2013-10-06 20:17 +0200
Organization Aioe.org NNTP Server
Message-ID <l2s9fr$eus$1@speranza.aioe.org> (permalink)

Show all headers | View raw


Hi all, I have a question about class creation and the __call__ method. 
I have the following metaclass:

 >>> class FooMeta(type):
...     def __call__(metacls, name, bases, namespace):
...         print("FooMeta.__call__()")


 From what I undestood, at the end of the class statement happens 
something like this:

 >>> def __call__(metacls, name, bases, namespace):
...     print("FooMeta.__call__()")
...
 >>> FooMeta = type('FooMeta', (type,), {'__call__': __call__})

The call to the metaclass type causes the call to type.__call__(), so 
that's happened is:

 >>> FooMeta = type.__call__(type, 'FooMeta', (type,), {'__call__': 
__call__})

Now I expected the output `FooMeta.__call__()` from the following Foo 
class creation:

 >>> class Foo(metaclass=FooMeta):
...     pass

because I thought at the end of the class Foo suite this should have 
been happened:

 >>> Foo = FooMeta.__call__(FooMeta, 'Foo', (), {})
FooMeta.__call__()

but I thought wrong:

 >>> class FooMeta(type):
...     def __call__(metacls, name, bases, namespace):
...         print("FooMeta.__call__()")
...
 >>> class Foo(metaclass=FooMeta):
...     pass
...
 >>>

How come? Is it because the first argument of metaclass.__call__() is 
always type or I am thinking something wrong?
Thanks in advance, Marco
-- 
Marco

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Overriding of the type.__call__() method in a metaclass Marco Buttu <marco.buttu@gmail.com> - 2013-10-06 20:17 +0200
  Re: Overriding of the type.__call__() method in a metaclass Peter Otten <__peter__@web.de> - 2013-10-06 21:04 +0200
  Re: Overriding of the type.__call__() method in a metaclass Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-07 02:27 +0000
    Re: Overriding of the type.__call__() method in a metaclass Marco Buttu <marco.buttu@gmail.com> - 2013-10-07 07:22 +0200

csiph-web