临时异常类是使用 Python 脚本中的“类型”动态定义的,旨在用作模块。当此类的实例在导入脚本中被捕获时,它无法识别该类。下面是代码片段
# the throwing module, defines dynamically
def bad_function():
ExceptionClass = type( "FooBar", (Exception,),
{ "__init__": lambda self, msg: Exception.__init__(self, msg) })
raise ExceptionClass("ExceptionClass")
使用代码
import libt0
try:
libt0.bad_function()
#except libt0.FooBar as e:
#print e
except Exception as e:
print e
print e.__class__
可以解释为什么 libt0.FooBase 对这个脚本不可见吗?最后一行的观察者输出。