On 10 August 2017 at 21:25, Chris Barker <chris.bar...@noaa.gov> wrote:
> On Thu, Aug 10, 2017 at 8:39 AM, Paul Moore <p.f.mo...@gmail.com> wrote:
>> Also, there's a potential issue
>> here - consider
>> [expr for var in even_numbers() if is_odd(var) while var < 100]
>> This is an infinite loop, even though it has a finite termination
>> condition (var < 100), because we only test the termination condition
>> if var is odd, which it never will be.
> why is the termination only tested if teh if clause is True? Could then not
> be processed in parallel? or the while first....
See? That's my point - the "obvious" interpretation stops being
obvious pretty fast...
> so maybe better to do:
> [expr for var in even_numbers() while var < 100 if is_odd(var)]
That would work. But I bet people's intuition wouldn't immediately
lead to that fix (or indeed, necessarily incline them to put the
clauses in this order in the first place).
> Maybe it's just me, but I would certainly expect the while to have
> I guess I think of it like this:
> "if" is providing a filtering mechanism
> "while" is providing a termination mechanism
> -- is there a use case anyone can think of when they would want the while
> to be applied to the list AFTER filtering?
Probably not, but when you can have multiple FORs, WHILEs and IFs, in
any order, explaining the behaviour precisely while still preserving
some sense of "filtering comes after termination" is going to be
pretty difficult. [expr for var1 in seq1 if cond1 for var2 in seq2 for
var3 in seq3 if cond2 if cond3] is legal - stupid, but legal. Now add
while clauses randomly in that, and define your expected semantics
clearly so a user (and the compiler!) can determine what the resulting
The main benefit of the current "works like a for loop" interpretation
is that it's 100% explicit. Nothing will make a mess like the above
good code, but at least it's well-defined.
Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/