Reputation: 582
What is the best way to create a parent class that is able to define a set of methods that will be implemented by the base classes?
The problem comes when a class A
requires 2 parameters for method_X
, while class B
requires 3 parameters for the same method.
How can I solve this issue?
class SuperClass:
def method_X(self):
...
class A(SuperClass):
def method_X(self, a, b):
...
class B(SuperClass):
def method_X(self, a, b, c):
...
Upvotes: 4
Views: 654
Reputation: 16733
What you're trying to accomplish is a clear violation of Liskov Substitution Principle. Moreover, defining inconsistent interfaces for the same named methods is going to be messy and would severely limit duck typing. Still, if you want to go with it, I think using kwargs
instead of named parameters would serve the purpose.
class SuperClass:
def method_x(self, **kwargs):
...
class A(SuperClass):
def method_x(self, **kwargs):
if 'a' in kwargs:
# Do something
Upvotes: 3