Finding the name of a function while defining it

Chris Angelico rosuav at
Thu Dec 27 06:44:36 CET 2012

On Thu, Dec 27, 2012 at 3:52 PM, Tim Roberts <timr at> wrote:
> The
> compiled code in a function, for example, exists as an object without a
> name.  That unnamed object can be bound to one or more function names, but
> the code doesn't know that.  Example:
> def one():
>     print( "Here's one" )
> two = one
> That creates one function object, bound to two names.  What name would you
> expect to grab inside the function?

Presumably 'one'.

> Even more obscure:
> two = lamba : "one"
> one = two
> Which one of these is the "name" of the function?

I would say '<lambda>'. Whatever method is used to get the function's
name, I would expect it to match the __name__ attribute of the
function (which is a peer to __code__, but I don't think the
function's code *is* the function).


More information about the Python-list mailing list