title: Python: linking/binding variables together in a dictionary
body: <p>first post, so play nice!</p>
  
  <p>I have a fairly basic question about Python dictionaries. I would like to have some dict value that updates when another variable is changed (or at least recalculated when it is next called) - eg:</p>
  
  <pre><code>mass = 1.0
  volume = 0.5
  mydict = {'mass':mass,'volume':volume}
  mydict['density'] = mydict['mass']/mydict['volume']
  </code></pre>
  
  <p>So in this case, mydict['density'] just returns 2.0. If I change mydict['mass'] = 2.0, the density will not be updated. Fine - I can kind of understand why - the density is defined by the values when they were passed to the declaration. So I thought maybe I could approach this with a lambda expression, eg (apologies for the horrid code!):</p>
  
  <pre><code>mydict['density_calc'] = lambda x,y: x/y
  mydict['density'] = mydict['density_calc'](mydict['mass'],mydict['volume'])
  </code></pre>
  
  <p>But again, this only returns the original density, despite changing mydict['mass']. As a final attempt, I tried this:</p>
  
  <pre><code>def density(mass,volume): return mass/volume
  mydict['density_calc'] = lambda x,y: density(x,y)
  mydict['density'] = mydict['density_calc'](mydict['mass'],mydict['volume'])
  </code></pre>
  
  <p>Again, no dice. This seems like a really simple problem to solve, so apologies in advance, but if anyone could help me out, I'd be very appreciative!</p>
  
  <p>Cheers,</p>
  
  <p>Dave</p>