Skip to content

Instantly share code, notes, and snippets.

@chenko515
Created July 27, 2013 01:29
Show Gist options
  • Save chenko515/6093286 to your computer and use it in GitHub Desktop.
Save chenko515/6093286 to your computer and use it in GitHub Desktop.
python3, with, contextlib, closing(), urlopen() using python3
from contextlib import contextmanager
@contextmanager
def closing(thing):
try:
yield thing
finally:
thing.close()
And lets you write code like this:
from contextlib import closing
from urllib.request import urlopen
with closing(urlopen('http://www.python.org')) as page:
for line in page:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment