[Python-ideas] Return expressions

Nathaniel Smith njs at pobox.com
Thu Nov 20 19:02:31 CET 2014


On Thu, Nov 20, 2014 at 4:36 PM, Chris Angelico <rosuav at gmail.com> wrote:
> On Fri, Nov 21, 2014 at 3:21 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> That does also suggest another possible alternative to the "or stop()"
>> trick - allow *break* as an expression.
>
> Or just as a keyword component of a comprehension, in the same way
> 'for' is. Since 'if' already has meaning, [x for x in range(10) if x
>>= 5 break] would be confusing, so probably it'd have to be the
> slightly-awkward-to-read [x for x in range(10) break x >= 5], adding a
> termination condition to the preceding loop.

'break' is certainly better than 'return' -- currently 'break' means
"look for the nearest enclosing loop", not "look for the enclosing
function", so it'd preserve that at least.

[x for x in range(10) if x >= 5 else break]?

with 'else break' handled in the grammar as a non-compositional phrase
like 'is not'? Not sure how this interacts with multiple mixed for and
if clauses -- I've never wrapped my head around those semantics.

-n

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org


More information about the Python-ideas mailing list