如何获得调用函数的函数名

def get_cur_info():

    """Return the frame object for the caller's stack frame."""

    try:

        raise Exception

    except:

        f = sys.exc_info()[2].tb_frame.f_back

    return (f.f_code.co_name, f.f_lineno)

 


 

   

================================================================================

另外,利用python的 inspect 模块中的getframeinfo也可以得到.

inspect.getframeinfo( frame [, context ])

Get information about a frame or traceback object. A 5-tuple is returned, the last five elements of the frame’s frame record.

Changed in version 2.6: Returns a named tuple Traceback(filename, lineno, function,code_context, index).

posted on 2016-08-06 15:38  与非朋仔  阅读(224)  评论(0编辑  收藏  举报

导航