Unexpected content of list comprehension with lambdas | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Unexpected content of list comprehension with lambdas

Hello SL community, I would appreciate it if you help me with the following problem: Can you explain the following code ? https://code.sololearn.com/c9an6szV8etn/?ref=app

29th Jan 2020, 9:10 PM
Aymen
19 Answers
+ 7
Kishalaya Saha yes... a list comprehension generates a closure but not a.... tuple comprehension... i. E generator.
30th Jan 2020, 2:24 PM
Oma Falk
Oma Falk - avatar
+ 5
Ha, nice, I knew I had explained it somewhere but didn't know when and where. You guys found it! :) Thx, Qasem!
29th Jan 2020, 11:41 PM
HonFu
HonFu - avatar
+ 4
HonFu but not the function makes the closure. Otherwise my linked prog couldn't work. It is the list comprehension
30th Jan 2020, 6:25 AM
Oma Falk
Oma Falk - avatar
+ 4
yessine HonFu ..... It is worth that. Real cool stuff.
30th Jan 2020, 9:16 AM
Oma Falk
Oma Falk - avatar
+ 4
Oma Falk, ah, yes, I think, there's not really a difference in what we're saying. In that other thread I just 'translated' the list comp to a regular nested function creating the same closure effect, in order to illustrate it.
30th Jan 2020, 1:36 PM
HonFu
HonFu - avatar
+ 4
HonFu if you await behavior in that prog, you are right https://code.sololearn.com/cNU52yK69BSj/?ref=app
30th Jan 2020, 1:43 PM
Oma Falk
Oma Falk - avatar
+ 4
actually the second version is a generator, not a tuple , am I not right ?
30th Jan 2020, 1:50 PM
Aymen
+ 4
Yeah, a generator is only a 'construction plan', so that generator will generate each function object as needed and hand over the latest i. Interesting! If I apply that old pattern of explanation, it would look like this now: def cm(): def f(x): return x*i l = [] for i in range(3): yield f
30th Jan 2020, 1:54 PM
HonFu
HonFu - avatar
+ 4
Katie Hattab you have a contribution to that topic ?
30th Jan 2020, 3:02 PM
Oma Falk
Oma Falk - avatar
+ 3
Yes, Qasem you found it, thank you
29th Jan 2020, 11:43 PM
Aymen
+ 3
yessine you're absolutely right! It's a generator, not a tuple. And that explains it!
30th Jan 2020, 1:54 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
I try to explain easier: All lambdas in cm depend on one variable i. But one variable can only have one value - that is the last one assigned to it = 2. Closures are really a bit tricky
29th Jan 2020, 9:46 PM
Oma Falk
Oma Falk - avatar
+ 1
In cm the function objects are not really built in list comprehension but only their description. Although the defining function is gone, they remember life in their function and share the same last memory concerning i which is 2. So we have a closure.
29th Jan 2020, 9:30 PM
Oma Falk
Oma Falk - avatar
+ 1
cm2 is not a closure all lambda are independent of their birthplace and share no common value.
29th Jan 2020, 9:32 PM
Oma Falk
Oma Falk - avatar
+ 1
By the way...it is easy to curate since tuples don't create an own closure https://code.sololearn.com/cJiE17h7fR1v/?ref=app
29th Jan 2020, 10:18 PM
Oma Falk
Oma Falk - avatar
+ 1
Oma Falk, seems I have to catch up with what you wrote and think about it for a moment...
30th Jan 2020, 9:12 AM
HonFu
HonFu - avatar
+ 1
Oma Falk I think difference in your version might have something to do with tuples being immutable. If we replace it by a list, a set, or even a dictionary, it works (fails?) like before. P.S. I, too, attempted to explain this code nearly a year ago 😄 It's pretty similar to what you and HonFu said. https://www.sololearn.com/Discuss/1577036/list-lambda Edit: turns out it's not a tuple, but a generator, as pointed out later. And it makes a lot more sense.
30th Jan 2020, 1:47 PM
Kishalaya Saha
Kishalaya Saha - avatar
0
Oma Falk I don't know what's a function description.. I guess I'll have to learn about it.
29th Jan 2020, 9:36 PM
Aymen