我想对文件之间的导入变量进行更改。 参见下例:导入变量的可变性
file1.py
string = 'data1'
def change_string():
global string
string = 'data2'
#this does not update string in file2
change_string()
file2.py
from file1 import string
#this prints initial value of string, regardless of change_string() function called in file1
print string
尽管file2中的值打印它仍然读取之前调用file1中CHANGE_STRING() 'DATA1'。
我在这里错过了什么?我可以通过导入语句编辑其他变量(如字典等集合)。
如果我拨打print string
后change_string()
我想打印data2
。
我该怎么做我想做的事?
我在测试后得到了“data2”。 – stamaimer
python 2或3? – MaNKuR
@MaNKuR应该是Python 2.'打印字符串'不允许在Python 3中。 –