Alex Martelli <aleax@aleax.it> writes: > be helpful too? Meaning to use local variable <identifier> in lieu of > the abstract _x -- for example in order to enable: > > with myfile = auto_closing_file('blah.txt', 'rb'): > xx = myfile.read(23) > # rest of suite snipped > > where auto_closing_file is a subclass of file defining useful __enter__ > (empty -- might be nice to have it optional...) and __exit__ = close > synonyms (or file itself might grow __exit__ as a synonym for close). > > How would I do this elegantly without the assignment...? Just as you do with if: myfile = auto_closing_file('blah.txt', 'rb') with myfile: xx = myfile.read(23) # rest of suite snipped Maybe not quite as elegant as allowing assignment in with but very similar to the normal try...final version: myfile = auto_closing_file('blah.txt', 'rb') try: xx = myfile.read(23) # rest of suite snipped finally: myfile.close() Bernhard -- Intevation GmbH http://intevation.de/ Sketch http://sketch.sourceforge.net/ MapIt! http://www.mapit.de/
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