[Python-ideas] use context managers for new-style "for" statement
dangyogi at gmail.com
Sat Feb 21 15:36:05 CET 2009
> On Fri, Feb 20, 2009, Raymond Hettinger wrote:
>> Bruce Frederikson:
>>> for i in closing(gen(x)):
>>> if you want the generator closed automatically.
>> That doesn't really improve on what we have now:
>> with closing(gen(x)) as g:
>> for i in g:
>> The proposed syntax puts to much on one-line and unnecessarily
>> complicates another one of Python's fundamental tools.
> In addition to Bruce's other followup, saving a level of indention does
> have some utility. That's not enough by itself, of course.
Yes, the *close* capability of generators can be exercised with an
external "with closing" statement (except for itertools.chain, which has
an inner generator which is inaccessible to the caller, but this
proposal doesn't fix that problem either).
But the improvement comes when you want to exercise the *throw*
capability of generators within a for statement. Adding an optional
__throw__ to context managers and honoring it in with statements doesn't
let the generator convert the exception into a new yielded value to be
tried ("oh, you didn't like the last value I yielded, try this one
instead") which is one use of generator.throw.
And this solves both issues (close and throw) with the same mechanism,
so it's conceptually simpler for the user than two separate solutions.
It also naturally extends the context manager capability to be able to
encapsulate try/except patterns into re-usable context managers, in
addition to encapsulating try/finally patterns. Here, I'm referring to
the standard use of context managers in with statements, irrespective of
the for statement.
Oh, and, yes, it does allow the programmer to elide the use of the with
statement in combination with the for statement. In my practice, 90% of
the with statements I use are this with/for pattern so I imagine that
this would also be appreciated by the Python community. But this is not
the sole benefit, nor even the most important benefit. So while I might
agree with "That's not enough by itself", I wish to point out that this
last benefit is not "by itself".
More information about the Python-ideas