首页 > 解决方案 > Python中的意外名称错误

问题描述

>>> e = 'e'
>>> e
'e'
>>> try: raise Exception
except Exception as e:
    e = 'e'
    a = 'a'

>>> e
Traceback (most recent call last):
  File "<pyshell#333>", line 1, in <module>
    e
NameError: name 'e' is not defined
>>> a
'a'

我以前没有注意到这种行为。为什么使用变量作为 try-except 别名会使其失去先前的定义?

标签: pythonvariablesexceptionnameerrortry-except

解决方案


推荐阅读