Table of Contents

不用 global 的文件范围的不预先声明的全局变量

def A():
    globals()["wzj"] = "nb"

A()
print(wzj)

输出:nb

但是:

another.py:

def B():
    print(wzj)

之后:

def A():
    globals()["wzj"] = "nb"

A()
print(wzj)

from profile import B
B()

则会 NameError: name 'wzj' is not defined

Nemo Xiong avatar
Nemo Xiong
ex-Cybersecurity Executor, now a student in Unimelb
comments powered by Disqus