Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Mark H Harris harrismh777 at gmail.com
Mon Mar 24 22:39:33 EDT 2014


On 3/24/14 8:20 PM, Terry Reedy wrote:
> On 3/24/2014 7:56 PM, Mark H Harris wrote:

>> the list which is used for each of the adder[] functions created.
>
> Wrong. Functions look up global and nonlocal names, such as n, when the
> function is called.
>

hi Terry, yeah, I know; this is what's *wrong* all at once.

Functions "look up global and nonlocal names" such as n, is semantics 
for "each created function 'grabs' the last number (n) in the list", 
well, because that (n) is bound at call-time to (3).

Your version semantically is detailed and correct; my version 
semantically is "how it is perceived" by the user, which is also 
correct. Again, how the function gets the n (grab or lookup) is mute. 
The user is often confused about how this happens. As you have shown, 
even experts in this field disagree about how this is described, which 
is also my secondary point--- the whole thing is VERY difficult to 
explain to "normal" users. It often takes several sessions and goes on 
and on, until Mark Lawrence calls it tedious.

marcus



More information about the Python-list mailing list