get the name of lambda

Terry Reedy tjreedy at udel.edu
Sat Oct 16 11:15:15 EDT 2004


"George Yoshida" <ml at dynkin.com> wrote in message 
news:ckr59p$1epo$1 at dojima-n0.hi-ho.ne.jp...
> Just out of curiosity, is there any way to get the name of a
> lambda expression? Lambdas are anonymous functions, so it's a
> stupid idea to get the name of it. But if it's possible, how
> could I?
>
> Let's take a simple example:
>  foo = lambda:None
>
> foo.__name__(or foo.func_name) returns '<lambda>'.
> I'm looking for a way to return 'foo', instead of '<lambda>'.

Use 'def foo: return None' instead.  This is precisely why a 'name = lambda 
...' statement is inferior to a def statement.

[and in followup]
> I implemented a function in several ways(using recursion,
> lambda, etc) and benchmarked them. In that script, I needed
> to display function names, along with ellapsed time.

So use a def instead of lambda.  Only use lambda when you have an 
expression to return and *do not need* a name to display.  Python is not 
Lisp ;-)

Terry J. Reedy







More information about the Python-list mailing list