相关疑难解决方法(0)

堆栈跟踪中的Python函数的名称

在Python2和Python3中,在堆栈跟踪__name__中没有使用函数,而是使用原始名称(后面指定的名称def).

考虑这个例子:

import traceback

def a():
    return b()

def b():
    return c()

def c():
    print("\n".join(line.strip() for line in traceback.format_stack()))

a.__name__ = 'A'
b.__name__ = 'B'
c.__name__ = 'C'

a();
Run Code Online (Sandbox Code Playgroud)

输出是:

File "test.py", line 16, in <module>
    a();
File "test.py", line 4, in a
    return b()
File "test.py", line 7, in b
    return c()
File "test.py", line 10, in c
    print("\n".join(line.strip() for line in traceback.format_stack()))
Run Code Online (Sandbox Code Playgroud)

为什么这样?如何更改堆栈跟踪中使用的名称?__name__那么使用的属性在哪里?

python metaprogramming

10
推荐指数
1
解决办法
773
查看次数

标签 统计

metaprogramming ×1

python ×1