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

Terry Reedy tjreedy at udel.edu
Mon Feb 25 15:42:51 EST 2008


"Steven D'Aprano" <steve at REMOVE-THIS-cybersource.com.au> wrote in message 
news:13s5kclk4maa584 at corp.supernews.com...
| 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)

Since functions are constants with respect to code attribute, you might as 
well condense that to

def parrot(x,y,z, func = lambda xyz: xyz+1):
   return func(x+y+z)

Then you can claim some actual space saving.

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

Good habit?
Don't mislead the newbies ;-?

tjr







More information about the Python-list mailing list