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


Groups > comp.lang.python > #82168 > unrolled thread

Re: Question on lambdas

Started byMRAB <python@mrabarnett.plus.com>
First post2014-12-09 01:40 +0000
Last post2014-12-09 01:40 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Question on lambdas MRAB <python@mrabarnett.plus.com> - 2014-12-09 01:40 +0000

#82168 — Re: Question on lambdas

FromMRAB <python@mrabarnett.plus.com>
Date2014-12-09 01:40 +0000
SubjectRe: Question on lambdas
Message-ID<mailman.16752.1418089246.18130.python-list@python.org>
On 2014-12-08 23:58, Ben Finney wrote:
> memilanuk <memilanuk@gmail.com> writes:
>
>> What I'm having trouble finding a concrete answer to is the difference
>> between:
>
> (Note that where you write “some_func” the syntax requires an
> expression, not a function. I've changed your examples to be clear).
>
>> 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’.
>
>> 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’.
>
This is useful when you want to 'capture' the current value of
'some_value' to be passed to some_expr; if you didn't, then some_expr
would be using the value of 'some_value' at the time it was called,
which might be different if some variable it used had been changed.

>> Any help would be greatly appreciated.
>
> Hope that helps.
>
> It's best to remember that ‘lambda’ is syntactic sugar for creating a
> function; the things it creates are not special in any way, they are
> normal functions, not “lambdas”.
>
> What is different is that the function starts with no name bound to it,
> and the syntax allows only a single expression as the body of the
> function.
>

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web