如果您在命令行啓動這個過程中,你可以檢查追溯。
>>> 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