title: __main__ and scoping in python
body: <p>I was somehow surprised by the following behavior:</p>
  
  <pre><code>def main():
      print "%s" % foo
  
  if __name__ == "__main__":
      foo = "bar"
      main()
  </code></pre>
  
  <p>i.e. a module function has access to enclosing variables in the <code>__main__</code>. What's the explanation for it?</p>