I got this idea after working with Qt. It uses an object property, not a variable:
from types import FunctionType class MyObject: def __init__(self,name): self.name= name self._a_f=None self._a=None @property def a(self): if self._a_f is not None: self._a= self._a_f() return self._a @a.setter def a(self,v): if type(v) is FunctionType: self._a_f=v else: self._a_f=None self._a=v o1,o2,o3=map(MyObject,'abc') o1.a = lambda: o2.a + o3.a o2.a = lambda: o3.a * 2 o3.a = 10 print( o1.a )
But what if you want to know when o1.a changed? This is my first wish, but the implementation is complicated. Even if this probably answers another question, here is an example:
class MyObject(metaclass=HaveBindableProperties): a= BindableProperty() someOther= BindableProperty() o1,o2,o3=map(MyObject,'abc') o1.a = lambda: o2.a + o3.a o2.a = lambda: o3.a * 2 @o1.subscribe_a def printa(): print( o1.a ) o3.a = 1
source share