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


Groups > comp.lang.python > #82159

Re: Question on lambdas

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Question on lambdas
Date 2014-12-09 10:58 +1100
References <m65d2m$3pe$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.16745.1418083098.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

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

-- 
 \       “Theology is the effort to explain the unknowable in terms of |
  `\                         the not worth knowing.” —Henry L. Mencken |
_o__)                                                                  |
Ben Finney

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


Thread

Re: Question on lambdas Ben Finney <ben+python@benfinney.id.au> - 2014-12-09 10:58 +1100
  Re: Question on lambdas Christoph Becker <cmbecker69@gmx.de> - 2014-12-09 02:44 +0100
    Re: Question on lambdas Steven D'Aprano <steve@pearwood.info> - 2014-12-09 06:38 +0000
  Re: Question on lambdas Chris Angelico <rosuav@gmail.com> - 2014-12-09 12:53 +1100
  Re: Question on lambdas Terry Reedy <tjreedy@udel.edu> - 2014-12-08 21:09 -0500
  Re: Question on lambdas Ben Finney <ben+python@benfinney.id.au> - 2014-12-09 16:23 +1100
    Re: Question on lambdas "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-09 13:43 +0100
  Re: Question on lambdas Rustom Mody <rustompmody@gmail.com> - 2014-12-08 22:44 -0800

csiph-web