One of the ways to show trace is simply to show which methods/functions are being called. Being lazy, I simply want to cut and paste something, I don't want to also have to change it. I would prefer to use the actual function name rather than hard coding into a string for printing / logging. The name might change in the future. I might forget to alter the name after pasting it.
This is what I don't want:
def testStepA(param):
writeToLogFile('Reached function 'testStepA')
...
def testStepB(param):
writeToLogFile('Reached function 'testStepB')
...
I want something like this instead:
def testStepA(param):
writeToLogFile('Reached function '%s', nameOfThisFunction())
...
def testStepB(param):
writeToLogFile('Reached function '%s', nameOfThisFunction())
...
Fortunately, inspect gives me the information I'm after:
import inspect
def nameOfThisFunction (offset=0):
return inspect.stack()[1 + offset][3]
No comments:
Post a Comment