Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding lambda in python and using it to pass multiple arguments

After reading everything I can find on lambda, I still don't understand how to make it do what I want.

Everyone uses the example:

lambda x, y : x + y

Why do you need to state both x and y before the :? Also how do you make it return multiple arguments?

for example:

self.buttonAdd_1 = Button(self, text='+', command=lambda : self.calculate(self.buttonOut_1.grid_info(), 1))

This works just fine. But the following code does not:

self.entry_1.bind("<Return>", lambda : self.calculate(self.buttonOut_1.grid_info(), 1))

It yields the error:

TypeError: () takes no arguments (1 given)

like image 719
Talisin Avatar asked Apr 27 '12 05:04

Talisin


People also ask

How do you pass multiple arguments in lambda function?

To pass multiple arguments in the lambda function, we must mention all the parameters separated by commas. Let us understand this with an example. We will create a lambda function that takes three parameters; a list and two integers.

How many arguments can lambda take Python?

Syntax of Lambda in Python You can use as many arguments as you want in a lambda function, but it can have only one expression.

How do you pass a lambda function as an argument in Python?

As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. This function can have any number of arguments but only one expression, which is evaluated and returned. Lambda function can also have another function as an argument.

How many arguments can lambda function take?

A lambda function can take any number of arguments, but can only have one expression.


4 Answers

Why do you need to state both 'x' and 'y' before the ':'?

Because a lambda is (conceptually) the same as a function, just written inline. Your example is equivalent to

def f(x, y) : return x + y

just without binding it to a name like f.

Also how do you make it return multiple arguments?

The same way like with a function. Preferably, you return a tuple:

lambda x, y: (x+y, x-y)

Or a list, or a class, or whatever.

The thing with self.entry_1.bind should be answered by Demosthenex.

like image 55
phipsgabler Avatar answered Oct 01 '22 01:10

phipsgabler


I believe bind always tries to send an event parameter. Try:

self.entry_1.bind("<Return>", lambda event: self.calculate(self.buttonOut_1.grid_info(), 1))

You accept the parameter and never use it.

like image 20
Demosthenex Avatar answered Oct 01 '22 00:10

Demosthenex


Why do you need to state both x and y before the :?

Because it's a function definition and it needs to know what parameters the function accepts, and in what order. It can't just look at the expression and use the variables names in that, because some of those names you might want to use existing local or global variable values for, and even if it did that, it wouldn't know what order it should expect to get them.

Your error message means that Tk is calling your lambda with one argument, while your lambda is written to accept no arguments. If you don't need the argument, just accept one and don't use it. (Demosthenex has the code, I would have posted it but was beaten to it.)

like image 37
kindall Avatar answered Oct 01 '22 00:10

kindall


Why do you need to state both 'x' and 'y' before the ':'?

You could actually in some situations(when you have only one argument) do not put the x and y before ":".

>>> flist = []
>>> for i in range(3):
...     flist.append(lambda : i)

but the i in the lambda will be bound by name, so,

>>> flist[0]()
2
>>> flist[2]()
2
>>>

different from what you may want.

like image 26
AlbertoAndreotti Avatar answered Oct 01 '22 00:10

AlbertoAndreotti