title: How to create a global variable if it doesn't exist in Python?
body: <p>I want to define a Python function doing:
  1. Check if a variable already exist.
  2. If not, create it as a global variable (because I want to use it outside the function).</p>
  
  <pre><code>def foo():
      try:
          x
      except NameError:
          global x
          x = 1
      else:
          pass
  
  foo()
  print(x)
  </code></pre>
  
  <p>Then there is an error: </p>
  
  <blockquote>
    <p>SyntaxError: name 'x' is used prior to global declaration</p>
  </blockquote>
  
  <p>How to solve this? Thank you :)</p>