Python inspect module
16 August 2004
My friend Jacob Lundqvist of Galdrion today showed me a nifty little method I did not know about in Python, namely the
inspect module. It allows you to find out what the name of the method was that called the method "you're in". Allow me to show an example:
import inspect def foo(): caller_module = inspect.stack() caller_method = inspect.stack() print caller_module, caller_method return "Something" def bar(): foo() if __name__=='__main__': bar()
And the result is:
>>> C:\Python23\dummy.py bar
I now use this in an internal debug method that prints all SQL statements used before being executed. Every method that needs to execute some SQL will always first send it's SQL statement to the debug method. The debug method can now also show where the SQL came from. Great!