Securing a future for anonymous functions in Python

Jeff Shannon jeff at ccvcorp.com
Tue Jan 11 15:41:44 EST 2005


Jacek Generowicz wrote:

> Given a population with previous exposure to computer programming, my
> money is on the map-lambda version. But this last point is mostly
> irrelevant. The fact is that you cannot program computers without
> doing a bit of learning ... and the lambda, map and friends really do
> not take any significant learning.

I guess we'll have to agree to disagree, because given the same 
conditions, I *still* think that a list comprehension expresses its 
semantics more clearly than map/lambda.  I'd also point out that not 
all Python programmers will have significant prior exposure to 
programming ideas, and even those who do will not necessarily have 
prior exposure to lambdas.

It's true that programming requires learning, and that map/lambda 
aren't a tremendous burden to learn.  Still, to my mind they make a 
program a tiny increment more complicated.  (I find that reading a 
lambda requires mentally pushing a stack frame to parse the lambda and 
another to translate map() into a loop, whereas a list comp's 
expression doesn't require such a shift, and a function name works as 
a good placeholder that makes reading easier.)  It's not a big 
difference in any individual case, but incremental differences build up.

 From the sounds of it, you may have the opposite experience with 
reading map/lambda vs. reading list comps, though, so we could go back 
and forth on this all week without convincing the other. :)

>>Speaking for *this* laboratory rat, at least, map/lambda was always a
>>nasty puzzle for me and difficult to sort out.  But when list comps
>>were introduced, after reading just a sentence or two on how they
>>worked, they were completely clear and understandable -- much more so
>>than map/lambda after many months of exposure.
> 
> Forgetting about lambda, map, filter and reduce, do you find that you
> pass callables around in your Python programs, or is this not
> typically done in your programs?

Sure, I pass callables around quite a bit.  Usually they're GUI 
callbacks or the like.  Usually they're also either complex enough 
that lambda would be undesireable if not impossible, or they're simple 
and numerous (e.g. calling a function with different parameters) such 
that it's easy to write a factory function that returns closures 
rather than feed the parameter in with a lambda.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list