__init__ explanation please

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Jan 15 05:06:47 EST 2008


Hrvoje Niksic a écrit :
> Ben Finney <bignose+hates-spam at benfinney.id.au> writes:
> 
>> Hrvoje Niksic <hniksic at xemacs.org> writes:
>>
>>> Wildemar Wildenburger <lasses_weil at klapptsowieso.net> writes:
>>>> __init__() /initializes/ an instance (automatically after
>>>> creation). It is called, /after/ the instance has been constructed
>>> I don't understand the purpose of this "correction".  After all,
>>> __init__ *is* the closest equivalent to what other languages would
>>> call a constructor.
>> No. That would be '__new__', which actually constructs the instance,
> 
> That's not what other OO languages (C++, Java)  actually call a
> constructor, 

There are actually quite a few other OOPLs than C++ and Java, and at 
least one of them (namely Smalltalk, which predates both C++ and Java) 
uses distinct phases for allocation and initialisation.

IOW, it's not because C++ and/or Java use a given terminology that this 
terminology should be blindly applied to each and every other OOPL. 
FWIW, there are quite a lot of other differences between C++/Java and 
Python when it comes to object model, and what OO is is definitively 
*not* defined by C++ and/or Java.

So while it's true that __init__ is the closest equivalent to what C++ 
and Java (and possibly a couple "other languages") call a constructor, 
it doesn't imply that you should refer to it as "the constructor". As 
Neil Cerutti points out, there's in fact nothing like a 'constructor 
method' in Python : there are a "__new__" method, an "__init__" method, 
and "constructor expressions" which may invoke them !-)



More information about the Python-list mailing list