python - 如何修复 : W602 deprecated form of raising exception

标签 python pylint pep8

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

W602 已弃用的引发异常的形式

这是我在代码中使用异常的方式:

if CONDITION == True:
    raise ValueError, HELPING_EXPLANATION

最佳答案

像这样提出你的异常:

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.

关于python - 如何修复 : W602 deprecated form of raising exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11991637/

相关文章:

python - 在 for 循环内打开,类型错误 : 'str' object is not callable (Python)

java运行时使用raw_input执行python脚本

python - 如何停止重定向移动请求 Nginx Flask?

python - 用于记录继承另一个类的类的 PEP8 约定是什么?

Python 命名约定 : indicating if function alters arguments

python - 没有日期的 pandas.read_csv parse_dates

python - Pylance 要求在一致的列表变量上显式类型

python - Pylint 未分组导入警告

python - 有没有办法让 PyLint 标志 'Yoda conditions' ?

python - 演示所有 PEP-8 规则的完整代码示例