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:
class MyProperty(object):
def __init__(self, name):
self.name = name
def __get__(self, instance, owner):
if instance is None:
return self
else:
# get attribute from the instance
return getattr(instance, '_%s' % self.name) # return x._prop
def __set__(self, instance, value):
# set attribute and the corresponding key in the "remote" dict
instance.other_dict[self.name] = value # x.other_dict["prop"] = value
setattr(instance, '_%s' % self.name, value) # x._prop = value
And use them as follows:
class MyClass(object):
prop = MyProperty("prop")
another_prop = MyProperty("another_prop")
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 from other_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.
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