In a framework, I often want to provide a base class that the framework user sub classes. The base class provides controlled access to the base class. One way to accomplish this is to provide unimplemented methods with different names, for example by adding an underscore as prefix:
class Base:
def method(self):
self._method()
def _method(self):
raise NotImplementedError
However, this scheme only works for one level of inheritance. For more levels, the different method names make it hard to keep an overview of what's going on. Moreover, the framework user has to override different methods depending on the base class he chooses:
class Base:
def method(self):
self._method_a()
def _method_a(self):
raise NotImplementedError
class Intermediate(Base):
def _method_a(self):
self._method_b()
def _method_b(self):
raise NotImplementedError
Calling super methods does not help when the base method needs to access return values of the child method. I feel object orientation is slightly flawed, missing a child
keyword that allows to forward calls to the child class. What solutions exist to solve this problem?
Aucun commentaire:
Enregistrer un commentaire