title: In Python, variables inside if conditions hide the global scope even if they are not executed?
body: <pre><code>def do_something():
      print 'doing something...'
  
  def maybe_do_it(hesitant=False):
      if hesitant:
          do_something = lambda: 'did nothing'
      result = do_something()
      print result
  
  maybe_do_it()
  </code></pre>
  
  <p>The result of this code is:</p>
  
  <pre><code>  File "scope_test.py", line 10, in &lt;module&gt;
      maybe_do_it()
    File "scope_test.py", line 7, in maybe_do_it
      result = do_something()
  UnboundLocalError: local variable 'do_something' referenced before assignment
  </code></pre>
  
  <p>But this code prints "did something..." as expected:</p>
  
  <pre><code>def do_something():
      print 'doing something...'
  
  def maybe_do_it(hesitant=False):
      result = do_something()
      print result
  
  maybe_do_it()
  </code></pre>
  
  <p>How did the function get overridden even though the condition inside the if statement never executed?  This happens in Python 2.7 -- is it the same in Python 3?</p>