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

Alex Martelli aleaxit at gmail.com
Sat May 3 17:24:54 CEST 2008


On Fri, May 2, 2008 at 11:32 PM, Mike Klaas <mike.klaas at gmail.com> wrote:
   ...
>  Sorry, that was a bad example.  It is obviously silly if the return value
> of the function is callable.

...and yet it's *exactly* what keeps happening to lambda-happy
programmers -- in production code as well as examples, and in
major/famous projects too.  E.g., a simple google code search shows
many Zope versions containing "Bucket=lambda:{}" instead of the
obvious "Bucket=dict", Chandler with an intricate
t = threading.Thread(target=lambda x=activePort:testcon(x),verbose=0)
instead of
t = threading.Thread(target=testcon, args=(activePort,), verbose=0)
SQLAlchemy with "callable_=lambda i: self.setup_loader(i)" instead of
"callable_=self.setup_loader" ... apparently the existence of lambda
may easily blind one to the fact that one can simply pass a callable.
I guess that's inevitable (given lambda's existence... and human
nature;-) and about on the same plane as another hatefully redundant
construct I find myself having to beat upon over and over in code
reviews:

if <expression>:
    result = True
else:
    result = False
return result

vs the simple "return <expression>" [[or bool(<expression>) if it's
actually mandatory to return a bool and <expression> can't be relied
upon to produce one]].


Alex


More information about the Python-Dev mailing list