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

Marco name.surname at gmail.com
Tue Nov 20 12:29:05 EST 2012


Looking at the documentation of Py3.3:

http://docs.python.org/3.3/reference/datamodel.html#object.__new__

I saw the method `__new__()` is called automatically when I create an 
istance. After the call to __new__(), if it returns an instance `self` 
then `self.__init__()` is called.

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:

 >>> class Foo:
...     def __new__(cls):
...         print('Foo.__new__()')
...         return super().__new__(cls)
...     def __init__(self):
...         print('Foo.__init__(self)')
...
 >>> f = type.__call__(Foo)
Foo.__new__()
Foo.__init__(self)

Is that right? Thanks in advance


-- 
Marco



More information about the Python-list mailing list