【发布时间】:2021-01-27 06:44:57
【问题描述】:
假设我有两个 Python 文件:
-
abc.py:
from .config import *
update_a()
print_a() # prints 5
print(a) # prints 2 rather than 5 even after calling update_a() and using global in update_a()
-
config.py:
a = 2
def update_a():
global a
a = 5
def print_a():
global a
print(a) # prints 5
config.py 中的全局变量在从abc.py 访问时似乎没有更新的值。
【问题讨论】:
-
使用
globals()来实现您想要实现的目标!
标签: python python-3.x global-variables