import sys
def one():
two()
def two():
three()
def three():
for num in range(3):
frame = sys._getframe(num)
show_frame(num, frame)
def show_frame(num, frame):
print frame
print " frame = sys._getframe(%s)" % num
print " funname = %s()" % frame.f_code.co_name
print " file/line = %s:%s" % (frame.f_code.e, frame.f_lineno)
one()
Output
<frame object at 0x606c50>
#返回当前stack frame
frame = sys._getframe(0)
funname = three()
file/line = stack.py:12
<frame object at 0x180be10>
#返回当前stack 的上层stack frame
frame = sys._getframe(1)
funname = two()
file/line = stack.py:7
<frame object at 0x608d30>
frame = sys._getframe(2)
funname = one()
file/line = stack.py:4
sys._getframe([depth]) -> frameobject
Return a frame object from the call stack. If optional integer depth is
given, return the frame object that many calls below the top of the stack.
If that is deeper than the call stack, ValueError is raised. The default
for depth is zero, returning the frame at the top of the call stack.
frameobject 属性
f_back -> frameobject
f_builtins -> dict key:python的内置函数名
f_code -> 代码对象
def test():
pass
print dir(test)
test.func_code #即是code object
f_globals -> dict 全局对象 可以修改全局对象
f_locals -> dict 同上
f_lineno
通过pdb inspect current frame
import sys, pdb
def test():
frame = sys._current_frames()
a = "lmy"
pdb.set_trace()
def aFunction():
a = 1
b = 'hello'
c = (12, 3.45)
test()
d = "This won't show up in the frame"
aFunction()
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。