[Python-ideas] combine for/with statement
Nick Coghlan
ncoghlan at gmail.com
Wed Aug 3 03:56:52 CEST 2011
On Wed, Aug 3, 2011 at 11:11 AM, 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
I'm not sure why you think putting the context manager way over on the
right hand side is an improvement, but no, merging arbitrary
statements is never going to happen (even the comprehension inspired
merging of for and if statements has been explicitly rejected many
many times).
As Carl notes, the ambiguity of the propose syntax is also not good -
it is unclear whether the context manager is recreated on each pass
around the loop, reused on each pass, or applied once to cover the
entire loop operation.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list