Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I still use abstract method if one of the method parameters is different?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading