Q:

Good Python with statement explanation

I've tried google and other places but I can't seem to find a good explanation of the with statement. In what situations is it useful? I get how it works with files but how else could it be used?

A:

Here's a good example:
  class controlled_execution:
            def __enter__(self):
                set things up
                return thing
            def __exit__(self, type, value, traceback):
                tear things down

        with controlled_execution() as thing:
             some code

When the “with” statement is executed, Python evaluates the expression, calls the enter method on the resulting value (which is called a “context guard”), and assigns whatever enter returns to the variable given by as. Python will then execute the code body, and no matter what happens in that code, call the guard object’s exit method.

