Securing a future for anonymous functions in Python

J

Jeff Shannon

Jacek said:
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. :)
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
 
D

Donn Cave

Jeff Shannon said:
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. :)

I'm with him. List incomprehensions do not parse well in
my eyes. I am reduced to guessing what they mean by a kind
of process of elimination. map is simply a function, so
it doesn't pose any extra reading problem, and while lambda
is awkward it isn't syntactically all that novel.

Donn Cave, (e-mail address removed)
 
J

Jacek Generowicz

Jeff Shannon said:
I guess we'll have to agree to disagree

Indeed :)
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
From the sounds of it, you may have the opposite experience with
reading map/lambda vs. reading list comps

No, I'm perefectly happy with both. I'm just trying to understand the
underlying reasons for people having trouble with one or the other, in
order to be better armed when the didactic need might arise.

One more question. Imagine that Python had something akin to Smalltalk
code blocks. Would something like

map([x | x+1], seq)

be any better for you than

map(lambda x:x+1, seq)

?
 
D

Donn Cave

Are you familiar with the Haskell syntax for list comprehensions?

For example:

http://www.zvon.org/other/haskell/Outputsyntax/listQcomprehension_reference.h

I haven't used it more than once or twice in the modest
amount of Haskell code I've written, but I've seen it
a few times.
Does their striking similarity to mathematical set notation help at
all ?

Not a bit.

If it's any more obvious than the Python version, I suppose
it's the | -- my parser sees [a|b] on the first pass.

But it isn't like I ever made any real effort to get comfortable
with Python list comprehensions. I was just relaying my (lack of)
intuitive grasp of them, compared to map and lambda.

Donn Cave, (e-mail address removed)
 
J

Jeff Shannon

Jacek said:
One more question. Imagine that Python had something akin to Smalltalk
code blocks. Would something like

map([x | x+1], seq)

be any better for you than

map(lambda x:x+1, seq)

?

I'd say that this is very slightly better, but it's much closer (in my
mind) to map/lambda than it is to a list comprehension. In this case,
at least the code block is visually self-contained in a way that
lambdas are not, but I still have to do more mental work to visualize
the overall results than I need with list comps.

Jeff Shannon
Technician/Programmer
Credit International
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top