still thinking about this stuff. At 01:19 07.06.2003 +0200, Samuele Pedroni wrote: >I thought python-dev archives would be a better place for this than my hd. >Warning: terse. > >I have come to the conclusion that I should agree with Guido: > >- generators do not let really abstract over things like exception >handling etc. what I had in mind were generators like this one: def all_lines(file_list_filename): for fn in open(file_list_filename,'r'): fn = fn.rstrip() for line in open(fn,'r'): yield line for line in all_lines("all_files"): <do-stuff> what if we want to release resources (here in this toy example namely close files) in a timely fashion in face of exception also in <do-stuff>. all of these touch on the issue: - non-support of 'yield' inside try-finally - PEP 288: Generators Attributes and Exceptions - PEP 310: Reliable Acquisition/Release Pairs - proposals for syntax extensibility through Ruby/Smalltalk-like first-class blocks so all these proposals should be considered together and in an organic way. With current Python a possible rewrite of the above would be: class all_lines(object): def __init__(self,file_list_fname): self.listf = open(file_list_fname,'r') self.f = None def __iter__(self): for fn in self.listf: fn = fn.rstrip() self.f = open(fn,'r') for line in self.f: yield line self.f.close() self.f = None def close(self): self.listf.close() if self.f: self.f.close() x = all_lines("all_files") try: for line in x: <do-stuff> finally: x.close() kind of obfuscated. maybe-generators-should-grow-a-close-method-ly y'rs - Samuele
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