Dive into Python -
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 constructor Foo.__init__()
is the initializer Foo.__new__()
is the allocator Construction of a Python object is simply allocation of a new instance followed by initialization of said instance.
Personally, I find "__init__
is not a constructor" to be pretty fine hair-splitting.
__init__
is called when a new object is requested. It is supposed to use its arguments to assign attributes on the new object, such that the required invariants for normal operation of the object are set up. The object is already a valid pre-existing place to store attributes by the time the code in __init__
begins running. The new object normally has no attributes defined on it already when the code in __init__
begins running (other than the ones that all objects possess).
A C++ constructor is called when a new object is requested. It is supposed to use its arguments to assign to fields on the new object, such that the required invariants for normal operation of the object are set up. The object is already a valid pre-existing place to store fields by the time the code in the constructor begins running. The new object has all its declared fields already when the code in the constructor begins running, but they contain garbage.
A Java constructor is called when a new object is requested. It is supposed to use its arguments to assign to fields on the new object, such that the required invariants for normal operation of the object are set up. The object is already a valid pre-existing place to store fields by the time the code in the constructor begins running. The new object has all its declared fields already when the code in the constructor begins running, with their default values.
The major difference between an __init__
method and a C++/Java constructor is in that last sentence I've highlighted, and that's just the difference between the static nature of Java/C++ and the dynamic nature of Python. I don't think this warrants calling them fundamentally different concepts that must not be referred to by the same word.
I think the main reason Pythonistas don't like to refer to __init__
as a constructor is that people think of C++/Java constructors as "making a new object", because that's what they seem to do when you call them. But there's really two things going on when you call a constructor; a new object is created and then the constructor is called to initialise it. In C++/Java the "create a new object" part of that is invisible, whereas that can be exposed/customised in Python (via the __new__
method).
So while the role of the __init__
method is extremely similar to the role of a C++/Java constructor, some people prefer to emphasise the fact that this isn't the whole process by saying that "__init__
is not a constructor".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With