跳到内容

builtin-variable-shadowing (A001) (内置变量遮蔽 (A001))

派生自 flake8-builtins linter。

作用

Checks for variable (and function) assignments that use the same names as builtins. (检查使用与内置函数相同名称的变量(和函数)赋值。)

为什么这不好?

Reusing a builtin name for the name of a variable increases the difficulty of reading and maintaining the code, and can cause non-obvious errors, as readers may mistake the variable for the builtin and vice versa. (将内置名称重新用于变量名称会增加阅读和维护代码的难度,并可能导致不明显的错误,因为读者可能会将变量误认为是内置函数,反之亦然。)

可以通过 lint.flake8-builtins.ignorelist 配置选项将内置函数标记为此规则的例外。

示例

def find_max(list_of_lists):
    max = 0
    for flat_list in list_of_lists:
        for value in flat_list:
            max = max(max, value)  # TypeError: 'int' object is not callable
    return max

建议改为

def find_max(list_of_lists):
    result = 0
    for flat_list in list_of_lists:
        for value in flat_list:
            result = max(result, value)
    return result

Options (选项)

参考