Yield inside try...finally
mcherm at mcherm.com
Thu Jun 5 21:09:25 CEST 2003
Alan Kennedy writes:
> The real problem here is the fact that try..finally has been actively
> disabled inside generators,
> I wonder if he's considered relaxing that restriction to allow at most
> *one* try: finally: block, [...]
> He could then just compile the finally code
> into an __del__ method on the generator-iterator.
Won't work. In particular, __del__ may not be called at any known
time. In cPython, it's normally (whenever cycles don't occur) called
as soon as the last reference is released, but in, for instance,
Jython it won't be called until the java VM gets around to it. There's
no ironclad guarantee of its being called ever. And the same problem
exists in cPython for objects that participate in reference cycles.
Too bad, that, but it can't easily be avoided.
-- Michael Chermside
More information about the Python-list