get the name of lambda

Michael Hoffman m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Sat Oct 16 09:18:09 EDT 2004


George Yoshida wrote:

> 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>'.

But you already know that it is called 'foo'! :)

Anyway I don't think it has anything to do with lambda:

 >>> def x(): pass
...
 >>> y = x
 >>> y.__name__
'x'

IIRC, in 2.4 you can set the func_name attribute so you could set up a 
decorator to set the name for you. Or if you want to be fancy you could 
even set up a metaclass that automatically renames all the lambda 
functions it finds in a class.

But why?
-- 
Michael Hoffman



More information about the Python-list mailing list