In your situation, c is actually a function that needs to be called. You can use something like this:
a = 10
b = 30
c = lambda: a + b
print c()
a += 20
print c()
If you don't like the method call being made explicit to c, you can use a generic Calc object that hides this implementation:
class Calc(object):
def __init__(self):
object.__setattr__(self, '_params', dict())
def __getattr__(self, name):
param = self._params[name]
if callable(param):
return param()
else:
return param
def __setattr__(self, name, value):
self._params[name] = value
def __delattr__(self, name):
del self._params[name]
And then you could do:
c = Calc()
c.a = 10
c.b = 30
c.c = lambda: c.a + c.b
print c.c
c.a += 20
print c.c
Johan source
share