+ 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