Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'output': 0.05; 'class,': 0.07; 'method.': 0.07; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'subject:skip:t 10': 0.09; 'def': 0.12; '(),': 0.16; '*args):': 0.16; 'and)': 0.16; 'bases,': 0.16; 'down...': 0.16; 'foo()': 0.16; 'invokes': 0.16; 'marco': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; '{})': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'skip:f 30': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'skip:` 20': 0.24; 'question': 0.24; 'define': 0.26; 'this:': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'statement': 0.30; "skip:' 10": 0.31; 'invoke': 0.31; 'class': 0.32; 'regular': 0.32; 'moment': 0.34; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'something': 0.35; 'but': 0.35; 'method': 0.36; 'thanks': 0.36; 'should': 0.36; 'so,': 0.37; 'easily': 0.37; 'expected': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'first': 0.61; 'making': 0.63; 'yourself': 0.78 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Overriding of the type.__call__() method in a metaclass Date: Sun, 06 Oct 2013 21:04:20 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849e31.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 95 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381086267 news.xs4all.nl 15979 [2001:888:2000:d::a6]:33228 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56276 Marco Buttu wrote: > 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 Forget about metaclasses for the moment and ask yourself what happens when a regular class class A: def __init__(...): ... def __call__(...): ... is "called": a = A(...) # invokes __init__() a(...) # invokes __call__() The metaclass is just the class of a class, i. e. the Foo object is an instance of FooMeta, so making Foo invokes (__new__() and) __init__(), and calling Foo invokes FooMeta.__call__(): >>> class FooMeta(type): ... def __call__(self, *args): print("__call__%r" % (args,)) ... >>> class Foo(metaclass=FooMeta): pass ... >>> Foo() __call__() If you follow that logic you can easily see that for FooMeta to invoke your custom __call__() method you'd have to define it in FooMeta's metaclass: >>> class FooMetaMeta(type): ... def __call__(*args): print(args) ... >>> class FooMeta(metaclass=FooMetaMeta): ... pass ... >>> class Foo(metaclass=FooMeta): ... pass ... (, 'Foo', (), {'__module__': '__main__', '__qualname__': 'Foo'}) >>> Foo is None True So, yes, it's turtles all the way down...