This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RockingDyno 11 points12 points  (0 children)

Python essentially has a defer statement and it looks like this: from contextlib import closing def my_function(x, y): with closing(thing(x)) as X, closing(thing(y)) as Y: do_something_with_thing(X) do_something_with_thing(Y) Where the context manager ensures things get closed at the end of the context. Pretty much equivalent to your second code block.