How to refer to the function object itself in the function per se?

Kay Schluehr kay.schluehr at gmx.net
Sat Mar 11 18:05:56 EST 2006


Sullivan WxPyQtKinter wrote:
> I am sorry but you misunderstood my idea.
> What I want is a generalized method to print out the function name, or
> refer to the name of a function. If I use f.__name__, I think I should
> just use print "f" to save my keyboard. What I expect is using a
> method, or attribute, or another function to get the name of a
> function.

Sorry, if the example was misleading. Here is a better one:

def f(g):
     print g.__name__

>>> f(f)
f

The __name__ attribute of f is constant and it doesn't matter whether
you pass f around or create aliases:

>>> x = f
>>> x.__name__
f

x refers still to the function object f. Therefore the __name__
attribute does not return "x" but "f". You will always print the right
function name not the name of the local variable.

Kay




More information about the Python-list mailing list