On Sunday 02 February 2003 10:41 pm, Brett Cannon wrote: ... > > > with myfile = auto_closing_file('blah.txt', 'rb'): > > > for line in myfile: ... > So if someone (read: Guido and Samuele =) could implement a file that > auto-closes once the iterator on the file is exhausted with each > suggestion I would *really* appreciate it. And if there could even be > code given that used what these things returned by searching each returned > line for the word "Python" would be great as well. That way we not only As I understood Samuele's "with" proposal (enhanced to allow assignment in the with, and to make __enter__ optional): class auto_closing_file(file): __exit__ = file.close with myfile = auto_closing_file('blah.txt'): for line in myfile: if line.find('Python')>=0: print line, If __enter__ were not optional, auto_closing_file would have to add one boilerplate line defining it: class auto_closing_file(file): __exit__ = file.close def __enter__(self): pass Actually, it would seem sensible, if we added this 'with', to extent built-in file by this tiny bit -- let it have __exit__ as a synonym of close (and if needed __enter__ as a no-operation method) so it can be used directly in what would then become a very common idiom: with myfile = open('blah.txt'): for line in myfile: if line.find('Python')>=0: print line, I don't understand the 'do' concepts well enough to code to them, as I have not followed this thread closely enough for this. Alex
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4