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

Chris Angelico rosuav at gmail.com
Mon Mar 24 19:01:27 EDT 2014


On Tue, Mar 25, 2014 at 8:43 AM, Mark H Harris <harrismh777 at gmail.com> wrote:
>>         adders[n] = (lambda b: lambda a: b + a)(n)
>
>    Now, here is where I talk about confusion; explaining why the first
> lambda above does not work because of scope and closure, and then even
> worse, explaining why the second "double" lambda works in the lower example!

Easy fix. Use the "explicit capture" notation:

adders[n] = lambda a, n=n: a+n

And there you are, out of your difficulty at once!

ChrisA



More information about the Python-list mailing list