+ 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)
2 Respostas
+ 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).
- 1
hhff