简体   繁体   中英

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

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 , ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM