a little trap revealed (was Re: Let's Talk About Lambda Functions!)

Andreas Kostyrka andreas at kostyrka.priv.at
Tue Jul 30 05:34:13 EDT 2002


Am Sam, 2002-07-27 um 09.11 schrieb Alex Martelli:
> The lambda solution doesn't work.  At all.  It gives you a SyntaxError
> if you try it!
Well, I usually type sys.stdout.write in a lambda without even thinking
to much :)
> 
> lambda cannot include a statement.  print is a statement.  Oooops...!-)
> 
> So, if you structure your code with lambda, the moment you want
> to add a print statement, boom -- can't do.  You have to restructure
Why would I want to add a print statement for debugging. (Admittingly
I've done this before, but these days are long gone)
With python it's trivial to define an automatic instrumentation with the
tracing support. Actually, print statements are used usually by people
to lazy to do it right. Usually, when you get to your first hard problem
in a small piece of old code (say 25kloc, deployment started in 1996,
and development never stopped) where you need much instrumentation to
get a feel how certain things are done. (Well the app contains at least
3 seperate layers of db abstraction layers, each one on top of the older
one ;) )

> your code to use def instead.  When you remove the print after it's
> done its debugging-help job, will you restructure once again to
> take the def away in favor of lambda... and again NEXT time you
> need to have a statement there too, etc, etc...?  What a lot of
> purposeless code churning...!
Again, why would I do that?
lambda x: x
->
lambda x: (x,sys.stdout.write("x=%d\n" % x))[0])

> Use def and avoid all of this silly self-imposed useless work.
Use the best tool for the task. lambda are quite nice for small wrapper
functions that rearrange arguments.
Basically, lambda can sometimes substitute for the code block objects
(like in smalltalk).
So IMHO, there is nothing wrong with lambda. Just don't use a fork to
eat soup. At least do not complain about the experience to c.l.p ;)

Andreas





More information about the Python-list mailing list