Finding the name of a function while defining it

Abhas Bhattacharya abhasbhattacharya2 at gmail.com
Thu Dec 27 02:46:31 EST 2012


On Thursday, 27 December 2012 11:14:36 UTC+5:30, Chris Angelico  wrote:
> On Thu, Dec 27, 2012 at 3:52 PM, Tim Roberts <timr at probo.com> 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).
> 
> 
> 
> ChrisA

If i call one() and two() respectively, i would like to see "one" and "two".



More information about the Python-list mailing list