Find function name in function
Sean Ross
frobozz_electric at hotmail.com
Tue May 20 13:21:19 EDT 2003
Here's something that gets you a little bit closer to what you're looking
for, however, you will notice that it does not resolve the issue presented
in my previous posting. I.e., if you have two (or more) aliases to a
function and you call them in the same line of code, WhoMightIBe() cannot
determine which of the aliases was used, it can only tell you that the list
of possible aliases.
def WhoMightIBe():
"returns name and/or possible alias' of called calling function"
import inspect, re
stack = inspect.stack()
# get name of function as listed in def statement
defname = eval(stack[1][-3])
# get the source line where function is called
srcline = stack[2][-2][0].strip()
# extract function names from source line (robust? ...doubtful)
pattern = re.compile(r'[a-z,A-Z,0-9,_]+\(')
matches = [txt.replace('(','') for txt in pattern.findall(srcline)]
# see which function name(s) alias(es) our target function
possibles = [funcname for funcname in matches if eval(funcname) is
defname]
return possibles
def XXX():
print WhoMightIBe()
XXX()
yy=XXX
a= yy()
zz=XXX
zz()
print "%s %s"%(yy(), zz())
#outputs:
['XXX']
['yy']
['zz']
['yy', 'zz']
['yy', 'zz']
None None
More information about the Python-list
mailing list