[Tutor] subclass problem: __names and type-checking

Alan Gauld alan.gauld at freenet.co.uk
Sat Oct 8 23:46:50 CEST 2005

> If B inherits from A then every context where A or an A instance appears
> should work correctly with B or a B instance.  Since the B constructor
> *requires* more arguments, it violates that ideal.  In other words, it
> would be OK to allow additional arguments.  It is not OK to require
> them.

Which is all true but it should be pointed out that *allowing* them
includes such devices as constructor overloading (in Java/C++ etc)
or using default values for the additional parameters. The latter is
obviously a possible option in Python.

> In other words sub-class should really be a different class that adapts
> or possibly acts as a proxy for the _BaseClass.

Not necessarily, it may just provide alternate construction semantics.

> I've abused inheritance in the past in an attempt to reuse code and have
> usually regretted it

But this is always trure. Inheritance for code reuse is usually a bad idea
that comes back to bite you later. Inheritance implies an 'is-a' 
If the sub class is not really  the same kinde of thing as the superclass
then inheritance is probably the wrong solution.

Alan G
Author of the learn to program web tutor

More information about the Tutor mailing list