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


Groups > comp.lang.python > #33647

Re: The type.__call__() method manages the calls to __new__ and __init__?

References <k8gekt$n48$1@speranza.aioe.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-11-20 12:25 -0700
Subject Re: The type.__call__() method manages the calls to __new__ and __init__?
Newsgroups comp.lang.python
Message-ID <mailman.80.1353439541.29569.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Nov 20, 2012 at 10:29 AM, Marco <name.surname@gmail.com> wrote:
> Because when I call an instance the __call__ method is called, and because
> the classes are instances of type, I thought when I call a Foo class this
> imply the call type.__call__(Foo), and so this one manages the Foo.__new__
> and Foo.__init__ calls:

Yes, that's right.  Observe:

>>> class MetaFoo(type):
...     def __call__(cls):
...         print("before")
...         self = super().__call__()
...         print("after", self)
...         return self
...
>>> class Foo(metaclass=MetaFoo):
...     def __new__(cls):
...         print("__new__")
...         return super().__new__(cls)
...     def __init__(self):
...         print("__init__")
...
>>> f = Foo()
before
__new__
__init__
after <__main__.Foo object at 0x00C55410>

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


Thread

The type.__call__() method manages the calls to __new__ and __init__? Marco <name.surname@gmail.com> - 2012-11-20 18:29 +0100
  Re: The type.__call__() method manages the calls to __new__ and __init__? Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-20 12:25 -0700

csiph-web