title: variable in try clause not accessible in finally clause - python
body: <p>I am new to python, so sorry if this question is dumb, but can someone tell me what's going on here.</p>
  
  <p>When I run the following code with no errors in the mdb.connect() call, the code runs fine.</p>
  
  <p>But when I purposely insert an error (for example, put in 'localhostblahblah'), I get a 'NameError: name 'con' is not defined' error when I execute.</p>
  
  <p>I thought that variables defined in the try clause should be accessible in the finally clause. What's going on?</p>
  
  <pre><code>#!/usr/bin/python
  
  import MySQLdb as mdb
  import sys
  
  try:
      con = mdb.connect('localhost','jmtoung','','ptb_genetics')
  
  except mdb.Error, e:
      print "Error"
      sys.exit(1)
  
  finally:
      if con:
          con.close()
  </code></pre>