I'd like to do something like this (this is a trivial example, but it shows the problem):
a = {'a' : 3, 'b': (lambda x: x * 3)(a['a'])}
but I want the value of a['b']
to get automatically updated when a['a']
changes.
In this case of course a['b']
gets evaluated only at the beginning, so I get this:
>>> a = {'a': 3, 'b': (lambda x: x * 3)(a['a'])}
>>> a
{'a': 3, 'b': 9}
>>> a['a'] = 4
>>> a
{'a': 4, 'b': 9}
What I want is this instead:
>>> a = {'a': 3, 'b':(lambda x: x * 3)(a['a'])}
>>> a
{'a': 3, 'b': 9}
>>> a['a'] = 4
>>> a
{'a': 4, 'b': 16} <<<<<<<<<< change
Does anyone know if there's an easy/pythonic way to achieve this?