在 Python 中,许多方法使用“标准化”名称定义参数变量,例如:
def __exit__(self, type, value, traceback):
在上面的行中,变量 type 导致 pylint 警告 (W0622) 正在重新定义内置函数:正在重新定义内置“类型”(redefine-builtin).
有很多方法可以解决此问题并让 pylint 满意(重命名变量、添加 pylint 指令 (# pylint:disable=W0622) 来忽略问题等)。
在这些情况下保持良好的代码质量并使 pylint 满意的最佳/首选/建议/常规使用方法(如果有)是什么?
最佳答案
禁用 pylint 警告可能被认为是一种不好的做法。
In order for
__exit__
to work properly it must have exactly three arguments: exception_type, exception_value, and traceback. The formal argument names in the method definition do not need to correspond directly to these names, but they must appear in this order.
因此,使用元组打包是一个不错的选择def __exit__(self, *exc)
这是官方文档中建议的内容: https://docs.python.org/3/library/contextlib.html
关于python - pylint W0622(重新定义内置)在子类中重写 "standard"方法时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56056551/