Python antipattern: Close in finally

Don t do this: thing = Thing() try: thing.do_stuff() finally: thing.close() Do do this: from contextlib import closing with closing(Thing()) as thing: thing.do_stuff() Why is the second better? Using contextlib.closing() ties closing the item to its creation. These baby examples are about equally easy to reason about, with only a single line in the try block, Continue reading Python antipattern:…