mercredi 14 août 2019

Pythonic way to work with abstract classes

I am new to python, coming from C++ land. Have a question about pythonic way of designing something.

I want to provide an abstraction layer for some functionality. Attempting to do it with the ABC module. Then, having a base class like so:

from abc import ABC, abstractmethod

class AbstractBase(ABC):
    @abstractmethod
    def method(self):
        pass

I would ALSO want to pass some non-abstract-like arguments to child classes, e.g.:

class Child(AbstractBase):
    def method(self, arg):
        pass

This seems to work fine, but it technically speaking breaks the substitution principle. And it will break even further if I start using the typing module.

So question - in the C++ land I would solve this with another class serving as a factory to create such objects, but would true Pythonistas do the same? Or are there other ways of dealing with this?

Aucun commentaire:

Enregistrer un commentaire