I want to set up a system where a method of one object is called whenever an attribute of a different object is modified, with the new value as an argument to the method. How would I accomplish something like that?
Edit: Code sample/clarification:
My code looks something like this:
class SharedAttribute(object): #I use this as a container class to share attributes between multiple objects
def __init__(self, value):
self.value = value
class BaseObject(object):
def __init__(self, **shared_attributes:
for (key, value) in shared_attributes:
if isinstance(value, SharedAttribute):
setattr(self, key, value)
else:
setattr(self, key, SharedAttribute(value))
class MyObject(BaseObject):
def __init__(self, value1, value2):
BaseObject.__init__(self, value1, value2)
def on_value_changed(self, new_value):
#Do something here
def incrementValue(self):
self.value1.value += 1
And then it would be implemented like:
a = MyObject(value1 = 1)
b = MyObject(value1 = MyObject.value1) #a and b are storing references to the same attribute, so when one changes it, it's changed for the other one too.
I want to set it up so if I were to call a.incrementValue()
, b.on_value_changed
would be called, with the SharedAttribute
's new value as an argument. Is there a good way to do that?