I am looking for advice on how to construct some project I gonna work on.
I got a base class, with some methods.
class Base(object):
def method1(self):
# some code
def method2(self):
# some code
def method3(self):
# some code
... and some other methods
For my project, I cannot modify the Base class, and I must inherent from it in order to apply my project requirements.
I have several requirement that might be enabled or not. Depending on a given configuration. I.e. I'd like to be able to override either method1(), method2(), method3() or any combination of them.
One alternative is to construct several inheriting classes.
class Child1(Base):
def method1(self):
# some code
class Child2(Base):
def method2(self):
# some code
class Child3(Base):
def method3(self):
# some code
And then maybe use multiple inheritance to apply any composition of them. However this approach wouldn't scale well for covering all possible combinations.. (e.g. what happens if I will have Child4()? )
Another alternative is just having a single inheritance class and use if clauses to select whether to call the super method or apply the derived behavior.
class Child(Base):
def method1(self):
if not self.override1:
# call super
else:
# some code
def method2(self):
if not self.override2:
# call super
else:
# some code
def method3(self):
if not self.override3:
# call super
else:
# some code
I am more in favor on this alternative, however I feel there got to be a better OO approach for doing this.
Any thoughts or suggestions?
Thanks a lot
PS: I am constrained to python 2.7, and since I plan to share the code, I rather have a solution that is easy to understand by an average python programmer (and not only by advanced programmers).