Can I use the same @property setter for multiple properties?

2.2k views Asked by At

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?

1

There are 1 answers

0
sebastian On

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.