[Python-ideas] combine for/with statement

Bruce Leban bruce at leapyear.org
Wed Aug 3 07:10:49 CEST 2011

On Tue, Aug 2, 2011 at 6:11 PM, Mark McDuff <mmcduff at gmail.com> wrote:

> I find that I am often writing code in the following pattern:
> foo = MyContextManager(*args)
> for bar in my_iter:
>   with foo:
>       # do stuff
> I think it would be much cleaner to be able to write:
> for bar in my_iter with MyContextManager(*args):
>   # do stuff

The parts of the for statement have *no connection at all* to the parts of
the with statement. They're just stuck together which doesn't make much
sense to me.

When I read the subject of the original mail I immediately thought of this

     with open(foo) as _:
          for line in _:
               # stuff

which would at least make some sense if we could splice these together as

    with line in open(foo):

But no matter how common this might be, I have to agree with:

On Tue, Aug 2, 2011 at 9:32 PM, Chris Rebert <pyideas at rebertia.com> wrote:

> ... Down this path lies Perl ...

For every combination like this, there's another one just past it on the
road to Perl.

--- Bruce
Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110802/33c6e2c1/attachment.html>

More information about the Python-ideas mailing list