Thursday, April 10, 2008

Python stack inspection

Sometimes function curious to know who is made the call.

1 #!/usr/bin/python
2
3 import inspect
4
5 def f1():
6 stack = inspect.stack()
7 for level in stack:
8 print level[0].f_code.co_name
9
10 def f2():
11 f1()
12
13 def f3():
14 f2()
15
16 def f4():
17 f3()
18
19 def f5():
20 f4()
21
22 f5()


The output would be something like this:

f1
f2
f3
f4
f5
...

No comments: