title: How does one monkey patch a function in python?
body: <p>I'm having trouble replacing a function from a different module with another function and it's driving me crazy.</p>
  
  <p>Let's say I have a module bar.py that looks like this:</p>
  
  <pre><code>from a_package.baz import do_something_expensive
  
  def a_function():
      print do_something_expensive()
  </code></pre>
  
  <p>And I have another module that looks like this:</p>
  
  <pre><code>from bar import a_function
  a_function()
  
  from a_package.baz import do_something_expensive
  do_something_expensive = lambda: 'Something really cheap.'
  a_function()
  
  import a_package.baz
  a_package.baz.do_something_expensive = lambda: 'Something really cheap.'
  a_function()
  </code></pre>
  
  <p>I would expect to get the results:</p>
  
  <pre><code>Something expensive!
  Something really cheap.
  Something really cheap.
  </code></pre>
  
  <p>But instead I get this:</p>
  
  <pre><code>Something expensive!
  Something expensive!
  Something expensive!
  </code></pre>
  
  <p>What am I doing wrong?</p>