Home > Software engineering >  Why does a lambda incorporated with the argument directly in brackets not give me the desired output
Why does a lambda incorporated with the argument directly in brackets not give me the desired output

Time:10-26

When I write this:

def calculator(operation, n1, n2):
    return operation(n1, n2)`

print (calculator(lambda n1, n2: n1 * n2, 10, 20))

I get the desired output of 200.

But when I write this:

print (lambda n1,n2: n1*n2 (10,20))

I do not get 200 as the output.

Why is this so?

CodePudding user response:

The solution is to wrap your lambda assignment within paranthesis () as below:

print((lambda n1, n2: n1 * n2)(10, 20))

Prints:

200

Alternatively, you can assign the lambda expression to a variable, and then use the variable:

l = lambda n1, n2: n1 * n2
print(l(10, 20))
  • Related