title: Using a variable in a try,catch,finally statement without declaring it outside
body: <p>I'm pretty new to Python, here is some code I am looking at:</p>
  
  <pre><code>try:
      connection = getConnection(database)
      cursor = connection.cursor()
      cursor.execute("some query")
  except:
      log.error("Problem.")
      raise
  finally:
      cursor.close()
      connection.close()
  </code></pre>
  
  <p>Is that being cleaned up properly? In other languages I have written in, I am used to doing something like this:</p>
  
  <pre><code>connection = None
  cursor = None
  
  try:
      connection = getConnection(database)
      cursor = connection.cursor()
      cursor.execute("some query")
  except:
      log.error("Problem.")
      raise
  finally:
      if cursor is not None:
          cursor.close()
      if connection is not None:    
          connection.close()
  </code></pre>