如何修复:W602不推荐的引发异常的形式

2024-05-10 10:26:00 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我使用pylint(通过SublimerInter),我将收到以下警告消息:

W602 deprecated form of raising exception

我如何在代码中使用异常:

if CONDITION == True:
    raise ValueError, HELPING_EXPLANATION

Tags: of代码formtrue消息警告ifexception
1条回答
网友
1楼 · 发布于 2024-05-10 10:26:00

提出这样的例外:

if CONDITION == True:
    raise ValueError(HELPING_EXPLANATION)

来自PEP 8 -- Style Guide for Python Code - Programming Recommendations

When raising an exception, use raise ValueError('message') instead of the older form raise ValueError, 'message'.

The paren-using form is preferred because when the exception arguments are long or include string formatting, you don't need to use line continuation characters thanks to the containing parentheses. The older form will be removed in Python 3.

相关问题 更多 >