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


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

Re: Question on lambdas

Started byBen Finney <ben+python@benfinney.id.au>
First post2014-12-09 10:58 +1100
Last post2014-12-08 22:44 -0800
Articles 8 — 7 participants

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

#82159 — Re: Question on lambdas

FromBen Finney <ben+python@benfinney.id.au>
Date2014-12-09 10:58 +1100
SubjectRe: Question on lambdas
Message-ID<mailman.16745.1418083098.18130.python-list@python.org>
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

[toc] | [next] | [standalone]


#82169

FromChristoph Becker <cmbecker69@gmx.de>
Date2014-12-09 02:44 +0100
Message-ID<mailman.16753.1418089457.18130.python-list@python.org>
In reply to#82159
Ben Finney wrote:

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

Could you please elaborate why ‘lambda’ does not create “lambdas”.  I'm
a Python beginner (not new to programming, though), and rather confused
about your statement.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#82179

FromSteven D'Aprano <steve@pearwood.info>
Date2014-12-09 06:38 +0000
Message-ID<548698ca$0$2817$c3e8da3$76491128@news.astraweb.com>
In reply to#82169
On Tue, 09 Dec 2014 02:44:12 +0100, Christoph Becker wrote:

> Ben Finney wrote:
> 
>> 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”.
> 
> Could you please elaborate why ‘lambda’ does not create “lambdas”.  I'm
> a Python beginner (not new to programming, though), and rather confused
> about your statement.


Compare these:

py> def f1():
...     return 23
... 
py> f2 = lambda: 23
py> type(f1) is type(f2)
True


Functions created with lambda are exactly the same type of object as 
functions created with def.

Functions created with lambda accept exactly the same arguments as 
functions created with def. They differences are:

- functions created with lambda all have the same internal name;
- functions created with def have the name you give them;

- functions created with lambda are expressions and can be embedded
  directly in function calls, lists, etc;
- functions created with def are statements and must stand alone.




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#82170

FromChris Angelico <rosuav@gmail.com>
Date2014-12-09 12:53 +1100
Message-ID<mailman.16754.1418089988.18130.python-list@python.org>
In reply to#82159
On Tue, Dec 9, 2014 at 12:44 PM, Christoph Becker <cmbecker69@gmx.de> wrote:
> Ben Finney wrote:
>
>> 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”.
>
> Could you please elaborate why ‘lambda’ does not create “lambdas”.  I'm
> a Python beginner (not new to programming, though), and rather confused
> about your statement.

For the same reason that "def" doesn't create "defs", and "for"
doesn't create fors (uhh... the Force?). Both "lambda" and "def"
create functions. Functions are things that can be called, and they're
created by the "def" statement, the "lambda" expression, and very VERY
occasionally, by directly calling the function constructor. You could
distinguish between "lambda functions" and "def functions" if you
like, but the distinction is almost never significant. In fact,
usually you don't even need to distinguish between functions and other
callables (types, objects with __call__ methods, bound method objects,
etc, etc, etc).

ChrisA

[toc] | [prev] | [next] | [standalone]


#82171

FromTerry Reedy <tjreedy@udel.edu>
Date2014-12-08 21:09 -0500
Message-ID<mailman.16755.1418091001.18130.python-list@python.org>
In reply to#82159
On 12/8/2014 8:53 PM, Chris Angelico wrote:
> On Tue, Dec 9, 2014 at 12:44 PM, Christoph Becker <cmbecker69@gmx.de> wrote:
>> Ben Finney wrote:
>>
>>> 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”.
>>
>> Could you please elaborate why ‘lambda’ does not create “lambdas”.  I'm
>> a Python beginner (not new to programming, though), and rather confused
>> about your statement.
>
> For the same reason that "def" doesn't create "defs", and "for"
> doesn't create fors (uhh... the Force?). Both "lambda" and "def"
> create functions. Functions are things that can be called, and they're
> created by the "def" statement, the "lambda" expression, and very VERY
> occasionally, by directly calling the function constructor. You could
> distinguish between "lambda functions" and "def functions" if you
> like, but the distinction is almost never significant. In fact,
> usually you don't even need to distinguish between functions and other
> callables (types, objects with __call__ methods, bound method objects,
> etc, etc, etc).

To exemplify Chris's answer: Consider

 >>> def f1(a, b='hi'): return a+b

 >>> f2 = lambda a, b='hi': a+b
 >>>

I believe the only difference between any of the attributes of f1 and f2 is

 >>> f1.__name__
'f1'
 >>> f2.__name__
'<lambda>'
 >>> f1.__code__.co_name
'f1'
 >>> f2.__code__.co_name
'<lambda>'

All other values of corresponding attributes of f1 and f2, and 
f1.__code__ and f2.__code__, should be the same.  For instance

 >>> f1.__code__.co_varnames
('a', 'b')
 >>> f2.__code__.co_varnames
('a', 'b')

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#82176

FromBen Finney <ben+python@benfinney.id.au>
Date2014-12-09 16:23 +1100
Message-ID<mailman.16759.1418102596.18130.python-list@python.org>
In reply to#82159
Christoph Becker <cmbecker69@gmx.de> writes:

> Ben Finney wrote:
>
> > 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”.
>
> Could you please elaborate why ‘lambda’ does not create “lambdas”. I'm
> a Python beginner (not new to programming, though), and rather
> confused about your statement.

We already have a term for what the ‘lambda’ keyword creates: a
function.

That is, ‘lambda’ creates a function object, without anything to
distinguish it from a function created any other way.

-- 
 \     “Here is a test to see if your mission on earth is finished. If |
  `\                          you are alive, it isn't.” —Francis Bacon |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#82195

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-12-09 13:43 +0100
Message-ID<m66qor$3g0$1@solani.org>
In reply to#82176
Ben Finney wrote:

> Christoph Becker <cmbecker69@gmx.de> writes:
> 
>> Ben Finney wrote:
>>
>>> 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”.
>>
>> Could you please elaborate why ‘lambda’ does not create “lambdas”. I'm
>> a Python beginner (not new to programming, though), and rather
>> confused about your statement.
> 
> We already have a term for what the ‘lambda’ keyword creates: a
> function.
>
> That is, ‘lambda’ creates a function object, without anything to
> distinguish it from a function created any other way.

Ah, now I understand.  It's "just" about proper naming.  Thanks. :)

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#82181

FromRustom Mody <rustompmody@gmail.com>
Date2014-12-08 22:44 -0800
Message-ID<142319f9-ea78-402c-85cd-cc637dca7183@googlegroups.com>
In reply to#82159
On Tuesday, December 9, 2014 5:28:49 AM UTC+5:30, Ben Finney wrote:
> memilanuk 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”.

This is a good thing to remember (and better than most of the λ-calculus memes
floating around the net).

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

Cant quarrel with this as a fact but its the wrong emphasis IMHO:
Its not that lambdas are functions without names -- so called `anonymous' functions.
Its rather that defs are functions with names, ie a def bundles up two independent activities:
naming and function-making.

Do we call constants 'anonymous variables'?
One could of course but its misleading because constants come before variables

Most people know this in the form: "Arithmetic comes before algebra"
A CS buzz version: Functional programming is simpler than imperative programming.
However I find this unhelpful and misleading for the same reason that 'λ is 
an anonymous function' is: If functional programming were 'programming with functions' then
C would be a functional language <wink>

Here is our attempt at explaining this in more detail in 'modern' format
https://www.youtube.com/watch?v=FZoq-GlOdSs

The above is all with 'conceptual-fundamentals' hat firmly on.

From the pragmatic pov, the need to mix naming with function-making is
so overwhelmingly what is needed, that rarely/never using lambdas is ok.

JFTR Miranda one of the seminal functional languages, had no lambdas, only
(the equivalent of) defs.
Put differently, Ive no issue with Guido wanting to remove lambda from python
[In any case in the unicode age I'd prefer it spelt as λ]

Ive an issue with people claiming to have a degree in CS not knowing (basics of)
λ-calculus

[toc] | [prev] | [standalone]


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


csiph-web