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

Terry Reedy tjreedy at udel.edu
Mon Mar 24 21:31:46 EDT 2014


On 3/24/2014 8:28 PM, Chris Angelico wrote:

> Pure functional programming, from what I understand, doesn't *have*
> variables other than function arguments.

function *parameters*, if in the 'variable = name' camp

> So the way to implement "x = 1" is to call a subfunction

with a parameter named 'x'

> with an argument of 1

Funny, I was just thinking about that last night. I really learned it 
when trying to translate python code to the scheme dialect racket.

import math as m

s = m.sqrt(2)
a = m.sin(s) + m.cos(s)
del s # to be exactly equivalent to the below, but not needed

b = (lambda x: m.sin(x) + m.cos(x))(m.sqrt(2))

print(a,b)
 >>>
1.14370964075811 1.14370964075811


-- 
Terry Jan Reedy




More information about the Python-list mailing list