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

Ron Adam rrr at ronadam.com
Thu Jul 7 13:05:42 EDT 2005


Steven D'Aprano wrote:

> On Thu, 07 Jul 2005 09:36:24 +0000, Duncan Booth wrote:
> 
> 
>>Steven D'Aprano wrote:
>>
>>>This is something I've never understood. Why is it bad 
>>>form to assign an "anonymous function" (an object) to a 
>>>name?
>>
>>Because it obfuscates your code for no benefit. You should avoid making it 
>>hard for others to read your code (and 'others' includes yourself in the 
>>future).

Use a descriptive name like this?

def get_the_cube_of_x_and_then_subtract_five_multiplied_by_x(x):
      x**3 - 5*x

I think I like the lambda version here.  ;-)

It would probably have a name which refers to the context in which it's 
used, but sometimes the math expression it self is also the most readable.



> Put it this way: whenever I see a two-line def as above, I can't help
> feeling that it is a waste of a def. ("Somebody went to all the trouble
> to define a function for *that*?") Yet I would never think the same about
> a lambda -- lambdas just feel like they should be light-weight. 

In the case of an interface module you might have a lot of two like 
def's that simply change the name and argument format so several modules 
can use it and have a standard consistent or simplified interface.

The lambda may be perfectly fine for that.  But why not use def?

func_x = lambda x: (someother_func_x(x,'value'))

def func_x(x): return someother_func_x(x,'value')

There's both nearly identical, but the def is understandable to 
beginners and advanced python programs.


Cheers,
Ron


> Am I just weird?

Aren't we all?   ;-)




More information about the Python-list mailing list