__init__ not a "constructor" ?

Rainer Deyke root at rainerdeyke.com
Tue Aug 14 09:01:17 CEST 2001


"Tim Hammerquist" <tim at vegeta.ath.cx> wrote in message
news:slrn9ngrqn.2ij.tim at vegeta.ath.cx...
> Me parece que Kirby Urner <urner at alumni.princeton.edu> dijo:
> >
> > I notice the author of the Dive Into Python tutorial cautions
> > that __init__ is not trully a class constructor, as by the time
> > it's invoked, an instance already exists (hence the argument
> > 'self', hence the fact that classes don't need an __init__
> > method).
>
> I'm not sure what the author means, but:
>
> class A:
>     def __init__(self, *args):
>         # do stuff
>
> ... is roughly analogous to C++'s constructors, if you don't count C++'s
> implicit passing of the object.

One important difference is that Python gives more control over the
construction process.  For example, in C++ the constructors of all base
classes are always called before the constructor body is entered.  A Python
'__init__' method can choose if, when, and in what order the base class
'__init__' methods should be called.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list