+1

# What is lambda doing here? What is the meaning of m(1)?

def cm(): return[lambda x:i*x for i in range(3)] for m in cm(): print(m(1),end='') Output 222

5/21/2019 6:33:58 AM

Subham

+1

https://www.sololearn.com/discuss/1265773/?ref=app https://www.sololearn.com/discuss/1267956/?ref=app https://www.sololearn.com/discuss/1323983/?ref=app

+1

Thanks Diego

+1

A lambda function is a nameless or anonymous function in your example it is used to populate the list which is returned by cm() function. Here for loop which is iterating over the range(3) makes 3 copies of lambda function as the range is from 0 to 2. As any other function lambda also takes a parameter here it is x ur specified value of x as 1 in m(1). While creating 3 copies of lambda i reached to its maximum value that is 2 so multiplying x with i gives the list as [2,2,2]. For more info check for the list comprehension and lambda function. To check my answer add one more line at the end of your code i.e. print(cm())

+1

Output 222[<function cm.<locals>.<listcomp>.<lambda> at 0x7fa7ed5aed90>, <function cm.<locals>.<listcomp>.<lambda> at 0x7fa7ed5aec80>, <function cm.<locals>.<listcomp>.<lambda> at 0x7fa7ed5aeea0>]

0

2[<function cm.<locals>.<listcomp>.<lambda> at 0x7f57ffaa3ea0>, <function cm.<locals>.<listcomp>.<lambda> at 0x7f57ffaa3f28>, <function cm.<locals>.<listcomp>.<lambda> at 0x7f57ffaac048>] 2[<function cm.<locals>.<listcomp>.<lambda> at 0x7f57ffaa3f28>, <function cm.<locals>.<listcomp>.<lambda> at 0x7f57ffaa3c80>, <function cm.<locals>.<listcomp>.<lambda> at 0x7f57ffaac048>] 2[<function cm.<locals>.<listcomp>.<lambda> at 0x7f57ffaa3c80>, <function cm.<locals>.<listcomp>.<lambda> at 0x7f57ffaa3ea0>, <function cm.<locals>.<listcomp>.<lambda> at 0x7f57ffaac048>] Output of ur print statement Yugabdh

0

Lol just realised that I have answered this already 😅 How u guys keep track of all this?

0

Subham I think, u have put print(cm()) at the wrong place. Copy this def cm(): return[lambda x:i*x for i in range(3)] for m in cm(): print(m(1),end='') print(cm())

0

#What is lambda doing here? What is the meaning of m(1)? def cm(): return[lambda x:i*x for i in range(3)] for m in cm(): print(m(1),end='') print (cm()) #Output 222 This is what my code looks like Yugabdh

0

U don't have to intend the last line. Try what I suggested you will find a list with some abstractions in it i.e. objects of lambda and listcomp There will be 3 such objects suggesting that three lambda objects are created.

0

https://code.sololearn.com/cUtrMtdd4e4S/?ref=app