I'm a newbie in writing OO program, and I cannot find any good solution of the problem I'm facing. May anyone please help?
I'm sourcing some modules which I cannot freely modify it, and I would like to add a method on a superclass so that I can call on instances of subclasses. So in my module:
import externalLib as myLib
class Superclass(myLib.Superclass):
def myNewMethod(self):
print(self.name) # Print a member variable
def __main__():
obj = myLib.Subclass(args)
obj.myNewMethod() # Expect prints the same member variable in subclass
Result: "Subclass" has no attribute or method named "myNewMethod".
Extending all the subclass is not possible to me, as there are too many subclasses.
I could solve the problem by defining the function under my module instead of the Superclass, but I just think that way is not like an OO-architecture.
Is there any better solution? Or any keywords or OO design concept can I refer to?
Thanks!