On Thu, Nov 20, 2014 at 2:36 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
On 20 November 2014 22:54, MRAB <python@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.
The "while" expresses intent, "return" caters to low-level mechanics. The "nested compound statements" explanation is already not that good: why does the value come first, not last? For readability*. The difference between function/nested compound statements syntax and comprehension syntax is already so big, and the low-level "while x:"→"if not x: break" translation is so easy, that between the two the readability of "while" should win. Outside of comprehensions, an expression that never has a value seems quite backwards (and dangerous, in this case). * (or you might say, for similarity to math notation)