__init__ explanation please

Reedick, Andrew jr9445 at ATT.COM
Tue Jan 15 23:34:16 CET 2008

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of Lie
> Sent: Tuesday, January 15, 2008 5:03 PM
> To: python-list at python.org
> Subject: Re: __init__ explanation please
> I've been in this Python mailing list for a few days, and I've noticed
> several things here: There are too many fundamentalist!
> Don't play stupid and all, don't be a fundamentalist. It might be true
> that __init__ isn't a constructor and __new__ might be the constructor
> (some people even claimed __new__ is also not a constructor).

Purist is a better term.  Fundamentalist is three syllables closer to
Holy War.

> >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
run.  If the __init__ makes the object meaningful, then how meaningful
is an object without an __init__?  I'm pretty sure that an object
without an __init__ is still a viable, working object.

> 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.

__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__.

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.   =)

Repeated for clarity:  smiley -->  =)  <-- smiley


The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625

More information about the Python-list mailing list