My class has a number of properties that all need to use the same type of setter:
@property
def prop(self):
return self._prop
@prop.setter
def prop(self, value):
self.other_dict['prop'] = value
self._prop = value
Is there an easy way to apply this setter structure to a number of properties that doesn't involve writing these two methods for each property?
You could implement this using a descriptor, i.e. as follows:
And use them as follows:
As a side note: it might be worth thinking about whether you really need to duplicate the properties values. You could easily get rid of the
_prop
attribute completely by returning the corresponding value fromother_dict
. This also avoids potential issues arising from different values stored in the dict and on your class instance - which may easily occur with your current scheme.