title: RAII in Python - automatic destruction when leaving a scope
body: <p>I've been trying to find <A HREF="https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization" rel="noreferrer">RAII</A> in Python.
  Resource Allocation Is Initialization is a pattern in C++ whereby
  an object is initialized as it is created. If it fails, then it throws
  an exception. In this way, the programmer knows that
  the object will never be left in a half-constructed state. Python
  can do this much.</p>
  
  <p>But RAII also works with the scoping rules of C++
  to ensure the prompt destruction of the object. As soon as the variable
  pops off the stack it is destroyed. This may happen in Python, but only
  if there are no external or circular references.</p>
  
  <p>More importantly, a name for an object still exists until the function it is
  in exits (and sometimes longer). Variables at the module level will
  stick around for the life of the module.</p>
  
  <p>I'd like to get an error if I do something like this:</p>
  
  <pre><code>for x in some_list:
      ...
  
  ... 100 lines later ...
  
  for i in x:
      # Oops! Forgot to define x first, but... where's my error?
      ...
  </code></pre>
  
  <p>I could manually delete the names after I've used it,
  but that would be quite ugly, and require effort on my part.</p>
  
  <p>And I'd like it to Do-What-I-Mean in this case:</p>
  
  <pre><code>for x in some_list:
      surface = x.getSurface()
      new_points = []
      for x,y,z in surface.points:
          ...     # Do something with the points
          new_points.append( (x,y,z) )
      surface.points = new_points
      x.setSurface(surface)
  </code></pre>
  
  <p>Python does some scoping, but not at the indentation level, just at
  the functional level. It seems silly to require that I make a new function
  just to scope the variables so I can reuse a name.</p>
  
  <p>Python 2.5 has the <a href="http://www.python.org/dev/peps/pep-0343/" rel="noreferrer">"with" statement</a>
  but that requires that I explicitly put in <code>__enter__</code> and <code>__exit__</code> functions
  and generally seems more oriented towards cleaning up resources like files
  and mutex locks regardless of the exit vector. It doesn't help with scoping.
  Or am I missing something?</p>
  
  <p>I've searched for "Python RAII" and "Python scope" and I wasn't able to find anything that
  addressed the issue directly and authoritatively.
  I've looked over all the PEPs. The concept doesn't seem to be addressed
  within Python.</p>
  
  <p>Am I a bad person because I want to have scoping variables in Python?
  Is that just too un-Pythonic?</p>
  
  <p>Am I not grokking it?</p>
  
  <p>Perhaps I'm trying to take away the benefits of the dynamic aspects of the language.
  Is it selfish to sometimes want scope enforced?</p>
  
  <p>Am I lazy for wanting the compiler/interpreter
  to catch my negligent variable reuse mistakes? Well, yes, of course I'm lazy,
  but am I lazy in a bad way?</p>