inspect.getouterframes()函数的'index'是什么?

时间:2013-03-07 06:13:26

标签: python

我在python上做了一个“help(inspect.getouterframes)”,这就是它给了我的东西:

getouterframes(frame, **context**=1)
Get a list of records for a frame and all higher (calling) frames.

Each record contains a frame object, filename, line number, function
name, a list of lines of context, and **index within the context**.

我只是想知道......这些“背景”和“指数”是什么意思?

谢谢!

1 个答案:

答案 0 :(得分:0)

从当前行的周围代码中添加一些上下文。简单的例子:

import sys
import inspect

def f():
    # prev
    return sys._getframe()
    # next

# prev
framelist = inspect.getouterframes(f(), 3)
# next

for frame in framelist:
    print frame[3], "context:\n"
    for i, line in enumerate(frame[-2]):
        print line.rstrip(),
        if i == frame[-1]:
            print ' *** index ***'
        else:
            print
    print

输出:

f context:

    # prev
    return sys._getframe()  *** index ***
    # next

<module> context:

# prev
framelist = inspect.getouterframes(f(), 3)  *** index ***
# next