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

Duncan Booth duncan.booth at invalid.invalid
Sat Mar 11 15:58:18 EST 2006


Sullivan WxPyQtKinter wrote:
> So how
> could I refer to the function object per se, in the body of the
> function itself?
> 
> 
I don't believe you can easily get at the function object, but you can get 
at the code object which also has a name (which will be the same as the 
function's name unless you've been doing strange things):

>>> import inspect
>>> def f():
    print inspect.currentframe().f_code.co_name

    
>>> f()
f

For convenience you might want to put this inside a function:

>>> def myname():
    f = inspect.currentframe().f_back
    return f.f_code.co_name

>>> def f():
    print myname()

    
>>> f()
f




More information about the Python-list mailing list