is it possible to use abc.abstractproperty
to create a concrete getter but make the setter abstract so its different for each of the inheriting classes. I handle the setting of val different for each subclass.
eg.
@abstractproperty
def val(self):
return self._val
@val.setter
def val(self, x):
pass
You can do everything in an abstract class that you can do in a normal class except creating a new object only by using a constructor. This means that you can simply copy and paste the getters and setters from your subclass into your parent class.
Getters and Setters in python are often used when: We use getters & setters to add validation logic around getting and setting a value. To avoid direct access of a class field i.e. private variables cannot be accessed directly or modified by external user.
@property is used to get the value of a private attribute without using any getter methods. We have to put a line @property in front of the method where we return the private variable. To set the value of the private variable, we use @method_name.
You'll need a little bit of indirection. Define the setter as you normally would, but have it call an abstract method that does the actual work. Then each child class will need to provide a definition of that method. For example,
class Base(object):
__metaclass__ = abc.ABCMeta
def __init__(self):
self._val = 3
@property
def val(self):
return self._val
@val.setter
def val(self, x):
self._val_setter(x)
@abc.abstractmethod
def _val_setter(self, x):
pass
class Child(Base):
def _val_setter(self, x):
self._val = 2*x
Then
>>> c = Child()
>>> print c.val
3
>>> c.val = 9
>>> print c.val
18
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With