__init__ explanation please
Lie.1296 at gmail.com
Thu Jan 17 18:55:44 CET 2008
On Jan 16, 5:34 am, "Reedick, Andrew" <jr9... at ATT.COM> wrote:
> > >From the base definition of a constructor: constructor is the creator
> > of an object. In this case, __new__ is technically the constructor
> > while __init__ is an initializer.
> > However, it is also to be noted that __init__ is what makes an object
> > meaningful, and that makes it a constructor in a sense (while still
> > technically a constructor). Without initialization, an object is
> > meaningless, even if the definition of the initializer is to leave it
> > as it is.
> You don't need to have an __init__ defined. A subclass has to
> explicitly call the parent's __init__ or the parent's __init__ is never
In other languages, constructor might be optional. In the case of non-
existent constructor, compilers would add an empty constructor, but
what's the difference (from programmer's POV) between Python ignoring
__init__ and other languages adding empty constructor? That's just an
> If the __init__ makes the object meaningful, then how meaningful
> is an object without an __init__?
It actually depends on the object, some objects might be pretty
meaningless without being initialized (or its members altered from
outside very carefully). Examples include a simple vector class. If
the vector is not initialized, the x and y equals None, which is not a
valid value for vector, which means the object is meaningless.
> I'm pretty sure that an object
> without an __init__ is still a viable, working object.
I'm sure it is, although it's initial value might not be a valid
> > If you can't be convinced with this argument, then I'd give you
> > another that's a bit more Pythonic:
> > DUCK TYPING: If it looks like a duck, walks like a duck, and quacks
> > like a duck, it is a duck!
> But you don't need __init__ to be a duck!
> > >From the class programmer's point of view, __init__ acts like an
> > object constructor in other languages, there is no significant
> > difference between __init__ and constructor in other languages.
> How many times can you call an object's constructor in other languages?
> __init__ can be called repeatedly.
That's a very good argument to separate __init__ from a real
constructor, but how many projects you do would require object
recycling (which is the only reason I can think of for calling
initializers more than once)? Object recycling should only be done on
systems which lacks resources because it have big potential to
introduce bugs caused by incomplete cleaning.
> __init__ is the last straw that breaks the camel's back. Or rather, the
> last method we see in the object creation process, and thus must be
> 'guilty' of being a constructor. Only a fundamentalist would blame the
> victim instead of the real criminal, __new__.
It's not about blaming, rather they shared parts in the act.
> We're splitting hairs. And I'm pretty sure that, aside from being a
> spiffy thought experiment, no one cares as long as it works and makes
> sense. =)
I agree with that.
More information about the Python-list