[Python-ideas] Allowing breaks in generator expressions by overloading the while keyword

Masklinn masklinn at masklinn.net
Fri Feb 21 14:34:49 CET 2014


On 2014-02-21, at 14:03 , M.-A. Lemburg <mal at egenix.com> wrote:
> On 21.02.2014 13:25, Nick Coghlan wrote:
>> On 21 February 2014 22:18, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>> That's not very readable to most Python programmers, but what if you
>>> could write something like:
>>> 
>>>    isprime = all(n % p for p in takewhile((: ? ** 2 < n), primes_seen))
>>> 
>>> This is somewhat similar to the implicit lambda proposal in
>>> http://legacy.python.org/dev/peps/pep-0312/, but with the following
>>> two essential differences:
>>> 
>>> 1. The parentheses would be required (as per generator expressions,
>>> and as is being discussed for except expressions)
>>> 2. By using a "?" token within the implicit lambda, you would create a
>>> lambda that takes a single argument. If there is no such token, then
>>> it would take no arguments.
>> 
>> Oh, and under such a proposal, the generator expression:
>> 
>>    (x for x in seq)
>> 
>> would be semantically equivalent to:
>> 
>>    (: yield x for x in ?)(seq)
>> 
>> Currently, there's no underlying construct you can decompose a
>> generator expression into, because there's no notation for a lambda
>> expression with an anonymous parameter.
> 
> Hmm, this reminds me too much of regular expression syntax :-)
> 
> I wonder why people are so keen on stuffing too much logic into
> a single line. Must be a twitter/SMS side-effect.

"Line" is a red herring, I think. Nobody *really* cares about lines.
"Expression" is the important factor. Moving stuff out of expressions
and into separate statements requires finding out a name for the result,
is generally significantly more verbose and isn't necessarily clearer.

Depending on the situation, it may also require significant reworking of
existing (supposedly working) code.

I usually write non-trivial generator expressions over multiple lines,
I may be after them remaining a single (readable) expression depending
on context.

Well that's just my experience anyway.

> Programs don't get faster that way, they don't get more readable,
> you don't get to do more things that couldn't do otherwise
> and requiring a master in computer science to be able to understand
> what goes on in one of those magical lines doesn't feel right
> to me either, given that we are promoting Python as first
> programming language.

Was the MSCS insult really necessary?

> Of course, tossing around ideas like these is fun and I don't
> want to spoil it. Eventually something useful will come out of
> these discussions, I'm sure :-)



More information about the Python-ideas mailing list