如何获取list.sort()调用的帧数据?

时间:2018-04-03 18:52:21

标签: python python-3.x introspection inspect stackframe

我尝试访问list.sort方法的特定调用的帧信息,但我遇到了麻烦。

import inspect

def calm_pear(x): # compare    
    cur_frame = inspect.currentframe()
    out_frames = inspect.getouterframes(cur_frame)
    print(out_frames[0].function) # prints 'calm_pear'
    print(out_frames[1].function) # prints 'inner'  
    print(out_frames[2].function) # prints 'outer'
    return id(x)

def outer():
    inner()

def inner():
    [0, 1].sort(key=calm_pear)

outer() # call outer

我得到的打印件是:

    calm_pear
    inner

但是,来电订单是(outerinnerlist.sortcalm_pear

为什么out_frames[1].function list.sort之类的内容不是corrplot

1 个答案:

答案 0 :(得分:1)

list.sort是用C语言编写的。用C语言编写的函数不会得到Python堆栈帧。 Python堆栈帧只需要执行用Python编写的代码。

无论您希望通过访问list.sort不存在的堆栈帧来做什么,您都必须找到其他方法来实现它。