I have code that someone else wrote like this:
class MyClass(object):
def __init__(self, data):
self.data = data
@property
def attribute1(self):
return self.data.another_name1
@property
def attribute2(self):
return self.data.another_name2
and I want to automatically create the corresponding property setters at run time so I don't have to modify the other person's code. The property setters should look like this:
@attribute1.setter
def attribue1(self, val):
self.data.another_name1= val
@attribute2.setter
def attribue2(self, val):
self.data.another_name2= val
How do I dynamically add these setter methods to the class?
You can write a custom Descriptor like this:
Demo: