print "the name of this function is " + "???"
Running the code above gives the following output
the name of this function is ???
I would like the output to be
the name of this function is cap
Is that possible ?
Yes, use the moduloe inspect to access the current stack frame:
frame = inspect.currentframe()
sys._getframe() would help you here:
>>> import sys
<frame object at 0x00B496D0>
>>> def f():
... global x
... x = sys._getframe()
<frame object at 0x00B15250>
[..., 'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', ...]
[...'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
So your function could be:
>>> import sys
>>> def cap():
... print 'function name is', sys._getframe().f_code.co_name
function name is cap
Just what I was looking for. In "Library Reference" heading
3.11.1 Types and members
I found the info about the method you described. I also made a little
function to print out not just the name of the function but also the
parameter list. Here it is
import sys, string
def cap(s, n):
print string.replace("".join([sys._getframe().f_code.co_name, \
repr(sys._getframe().f_code.co_varnames)]), "\'", "")
Running this yields the result
All the 'hello' stuff is in the aboutme() decorator code.
There is no code in the decorated functions themselves
doing anything to telling us the function name.
# The decorator
def thecall(f, *args, **kwargs):
# Gets to here during module load of each decorated function
def wrapper( *args, **kwargs):
# Our closure, executed when the decorated function is called
print "Hello\nthe name of this function is '%s'\n" \
return f(*args, **kwargs)
print "string '%s' is argument for function" % s
# Try these
You've now got three solutions. They'll work fine most of the time,
but can't be trusted in general. Binding a name to a function doesn't
change the name that these solutions return, and the name they return
may no longer be bound to said function. Just a warning.
>> Thanks Diez and Peter,
>> Just what I was looking for. In "Library Reference" heading
>> 3.11.1 Types and members
>> Running this yields the result
>> cap(s, n)
>You've now got three solutions. They'll work fine most of the time,
>but can't be trusted in general. Binding a name to a function doesn't
>change the name that these solutions return, and the name they return
>may no longer be bound to said function. Just a warning.
But the one buried in co_name seems to persist
(barring byte code munging in the decorator ;-)
>>> def fren(newname='newname'):
... def fren(f):
... f.__name__ = newname
... return f
... return fren
... def foo():pass
Could have done that manually, but just playing.
Ok, rebind foo and remove the old name, for grins
>>> baz = foo
>>> del foo
See what we've got
['__builtins__', '__doc__', '__name__', 'baz', 'fren']
Check name(s) ;-)
Local binding to the function object first:
<function bar at 0x02EEADF4>
Its outer name:
Its def name:
> Decorate any function with @aboutme(), which
> will print the function name each time the function is called.
> All the 'hello' stuff is in the aboutme() decorator code.
> There is no code in the decorated functions themselves
> doing anything to telling us the function name.
so you've moved a trivial print statement from the function itself into
a decorator, so you can add an extra line before the function instead
of inside it. wow.
print "the name of this function is " + "cap"
yes, I'm serious.
if your question had been "can a function figure out the name of the
function that called it", the answer would have been different.