这个安全吗?
与以下相关:Proper way to declare custom exceptions in modern Python? 但是,这样安全吗?我的意思是我正在覆盖self的属性?
在SpecialAlert中,我想传递各种东西(这是一个异常,用于进行一系列不同的检查,我在循环中使用"try..except SpecialAlert as error",然后在处理程序中获取错误的详细信息)。
如果我没有覆盖"message",我应该跳过在self上设置其他属性吗?它们是什么?
class SpecialAlert(Exception):
def __init__(self, message, **kwargs):
Exception.__init__(self, message)
for kw in kwargs:
if kw == 'message':
continue
setattr(self, kw, kwargs[kw])
与以下相关:Proper way to declare custom exceptions in modern Python? 但是,这样安全吗?我的意思是我正在覆盖self的属性?
在SpecialAlert中,我想传递各种东西(这是一个异常,用于进行一系列不同的检查,我在循环中使用"try..except SpecialAlert as error",然后在处理程序中获取错误的详细信息)。
如果我没有覆盖"message",我应该跳过在self上设置其他属性吗?它们是什么?
**kwargs
中,因此您不需要使用if kw == 'message'
。 - user2357112