如果您在命令行启动这个过程中,你可以检查追溯。
>>> def a(x): y=x; 0/0
...
>>> def b(x): a(x)
...
>>> try: b(100)
... except: import sys; e=sys.exc_info()
...
>>> e
(<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('integer division or modulo by zero',), <traceback object at 0x00B4B670>)
>>> e[2]
<traceback object at 0x00B4B670>
>>> dir(e[2])
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'tb_frame', 'tb_lasti', 'tb_lineno', 'tb_next']
>>> e[2].tb_next
<traceback object at 0x00B4B648>
>>> e[2].tb_next.tb_next
<traceback object at 0x00B4B5F8>
>>> e[2].tb_next.tb_next.tb_next
>>> e[2].tb_next.tb_next
>>> e[2].tb_next.tb_next.tb_frame
<frame object at 0x00B88060>
>>> dir(e[2].tb_next.tb_next.tb_frame)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'f_back', 'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_globals', 'f_lasti', 'f_lineno', 'f_locals', 'f_restricted', 'f_trace']
>>> e[2].tb_next.tb_next.tb_frame.f_locals
{'y': 100, 'x': 100}
所以在这里你可以得到调用堆栈中所有变量的值。 AFAIK他们仍然在这里untik下一个例外。
你试图做什么的目的仍然不清楚,但为什么不只是做一个尝试/除了在除了你应该有权访问该字典的部分。 – Bogdan 2012-01-11 12:41:31
@Bogdan,你说的是对的。我只是忘了包含try/except块!现在我必须再次运行这项工作! – user739807 2012-01-11 12:44:56