OOP / language design question

Carl Banks invalidemail at aerojockey.com
Wed Apr 26 05:40:26 EDT 2006


Duncan Booth wrote:
> Alex Martelli wrote:
>
> >> What I think I'm trying to get at is that I believe that most
> >> situations where someone actually tries to do something in the base
> >> initialiser which requires calling a virtual method are probably also
> >> cases where the initialiser is doing too much: i.e. separating the
> >> construction/initialisation from actually doing something is usually
> >> a good idea.
> >
> > But why should that be?  Template Method is perhaps the MOST generally
> > useful design pattern -- why would it be any less useful in
> > initialization than elsewhere?!
> >
> Because it is error prone?
>
> Any method which is called from the constructor/initialiser has to operate
> correctly on an object which at that point is not fully
> constructed/initialised. So instead of having to write a method on a Foo
> object, your template method has to operate on a partial Foo. The danger is
> that you haven't clearly defined the partial Foo interface sufficiently and
> the method tries to use other parts of the object which haven't yet been
> set up.

In Python, if you try to use an uninitialized member you get an
AttributeError; I really don't see too much inherent danger here.  If
you make a mistake, the language tells you and you fix it.  C++ and
Java are worse since accessing uninitialized variables is a silent
mistake, so it makes sense to avoid that kind thing in those languages.

Carl Banks




More information about the Python-list mailing list