[Python-ideas] combine for/with statement

Matt Joiner anacrolix at gmail.com
Wed Aug 3 05:53:11 CEST 2011


-1

On Wed, Aug 3, 2011 at 11:56 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list