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