Question on lambdas
memilanuk
memilanuk at gmail.com
Mon Dec 8 22:09:21 EST 2014
On 12/08/2014 03:58 PM, Ben Finney wrote:
> memilanuk <memilanuk at gmail.com> writes:
>
>> What I'm having trouble finding a concrete answer to is the difference
>> between:
>
>> lambda: some_expr
>
> This creates a new function which expects zero parameters. The function,
> when called, will return the value of ‘some_expr’.
>
>> lambda x: some_expr
>
> This creates a new function which expects one positional parameter named
> ‘x’. The function, when called, will return the value of ‘some_expr’.
so in the first example in my original post:
...
lambda: update_label2('A', 100)
would this work the same? It looks as though it'd be passing the same
two parameters to the same function...
lambda: 'A', 100: update_label2()
Also, in my second example:
class MyText(Text):
def __init__(self, master, **kw):
apply(Text.__init__, (self, master), kw)
self.bind("<Return>", lambda e: "break")
(from the page @
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm, if anyone
cares)
I'm kind of missing what 'e' is and where its defined and what it's
supposed to be passing to "break"...? The whole bind method for classes
and instances is a wee bit above my pay grade right about now; but I
happened to stumble upon this example when I went looking for more info
on lambda :/
>
>> lambda x=some_value: some_expr
>
> This creates a new function which expects one parameter named ‘x’, which
> parameter has a default value of ‘some_value’. The function, when
> called, will return the value of ‘some_expr’.
>
I was reading in 'Programming Python 4th ed' by Lutz and he talks about
something to do with default values vs. enclosing scopes... that
something like:
lambda x=x: some_expr
when evaluated inside a function loop to create buttons, etc., causes
'x' to be evaluated as the default value at the function creation time,
vs. when the function is actually called. Do I have that more or less
correct?
TIA,
Monte
--
Shiny! Let's be bad guys.
Reach me @ memilanuk (at) gmail dot com
More information about the Python-list
mailing list