132

After reading everything I can find on lambda expressions in Python, 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)

1
  • "Also how do you make it return multiple arguments?" This does not make sense, because "argument" is not what you call something that gets returned. Commented Mar 10, 2023 at 7:00

4 Answers 4

184

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.

Sign up to request clarification or add additional context in comments.

4 Comments

To really get the least bit out of them, you might try some functional programming, which is an awesome experience when you begin to understand it, and probably will make you a better programmer.</propaganda>
You are my god and your propaganda is my daily bread. Yet, I tried to it apply on a tuple : (train["pred_idx_cos"],train["target"]).apply(lambda x,y: get_result(x, y)) and it seems it doesn't work. Please, save my soul from the mist of procedural programing
@RevolucionforMonica A bit of a late reply, but here's a Python 3 REPL code example of how to do it: repl.it/@foobar123/ScarceWhimsicalMainframe
Probably good for someone asking this type of question, to explain that even a normal function, when returning multiple variables with e.g. return a, b, c, actually just returns a tuple (a, b, c). And the typical statement using a function a, b, c = fun(x,y) just unpacks the tuple that was returned.
14

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.

1 Comment

You can also try "event=None" to give it a default value, then the function can be used for bind and the button.
7

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.)

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.