What's the best, most Pythonic, way to deal with abstract properties in Python? If I want to implement a base class which has a mixture of abstract properties, and concrete methods, I can do so similar to the following.
class BaseClass(object):
__metaclass__ = ABCMeta
@abstractmethod
def property1(self):
pass
@abstractmethod
def property2(self):
pass
@abstractmethod
def property3(self):
pass
@abstractmethod
def abstract_method(self):
pass
def concrete_method(self):
return self.property1 + self.property2
However, when I then go to implement the inheriting class I need to implement each of those properties as getter method for a private property.
class Klass(BaseClass):
def __init__(self, property1, property2, property3):
self.__property1 = property1
self.__property2 = property2
self.__property3 = property3
@property
def property1(self):
return self.__property1
@property
def property2(self):
return self.__property2
@property
def property3(self):
return self.__property3
Which seems both unnecessarily verbose, and makes the code more obscure than it needs to be.
I don't love the idea of implementing things concretely and raising a NotImplementedErrorif the inheriting class doesn't implement it's own version.
Is there a better way to do this?
@propertydecorator fromproperty1, etc. inBaseClass? Theconcrete_methodimplementation suggests you really want to use these as attributes rather than method calls. But back in 2016, you probably could not stack/nest decorators...@abstractpropertywas invented for that