Python inspect module

16 August 2004   2 comments   Python

Mind That Age!

This blog post is 14 years old! Most likely, its content is outdated. Especially if it's technical.

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()[1][1]
    caller_method = inspect.stack()[1][3]
    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!

Comments

Jacob Lundqvist
Credits where its due ;)
I got it in my turtn from this article:

http://www.oreillynet.com/pub/wlg/5204
Rangler
Four score and seven minutes ago, I read a sweet artlice. Lol thanks

Your email will never ever be published


Related posts

Previous:
Film Music by Alfred Schnittke 15 August 2004
Next:
Changing the size of a textarea box 18 August 2004
Related by Keyword:
How do log ALL PostgreSQL SQL happening 20 July 2015
Speed test between django_mongokit and postgresql_psycopg2 09 March 2010
CSSViewer - new promising Firefox Extension 20 February 2006
Pretty print SQL script 06 August 2004
Related by Text:
Be very careful with your add_header in Nginx! You might make your site insecure 11 February 2018
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
I'm Prolog 01 May 2007