+ 6

Pls explain me this

def cm(): return[lambda x:i*x for i in range(3)] # what it is returning for m in cm(): print(m(1)) # why m(1)

28th Dec 2020, 11:16 AM
[bool left=True;]
[bool left=True;] - avatar
2 Answers
+ 8
Function ‘cm’ returns a list, in this list each element is a function that takes one parameter ‘x’ and returns it’s multiplication by ‘i’. So this list looks smth like this: [ lambda x: x*i, lambda x:x*i, lambda x: x*i ] for m in cm() - means that on each iteration the loop takes one element - function (called ‘m’) from this list and prints the result of its execution with parameter ‘x’ (which is equal to 1).
28th Dec 2020, 2:50 PM
Mikhail Solovyev
Mikhail Solovyev - avatar
- 1
hhff
29th Dec 2020, 4:47 PM
Bahadır Akyasan
Bahadır Akyasan - avatar