unintuitive for-loop behavior

Steve D'Aprano steve+python at pearwood.info
Sat Oct 1 07:32:17 EDT 2016


On Sat, 1 Oct 2016 01:44 pm, Rustom Mody wrote:

> Yes one basic problem with comprehensions in python is that they are
> defined by assignment not binding to the comprehension variable

¿Que Mr Fawlty?

I'm sorry, I don't understand you.

In Python, all assignments are name bindings. So you seem to be saying:

    one basic problem with comprehensions in python is that they 
    are defined by binding (to the comprehension variable) not 
    binding to the comprehension variable

which confuses me. How do you think that comprehension variables are
different from all other variables?

And while we're at it, what do you mean by "comprehension variable"? Are you
talking about the variable that the comprehension is bound to:

comprehension_variable = [x+1 for x in sequence]

or are you talking about the loop variable inside the comprehension?

foo = [comprehension_variable+1 for comprehension_variable in sequence]


It is not clear what you mean. Although I suspect it makes no difference --
either way, the nature of the assignment is identical: its a name binding.



> Python copied comprehensions from haskell and copied them wrong
> Here are all the things (that I can think of) that are wrong:
> 1. Scope leakage from inside to outside the comprehension

The core devs agree that this was a mistake. It was rectified in generator
expressions, and for comprehensions in Python 3. This is no longer an
issue.


> 2. Scope leakage from one value to the next

I don't know what that means.


> 3. The name 'for' misleadingly associates for-loops and comprehensions

What is misleading about it?


> 4. The for-loop based implementation strategy made into definitional
> semantics 
> 5. The absence of simple binding inside comprehensions: 
>    [f(newvar) for v in l newvar = rhs]

I don't know what this means.


> 1 was considered sufficiently important to make a breaking change from
> python2 to 3 

Correct.


> 2 is what causes the lambda gotcha 

I don't think so.


> 3 is what makes noobs to take longer than necessary to grok them

You haven't shown any evidence that beginners take longer than necessary to
grok list comprehensions. Perhaps they take exactly as long as necessary.

And you CERTAINLY haven't demonstrated that the average beginner would
understand Haskell's list comprehensions more easily than Python's.


https://wiki.haskell.org/List_comprehension

https://www.haskell.org/onlinereport/exps.html#sect3.11



> 4 is what causes a useless distinction between 1 and 2 — scope leakage is
> scope leakage. 

What is scope leakage?


> The explanatory mechanisms of why/whither/what etc should 
> at best be secondary 5. is workaroundable with a [... for newvar in [rhs]] 
>    Possible and clunky

I don't know what this means.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list