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
我永远喜欢妃爱
comments powered by Disqus