How to subclass a family

Discussion in 'Python' started by Antoon Pardon, Apr 8, 2013.

  1. 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
    Antoon Pardon, Apr 8, 2013
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Apr 9, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    429
    jstorta
    Feb 20, 2006
  2. S.Volkov
    Replies:
    2
    Views:
    206
    S.Volkov
    Mar 12, 2006
  3. Trans
    Replies:
    8
    Views:
    309
    Robert Klemme
    Oct 23, 2008
  4. Arnaud Delobelle

    Re: How to subclass a family

    Arnaud Delobelle, Apr 8, 2013, in forum: Python
    Replies:
    0
    Views:
    292
    Arnaud Delobelle
    Apr 8, 2013
  5. Devin Jeanpierre

    Re: How to subclass a family

    Devin Jeanpierre, Apr 9, 2013, in forum: Python
    Replies:
    0
    Views:
    290
    Devin Jeanpierre
    Apr 9, 2013
Loading...

Share This Page