Thursday, 15 April 2010

python - How to determine file, function and line number? -



python - How to determine file, function and line number? -

in c++, can print debug output this:

printf( "file: %s, func: %s, line: %d, log: %s\n", __file__, __function__, __line__, logmessage );

how can similar in python?

there module named inspect provides these information.

example usage:

import inspect def printframe(): callerframerecord = inspect.stack()[1] # 0 represents line # 1 represents line @ caller frame = callerframerecord[0] info = inspect.getframeinfo(frame) print info.filename # __file__ -> test.py print info.function # __function__ -> main print info.lineno # __line__ -> 13 def main(): printframe() # line main()

however, please remember there easier way obtain name of executing file:

print __file__

python

No comments:

Post a Comment