In the code below, I have 3 subclasses, Model1, Model2, Model3. For the ‘exec’ method, if Model1 and Model2 have only ‘text’ as parameter, but Model3 has one additional parameter ‘type’. In this case, can I still define ‘exec’ as the abstract method, or I shouldn’t define it as an abstract method?
class Model(ABC):
def __init__(self, name):
self.name = name
pass
@abstractmethod
def load(self):
pass
@abstractmethod
def exec(self, text):
pass
>Solution :
It depends on the logic.
If the type parameter is related to all Model objects, so you can create an abstractmethod of it for all of them:
class Model(ABC):
def __init__(self, name):
self.name = name
pass
@abstractmethod
def load(self):
pass
@abstractmethod
def exec(self, text, type=None):
pass
But, IMO if type param is not related to all Model objects, you cannot write an abstractmethod for it.