Using lambda [was Re: Article of interest: Python pros/cons for theenterprise]

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Feb 25 09:32:53 EST 2008


On Sun, 24 Feb 2008 21:13:08 -0500, Terry Reedy wrote:

> | I even use "named anonymous functions" *cough* by assigning lambda |
> functions to names:
> |
> | foo = lambda x: x+1
> 
> Even though I consider the above to be clearly inferior to
> 
> def foo(x): return x+1
> 
> since the latter names the function 'foo' instead of the generic
> '<lambda>'.

Absolutely. If foo() was a function that the user would see, I would 
certainly use the def form to create it.

But in a situation like this:


def parrot(x, y, z, func=None):
    if func is None:
        func = lambda x: x+1
    return func(x+y+z)


I don't see any advantage to writing it as:

def parrot(x, y, z, func=None):
    if func is None:
        def func(x): return x+1
    return func(x+y+z)



-- 
Steven



More information about the Python-list mailing list