Finding the name of a function while defining it

Roy Smith roy at panix.com
Thu Dec 27 10:09:01 EST 2012


In article <50dc29e9$0$29967$c3e8da3$5496439d at news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:

> On Wed, 26 Dec 2012 23:46:31 -0800, Abhas Bhattacharya wrote:
> 
> >> > two = lamba : "one"
> >> > one = two
> >> 
> >> > Which one of these is the "name" of the function?
> [...]
> > If i call one() and two() respectively, i would like to see "one" and
> > "two".
> 
> I'm afraid you're going to be disappointed. There is no possible way for 
> one() and two() as shown above to report different names, because they 
> are the same function object.

Well, there is the (yes, I know it's absurd) sledgehammer-and-peanut way 
of getting a stack trace, finding the frame that called your function, 
and parsing the text of that line.

Never tell a hacker, "no possible way" :-)



More information about the Python-list mailing list