python 函数的获取当前栈信息

示例代码

    import sys


    def example_function():
        frame = sys._getframe()
        for attr_name in dir(frame):
            if not attr_name.startswith('__'):
                attr_value = getattr(frame, attr_name)
                print('attr_name={attr_name}: attr_value={attr_value}'.format(attr_name=attr_name, attr_value=attr_value))

        code_obj = frame.f_code
        for attr_name in dir(code_obj):
            if not attr_name.startswith('__'):
                attr_value = getattr(code_obj, attr_name)
                print('attr_name={attr_name}: attr_value={attr_value}'.format(attr_name=attr_name, attr_value=attr_value))

    example_function()

关键代码

sys._getframe().f_code.co_name
posted @ 2025-06-30 10:15  vx_guanchaoguo0  阅读(12)  评论(0)    收藏  举报