initializing mutable class attributes

Alex Martelli aleaxit at yahoo.com
Thu Sep 2 07:07:59 EDT 2004


David Bolen <db3l at fitlinxx.com> wrote:

> "Dan Perl" <dperl at rogers.com> writes:
> 
> (...)
> > Very good point.  I would usually assume that you should initialize the
> > superclass's attributes first, and then the subclass's.  But that may not
> > always be the intent and I may just be influenced by my background in C++
> > and Java.  (...)
> 
> I do find it reasonably common (particularly in GUI applications when
> I'm subclassing standard GUI objects) to have a subclass that massages
> the constructor inputs slightly (perhaps adding different defaults, or
> filtering the arguments) before passing them on to the superclass.
> 
> In C++ you can sometimes manage this as long as the manipulation is
> simple exclusion or addition that can be performed in the
> initialization code prior to the start of the subclass constructor,
> but that's very limited (and otherwise can end up forcing you into the
> 2-phase construction model).  So I find Python's freedom of
> controlling exactly when the superclass is initialized with complete
> control over its arguments very liberating.

Very good explanation of the key reason why in C++ you end up using the
pattern "two phase constructor" far more often than in Python (although
it's not _foreign_ to Python, mind you)...


Alex
 



More information about the Python-list mailing list