[Python-ideas] use context managers for new-style "for" statement

Aahz aahz at pythoncraft.com
Sat Feb 21 04:53:38 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.
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.

More information about the Python-ideas mailing list