Name of function within the function?
Darrell
news at dorb.com
Thu Jul 1 13:48:09 EDT 1999
import sys, string, os
def myDirExcept(howFarBack):
"""
Get the directory of the module running at "howfarBack "
in the stack frames
This value is compiled into the .pyc file
This can be a problem.
This name will change depending on where python was run from
when the pyc file was created
Unix can store a relative path
Windows stores an absolute path
"""
try:
raise ZeroDivisionError
except ZeroDivisionError:
f = sys.exc_info()[2].tb_frame
for i in range(howFarBack):
f = f.f_back
t = f.f_code
fname = t.co_filename
p = string.split(fname, os.sep)
fname = string.join(p[:-1], os.sep)
print t.co_name, fname
return fname
myDirExcept(0)
###################################
E:\ace\v2\code\comp>junk2.py
myDirExcept E:\ace\v2\code\comp
--
--Darrell
Dula, Debbie (EXCHANGE:RICH2:2C25) <violet at americasm01.nt.com> wrote in
message news:377B8C2F.80F44F9 at americasm01.nt.com...
> I've looked and looked, but can't find the answer to this ... I'm
> sure it's right in front of me ...
>
>
> I want the current function's name to be output in my error
> messages, but can't figure out how a function can get its own name
> (without already knowing it). For example,
>
> def f1():
> print "Error in function", f1.__name__
>
> is not really useful for what I need as I might as well print "Error
> in function f1". Is there any generic way I can get the current
> function's name?
>
> Thanks,
> Debbie
More information about the Python-list
mailing list