[Python-ideas] revisit pep 377: good use case?
Gregory P. Smith
greg at krypto.org
Fri Mar 2 23:26:47 CET 2012
On Wed, Feb 29, 2012 at 6:24 AM, Michael Foord <fuzzyman at gmail.com> wrote:
>
>
> On 29 February 2012 08:23, Nick Coghlan <ncoghlan at gmail.com> wrote:
>
>>
>> One way to handle this case is to use a separate if statement to make
>> the flow control clear.
>>
>> with cm() as run_body:
>> if run_body:
>> # Do stuff
>>
>> Depending on the use case, the return value from __enter__ may be a
>> simple flag as shown, or it may be a more complex object.
>>
>
>
> The trouble with this is it indents all your code an extra level. One
> possibility would be allowing continue in a with statement as an early exit:
>
> with cm() as run_body:
> if not run_body:
> continue
>
>
-1 on this as an early __exit__.
It would be context dependent. For with statements within a loop body, a
continue today continues to the next loop iteration. Introducing this
syntax would call into question what continue does... exit the with
statement within the loop body? or continue the loop (also exiting the
with statement but skipping all other code in the loop body)?
for x in range(5):
with y() as run_body:
if not run_body:
continue
print x, run_body
Changing the existing continue semantics would break existing code and
adding continue semantics to exit a context manager that care if a with is
within a loop body or not seems very unwise and surprising.
-gps
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120302/550bd5ac/attachment.html>
More information about the Python-ideas
mailing list