inspect: get the calling command

Hans Georg Krauthaeuser hgk at et.uni-magdeburg.de
Mon Jun 14 09:13:39 EDT 2004


Dear all,

I have a problem to get the command that has called a function if the 
command was given on multiple lines. E.g.:

###################################################
import inspect

def call(a,b,c,d,e):
     s = inspector()
     return s

def inspector():
     frame = inspect.currentframe()
     outerframes = inspect.getouterframes(frame)
     cmd = ''
     for c in outerframes[2][4]:
         cmd += c
     return cmd

if __name__ == '__main__':
     s1 = call (1,2,3,4,5)
     s2 = call (1,\
                2,\
                3,\
                4,\
                5)
     print '-'*10
     print s1
     print '-'*10
     print s2
     print '-'*10
###################################################

This gives the output:

----------
     s1 = call (1,2,3,4,5)

----------
                5)

----------

I'm quite new to python and the standard libs. So forgive me, if this is 
a stupid question.

Any help is very much appreciated.

Best regards
Hans Georg



More information about the Python-list mailing list