简体   繁体   English

从Python中的另一个脚本进行全局var编辑

[英]Global var editing from another script in Python

I'm trying to increment a python global var from another script, but it doesn't seem to be updating. 我正在尝试从另一个脚本增加python全局变量,但是它似乎没有更新。 Am I doing something wrong here? 我在这里做错什么了吗?

I run func() multiple times during the script execution and it never seems to update globVal 我在脚本执行过程中多次运行func() ,但它似乎从未更新过globVal

script 1: 脚本1:

def func():
    from script2 import globVal
    global globVal
    print "glob val is " + str(globVal)
    globVal = globVal + 1

script 2 (different file): 脚本2(不同文件):

global globVal
globVal = 1

Yes, you are doing something wrong (answering your first question) 的,你做错了什么(在回答你的第一个问题)

You should move from script2 import globVal to the top of script2 as that value is being imported each time as 1. After moving the import to the top, the globVal variable should update. 你应该将from script2 import globVal到顶部script2为值,每次将被导入为1.移动后import到顶部, globVal变量应该更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM