In a class, I want to define N persistent properties. I can implement them as follow:
@property
def prop1(self):
return self.__prop1
@prop1.setter
def prop1(self, value):
self.__prop1 = value
persistenceManagement()
@property
def prop2(self):
return self.__prop2
@prop2.setter
def prop2(self, value):
self.__prop2 = value
persistenceManagement()
[...]
@property
def propN(self):
return self.__propN
@propN.setter
def propN(self, value):
self.__propN = value
persistenceManagement()
Of course, the only different thing between these blocks is the property name (prop1, prop2, ..., propN). persistenceManagement() is a function that has to be called when the value of one of these property changes.
Since these blocks of code are identical except for a single information (i.e., the property name), I suppose there must be some way to replace each of these blocks by single lines declaring the existence of a persistent property with a given name. Something like
def someMagicalPatternFunction(...):
[...]
someMagicalPatternFunction("prop1")
someMagicalPatternFunction("prop2")
[...]
someMagicalPatternFunction("propN")
...or maybe some decorating trick that I cannot see at the moment. Is someone has an idea how this could be done?