Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82158

Re: Question on lambdas

References <m65d2m$3pe$1@ger.gmane.org>
Date 2014-12-09 10:54 +1100
Subject Re: Question on lambdas
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16744.1418082867.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Dec 9, 2014 at 10:43 AM, memilanuk <memilanuk@gmail.com> wrote:
> What I'm having trouble finding a concrete answer to is the difference
> between:
>
> lambda: some_func
>
> lambda e: some_func

These two are quite simple. (In each case, it's an expression, not a
function, for what it's worth.) They're (roughly) equivalent to these
functions:

def anonymous():
    return some_func
def anonymous(e):
    return some_func

In other words, the second one takes an argument, the first doesn't.

> lambda e=e: some_func

I'm not sure what this one ought to be; do you have an example? If the
"e=" part comes before the "lambda", though, then it's simply a named
argument getting a lambda function bound to it. In your example above,
this:

Radiobutton(... command=lambda: update_label2('A', 100))

is equivalent to this:

def anonymous():
    return update_label2('A', 100)
RadioButton(... command=anonymous)

It's that simple.

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Question on lambdas Chris Angelico <rosuav@gmail.com> - 2014-12-09 10:54 +1100

csiph-web