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

Samuele Pedroni pedronis at openend.se
Sat May 3 11:32:38 CEST 2008


Terry Reedy 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
>
> The difference in the result (the only one I know of) is that the code and 
> function objects get the generic name '<lambda>' instead of the more 
> informative (in repr() output or tracebacks) 'somename'.  I consider this a 
> disadvantage.
>
> In the absence of any compensating advantages (other than the trivial 
> saving of 3 chars), I consider the def form to be the proper Python style 
> to the point I think it should be at least recommended for the stdlib in 
> the Programming Recommendations section of PEP 8.
>
> 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?
>   
I found only an example in my personal recent code:

START = "<!-- *db-payload-start* -->"
END = "<!-- *db-payload-end* -->"
TITLEPATTERN = lambda s: "<title>%s</title>" % s

this three are later used in a very few .find() and .replace() 
expressions in the same module. I suppose my point is that while I agree 
it should be discouraged and is really silly to do it for the few chars 
gain, it can be used to some effect in very rare cases.



More information about the Python-Dev mailing list