Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43053 > unrolled thread

How to subclass a family

Started byAntoon Pardon <antoon.pardon@rece.vub.ac.be>
First post2013-04-08 11:44 +0200
Last post2013-04-09 02:52 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  How to subclass a family Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-04-08 11:44 +0200
    Re: How to subclass a family Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-09 02:52 +0000

#43053 — How to subclass a family

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2013-04-08 11:44 +0200
SubjectHow to subclass a family
Message-ID<mailman.278.1365414362.3114.python-list@python.org>
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, ...)

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?

-- 
Antoon Pardon

[toc] | [next] | [standalone]


#43107

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-04-09 02:52 +0000
Message-ID<51638271$0$30003$c3e8da3$5496439d@news.astraweb.com>
In reply to#43053
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web