Yield inside try...finally
W Isaac Carroll
icarroll at pobox.com
Thu Jun 5 06:13:43 EDT 2003
Jeremy Fincher wrote:
> I wonder if he's considered relaxing that restriction to allow at most
> *one* try: finally: block, or any number of try: finally: blocks whose
> finally: clauses are identical (which is effectively equivalent to a
> single finally: clause). He could then just compile the finally code
> into an __del__ method on the generator-iterator.
I don't think it's that simple. For example:
def finalgen():
while not done:
try:
foo = makefoo()
yield foo
finally:
cleanup(foo)
yield somethingelse
At the first yield statement, the generator would need a __del__ method
that calls cleanup(foo). At the second yield statement, no __del__
method is needed because cleanup(foo) was called when the generator was
resumed. Solving this problem, however, would lift your proposed
restriction that only one finally block be allowed.
TTFN
More information about the Python-list
mailing list