[Python-Dev] PEP 8: Discourage named lambdas? (original) (raw)

Steven D'Aprano steve at pearwood.info
Sun May 4 11:39:48 CEST 2008


On Sun, 4 May 2008 01:24:54 am Alex Martelli wrote:

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",

In fairness, up until a few years ago, Bucket=dict wouldn't have worked. I'm sure there's a lot of Python programmers who learnt the language with version < 2.0 who still have blind-spots when it comes to types. I know I do (but I'm getting better with practice).

Besides, would that be any better written as this?

def Bucket(): return {}

I think not. This is not a "named lambda" problem, this is a "developer doesn't know how to use types" problem.

While we're discussing named lambdas, and at the risk of dragging this thread out even longer, here's one example where I would use one:

def parrot(args, transformation=None): if transformation is None: # Use an identity function. transformation = lambda x: x
for arg in args: do_something_with(transformation(arg))

Does anybody have any constructive comments to make about this usage?

-- Steven D'Aprano



More information about the Python-Dev mailing list