Created
July 27, 2013 01:29
-
-
Save chenko515/6093286 to your computer and use it in GitHub Desktop.
python3, with, contextlib, closing(), urlopen()
using python3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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