How to subclass a family

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Apr 8 22:52:33 EDT 2013


On Mon, 08 Apr 2013 11:44:51 +0200, Antoon Pardon wrote:

> Here is the idea. I have a number of classes with the same interface.
> Something like the following:
> 
> class Foo1:
>     def bar(self, ...):
>         work
>     def boo(self, ...):
>         do something
>         self.bar(...)
> 
> What I want is the equivallent of:
> 
> class Far1(Foo1):
>     def boo(self, ...)
>         do something different
>         if whatever:
>             self.bar(...)
>         else:
>             Foo1.boo(self, ...)


What do you mean, "the equivalent of"? What's wrong with the code as 
given?



> Now of course I could subclass every class from the original family from
> Foo1 to Foon but that would mean a lot of duplicated code. Is there a
> way to reduce the use of duplicated code in such circumstances?


I don't understand your question. The reason for using inheritance is to 
reduce the amount of duplicated code. If you're ending up with more code, 
you're doing something wrong. You're probably badly designing your 
methods, or your classes, or both. If you give a less contrived example, 
perhaps we can help.



-- 
Steven



More information about the Python-list mailing list