Pot: the with statement
Python’s with statement can be a very elegant alternative to long try/except/finally clauses. It offers a standard protocol that classes can implement to properly clean up state. The best example for it’s value is file reading. A good implementation would have to look like this traditionally: f = open('/tmp/myfile', 'r') try: content = f.read() finally: f.close() The with statement shortens this to the following code: with open('/tmp/myfile', 'r') as f: content = f.read() Behind the scenes it wraps the finally statement around this and makes sure that the file gets closed upon leaving the with block. ...