Created
June 10, 2013 18:01
-
-
Save baroquebobcat/5750890 to your computer and use it in GitHub Desktop.
With as impl w/ for
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
| # Why is there a with construct, when you can build it with | |
| # for loop and yield? | |
| # | |
| # ;) | |
| def open_f(filename, mode): | |
| try: | |
| f = open(filename, mode) | |
| yield f | |
| finally: | |
| try: | |
| f.close() | |
| except: | |
| pass | |
| def read_lines(filename): | |
| for f in open_f(filename, 'r'): | |
| for line in f: | |
| yield line | |
| for line in read_lines('testfile'): | |
| print("line: " + line.rstrip("\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment