It would be tempting but incorrect to call this the constructor of the class. It's tempting, because it looks like a constructor (by convention,
__init__
is the first method defined for the class), acts like one (it's the first piece of code executed in a newly created instance of the class), and even sounds like one (“init” certainly suggests a constructor-ish nature). Incorrect, because the object has already been constructed by the time__init__
is called, and you already have a valid reference to the new instance of the class.
Quote suggests it is incorrect to call __init__
as a constructor because the object is already constructed by the time __init__
is called. But! I have always been under the impression that the constructor is called only after the object is constructed because it is essentially used to initialized the data members of the instance which wouldn't make sense if the object didn't exist by the time constructor was called? (coming from C++/Java background)
If you have a class Foo
then:
Foo()
is the constructorFoo.__init__()
is the initializerFoo.__new__()
is the allocatorConstruction of a Python object is simply allocation of a new instance followed by initialization of said instance.