[Python-ideas] Return expressions

Nick Coghlan ncoghlan at gmail.com
Thu Nov 20 14:36:26 CET 2014


On 20 November 2014 22:54, MRAB <python at mrabarnett.plus.com> wrote:

> I'd prefer 'while' instead:
>
> >>> [x for x in range(10) while x < 5]
> [0, 1, 2, 3, 4]
> >>> {x for x in range(10) while x < 5}
> {0, 1, 2, 3, 4}
> >>> {x:x for x in range(10) while x < 5}
> {0:0, 1:1, 2:2, 3:3, 4:4}
>

That's been suggested many times, and always fails on the grounds of being
completely incompatible with the expansions of comprehensions and generator
expressions as nested compound statements.

The return expression idea is one that reflects the actual deep structure
of the nested scopes that underlie the syntactic sugar, so it's compatible
with the expansion.

This idea was mainly aimed at folks that might be inclined to worry about
the possible loss of the "or stop()" generator expression trick in PEP 479,
though. If that trick wasn't currently possible, and wasn't being proposed
for removal, I never would have posted this idea.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141120/a5af1969/attachment.html>


More information about the Python-ideas mailing list