lambda ??

Discussion in 'Python' started by Michael Foord, Aug 27, 2004.

  1. I'm starting to read through the developer works article on Functional
    Programming - http://www-106.ibm.com/developerworks/library/l-prog.html

    It starts by giving some basic examples using lambda. What I'm
    wondering is what's the actual difference between these two forms ?

    pr = lambda s:s
    *and*
    def pr(s):
    return s

    Both bind the name 'pr' to a function object that does the same thing
    ?? I know that lambda functions can only be a single expression......

    Is it just a basic example (and so in this case there is no
    difference).. or am I missing something. (What's the point of an
    'anonymous' function... if you give a name to it !!).

    Regards,

    Fuzzy

    http://www.voidspace.org.uk/atlatnibots/pythonutils.html
    Michael Foord, Aug 27, 2004
    #1
    1. Advertising

  2. lambda returns the functions, thus can be used *in* an expression or
    function call, when a callable is required. This is often a useful shortcut.
    But according to another thread 'lambda' is one candidate for things
    that are dropped in a future version of Python...

    Michael Foord wrote:
    > I'm starting to read through the developer works article on Functional
    > Programming - http://www-106.ibm.com/developerworks/library/l-prog.html
    >
    > It starts by giving some basic examples using lambda. What I'm
    > wondering is what's the actual difference between these two forms ?
    >
    > pr = lambda s:s
    > *and*
    > def pr(s):
    > return s
    >
    > Both bind the name 'pr' to a function object that does the same thing
    > ?? I know that lambda functions can only be a single expression......
    >
    > Is it just a basic example (and so in this case there is no
    > difference).. or am I missing something. (What's the point of an
    > 'anonymous' function... if you give a name to it !!).
    >
    > Regards,
    >
    > Fuzzy
    >
    > http://www.voidspace.org.uk/atlatnibots/pythonutils.html
    Benjamin Niemann, Aug 27, 2004
    #2
    1. Advertising

  3. Michael Foord

    Paul Rubin Guest

    (Michael Foord) writes:

    > It starts by giving some basic examples using lambda. What I'm
    > wondering is what's the actual difference between these two forms ?
    >
    > pr = lambda s:s
    > *and*
    > def pr(s):
    > return s


    They're the same.

    > Is it just a basic example (and so in this case there is no
    > difference).. or am I missing something. (What's the point of an
    > 'anonymous' function... if you give a name to it !!).


    It's like an anonymous expression. Look at the statement

    x = a + b * c

    That adds the expression 'a' to the expression 'b * c'. If Python
    didn't have anonymous expressions, you'd say something like

    temp = b * c
    x = a + temp

    Anonymous just means you can use it as an intermediate result without
    having to give it a name of its own.

    Example:

    def derivative(f, x): # find approximate value of f'(x)
    h = .0001
    return (f(x+h) - f(x)) / h

    def square(x):
    return x*x

    print derivative(square, 3) # approximately 6

    An anonymous function lets you do the same thing without having to
    create a named function (like a temporary variable):

    print derivative(lambda x: x*x, 3) # same thing

    Using a lot of lambdas can be like using a lot of complicated, deeply
    nested arithmetic expressions. You have to exercise some judgement to
    keep your code readable. But there's a school of thought that says
    lambda is a wart in Python and shouldn't be used. That's as silly as
    saying you should never say "a + b * c" and instead name every
    subexpression with a temp variable.
    Paul Rubin, Aug 27, 2004
    #3
  4. Michael Foord

    Jan Gregor Guest

    Python is little bit limited in lambda functions. In lisp I use lambda
    functions in functionals (something like a map or reduce in python)
    as a wrapper for functions with more than one argument.

    Jan

    On 2004-08-27, Michael Foord <> wrote:
    > I'm starting to read through the developer works article on Functional
    > Programming - http://www-106.ibm.com/developerworks/library/l-prog.html
    >
    > It starts by giving some basic examples using lambda. What I'm
    > wondering is what's the actual difference between these two forms ?
    >
    > pr = lambda s:s
    > *and*
    > def pr(s):
    > return s
    >
    > Both bind the name 'pr' to a function object that does the same thing
    > ?? I know that lambda functions can only be a single expression......
    >
    > Is it just a basic example (and so in this case there is no
    > difference).. or am I missing something. (What's the point of an
    > 'anonymous' function... if you give a name to it !!).
    >
    > Regards,
    >
    > Fuzzy
    >
    > http://www.voidspace.org.uk/atlatnibots/pythonutils.html
    Jan Gregor, Aug 27, 2004
    #4
  5. Jan Gregor <> wrote in message news:<cgmtp7$f6v$>...
    > Python is little bit limited in lambda functions. In lisp I use lambda
    > functions in functionals (something like a map or reduce in python)
    > as a wrapper for functions with more than one argument.
    >
    > Jan


    Thanks to those that answered. Looks like I need to elarn Lisp to
    getter a fuller understanding... right after I learn C.

    Hmmm... unfortunately it looks like Python 2.2 or 2.3 broke the
    'closure' examples from the Charming Python 'Functional Programming'
    series. Looks like the Xoltar toolkit could do with an update. The
    article says that the nested scope rules of python 2.1 + mean that you
    don't need the toolkit to do closures.... but without the examples
    it's a little harder to follow. Maybe it's time for an update !!
    Anyway - I did get some interesting ideas.

    Regards,

    Fuzzy


    > [snip...]


    http://www.voidspace.org.uk/atlantibots/pythonutils.html
    Michael Foord, Aug 28, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee

    A Lambda Logo Tour

    Xah Lee, Apr 5, 2006, in forum: Java
    Replies:
    5
    Views:
    516
    Alex Hunsley
    Apr 7, 2006
  2. Xah Lee
    Replies:
    177
    Views:
    3,057
    Kay Schluehr
    May 23, 2006
  3. Roman Suzi
    Replies:
    13
    Views:
    606
    Bengt Richter
    Jan 7, 2005
  4. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    175
    Sean O'Halpin
    Mar 30, 2009
  5. Haochen Xie
    Replies:
    4
    Views:
    242
    Haochen Xie
    Mar 17, 2013
Loading...

Share This Page