未将嵌套 Class 视为异常
Not Seeing Nested Class as Exception
下面的代码说找不到 MyError:
class Test:
class MyError(ValueError):
pass
def bad(self):
raise MyError
def myfunc(self):
try:
self.bad()
except MyError as me:
print(me)
tt = Test()
tt.myfunc()
当我 运行 我得到这个:
Traceback (most recent call last):
File "testcase.py", line 16, in <module>
tt.myfunc()
File "testcase.py", line 11, in myfunc
except MyError as me:
NameError: name 'MyError' is not defined
关于引用嵌套 类 我错过了什么?
原来我需要将 class 的路径完全限定为 Test.MyError
class Test:
class MyError(ValueError):
pass
def bad(self):
raise Test.MyError
def myfunc(self):
try:
self.bad()
except Test.MyError as me:
print(me)
tt = Test()
tt.myfunc()
下面的代码说找不到 MyError:
class Test:
class MyError(ValueError):
pass
def bad(self):
raise MyError
def myfunc(self):
try:
self.bad()
except MyError as me:
print(me)
tt = Test()
tt.myfunc()
当我 运行 我得到这个:
Traceback (most recent call last):
File "testcase.py", line 16, in <module>
tt.myfunc()
File "testcase.py", line 11, in myfunc
except MyError as me:
NameError: name 'MyError' is not defined
关于引用嵌套 类 我错过了什么?
原来我需要将 class 的路径完全限定为 Test.MyError
class Test:
class MyError(ValueError):
pass
def bad(self):
raise Test.MyError
def myfunc(self):
try:
self.bad()
except Test.MyError as me:
print(me)
tt = Test()
tt.myfunc()