Extending Python Syntax with @

John Roth newsgroups at jhrothjr.com
Thu Mar 11 09:37:08 EST 2004


"Cameron Laird" <claird at lairds.com> wrote in message
news:1050odofl644aac at corp.supernews.com...
> In article <2f9050pq22u53o7aqo9i8ebqj11vo9kilg at 4ax.com>,
> David MacQuigg  <dmq at gain.com> wrote:
> .
> .
> .
> >going to help me write some code, I won't have time to study it.  My
> >understanding of lambda functions is simply that they are a way to
> >squeeze functions into a tight space:
> >
> >list_of_funcs = [(lambda x: 2**x), (lambda x: 3**x), (lambda x: 4**x)]
> >
> >If you are not concerned about space, simply use normal defs:
> >
> >def f2(x): return 2**x
> >def f3(x): return 3**x
> >def f4(x): return 4**x
> >list_of_funcs = [f2, f3, f4]
> >
> >Is there any other reason in Python to use lambdas?
> .
> .
> .
> In fact, *that*'s not a reason.  Part of tribal lore--a true,
> documented part, by the way--is that Big Cheese Guido depre-
> cates lambdas.  He says they're a mistake, and people shouldn't
> be using 'em.  Whenever you feel like a lambda, define a named
> function; it forces the developer to come up with a name for
> the operation, and that's likely to make the code more read-
> able (I'm slightly abbreviating the argument here).

I tend to agree, but for slightly different reasons. Lambdas
are a means of in-lining a function definition. However, they
are so restricted that we constantly get suggestions for
"improving" them by adding more syntax.

Given the restrictions, I see the natural growth path as leading
to a callable instance or a bound method, not a module level
function. Module level functions are a distraction; usually you
want to interface with an object, and module level functions make
that very difficult.

The other reason to avoid lambdas is the DRY principle:
Don't Repeat Yourself. Most uses of lambdas I've seen
lead to duplication in anything larger than a toy program.

What I'd really like is for all of the instructional material
with lambdas to just magically vanish and be replaced by
instructional material that does whatever it is in proper
object oriented fashion, using bound methods for callbacks.
Relegate lambda to a sidebar.

John Roth


> -- 
>
> Cameron Laird <claird at phaseit.net>
> Business:  http://www.Phaseit.net





More information about the Python-list mailing list