__init__ not a "constructor" ?
Tim Hammerquist
tim at vegeta.ath.cx
Tue Aug 14 16:26:38 EDT 2001
Me parece que Rainer Deyke <root at rainerdeyke.com> dijo:
> "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.
Good point. There are times I appreciated C++'s automatic calling of
base constructors (eg, when the first line of every subclass's
__init__() is SuperClass.__init__(self) =), but it's one line of code in
exchange for the ability to decide for oneself what the interpreter
should do...and that's why I switched from Win32 in the first place: it
wouldn't do what I wanted it to. =)
Cheers!
--
Tell me -- How's your love life? Killed any girlfriends
recently? Or sentenced any more of them to Hell?
-- Desire to Morpheus, The Sandman
More information about the Python-list
mailing list