Question on lambdas

Ben Finney ben+python at benfinney.id.au
Tue Dec 9 00:30:00 EST 2014


memilanuk <memilanuk at gmail.com> writes:

> ...
> lambda: update_label2('A', 100)
>
> would this work the same?

(I don't know what you mean here by “the same”; the same as what?)

The above creates a new function, which expects no parameters (because
there are no parameters before its ‘:’).

The function, when called, will return the value of the expression
‘update_label2('A', 100)’.

> It looks as though it'd be passing the same two parameters to the same
> function...

Yes, it looks that way, and that's what it does. The parameters are
fixed in the expression and will be the same each time the new function
is called.

> lambda: 'A', 100: update_label2()

This is a syntax error.

Have you tried experimenting with these? Carefully read the reference
material on the ‘lambda’ syntax, and create some functions and
experiment with them.

-- 
 \     “Not to be absolutely certain is, I think, one of the essential |
  `\                         things in rationality.” —Bertrand Russell |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list