[Python-Dev] PEP 8: Discourage named lambdas?

Steven steve at pearwood.info
Sat May 3 02:04:37 CEST 2008


On Fri, 2 May 2008 19:03:55 -0400
"Terry Reedy" <tjreedy at udel.edu> wrote:

> Some people write
>     somename = lambda args: expression
> instead of the more obvious (to most people) and, dare I say, standard
>     def somename(args): return expression
[...]
> There are currently uses of named lambdas at least in urllib2.  This to me 
> is a bad example for new Python programmers.
> 
> What do our style mavens think?

Speaking as one of those "some people", my position is that functions
created with lambda are first-class objects the same as everything else
in Python, and a rule that says "You must not assign a lambda to a
name, ever" would be a terrible rule.

(And I don't do it to save three characters. I don't do it often, and I
can't exactly articulate why I do it, only that I do it when it feels
right. It's a style thing.)

However, I'm happy for "no named lambdas" to be a guideline or
recommendation. I'm even happy for a stronger prohibition to apply
to the standard library. I don't dislike named lambdas, but I don't
expect others to like them.



-- 
Steven D'Aprano


More information about the Python-Dev mailing list