OOP / language design question

Duncan Booth duncan.booth at invalid.invalid
Tue Apr 25 08:43:25 EDT 2006


bruno at modulix wrote:

> Duncan Booth wrote:
> (snip)
>> Usually though, if a subclass doesn't immediately call the base class
>> constructors as the first thing it does in __init__ it indicates poor
>> code and should be refactored.
> 
> Not necessarily. It's a common case to have some computations to
> do/some attributes to set in the derived class's __init__ before
> calling the superclass's.
> 

I did only say 'usually'. Can you actually think of any good examples where 
you have to set a derived attribute before you can call the base class 
constructor? I can't, which is why I was a bit vague.

The base class is unlikely to depend on the derived class attributes, and 
unless it does there should be no reason which you can't just call the base 
__init__ straight away. Perhaps if the base __init__ calls an overridden 
method, but at that point it sounds to me like something wants refactoring.

I can think that you might have to do some computations to calculate 
parameters for the base __init__, but that is a separate issue.



More information about the Python-list mailing list