Why anonymity? [was Re: map/filter/reduce/lambda opinions and background unscientific mini-survey]

Steven D'Aprano steve at REMOVEMEcyber.com.au
Thu Jul 7 04:07:03 EDT 2005


Steven Bethard wrote:

> If you're really afraid of two lines, write it as:
> 
>     def r(): randint(1, 100)
> 
> This is definitely a bad case for an anonymous function because it's not 
> anonymous!  You give it a name, r.

This is something I've never understood. Why is it bad 
form to assign an "anonymous function" (an object) to a 
name?

It isn't just that lambda _can_ create functions that 
aren't bound to any name. That I get. But why is it 
suppose to be wrong to bind such a function to a name?

Sure, if the lambda is so complicated that it becomes 
unreadable, the usage case is wrong and a def should be 
used instead. But I see nothing wrong with doing this:

func = lambda x: x**3 - 3*x**2

Why is it considered abuse of lambda to assign the 
functions to a name? Is it an abuse of lambda to do this?

D = {"one": lambda noun: noun,
     "two": lambda noun: noun + 's',
     "many": lambda noun: 'lots of ' + noun + 's' }

assert D["two"]("python") == "pythons"


-- 
Steven.




More information about the Python-list mailing list