How to subclass a family

A

Antoon Pardon

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?
 
S

Steven D'Aprano

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top