title: What's the scope of a variable initialized in an if statement?
body: <p>I'm new to Python, so this is probably a simple scoping question. The following code in a Python file (module) is confusing me slightly:</p>
  
  <pre><code>if __name__ == '__main__':
      x = 1
  
  print x
  </code></pre>
  
  <p>In other languages I've worked in, this code would throw an exception, as the <code>x</code> variable is local to the <code>if</code> statement and should not exist outside of it. But this code executes, and prints 1. Can anyone explain this behavior? Are all variables created in a module global/available to the entire module?</p>