跳到内容

global-variable-not-assigned (PLW0602)

源自 Pylint 代码检查工具。

作用

检查当前作用域中未赋值的 global 变量。

为什么这不好?

global 关键字允许内部作用域修改在外部作用域中声明的变量。如果变量在内部作用域中未被修改,则无需使用 global

示例

DEBUG = True


def foo():
    global DEBUG
    if DEBUG:
        print("foo() called")
    ...

建议改为

DEBUG = True


def foo():
    if DEBUG:
        print("foo() called")
    ...

参考