[英]Calling a global variable in a function in one program from another
I'm sure this is very simple, but I am trying to manipulate a global variable I have created in function from another script calling that function as follows: 我敢肯定这很简单,但是我试图通过另一个调用该函数的脚本来操纵我在函数中创建的全局变量,如下所示:
Script 1: 脚本1:
def function_1():
global myvar
myvar = 1
Script 2: 脚本2:
from script1 import function_1
function_1()
myvar2 = myvar + 1
print myvar2
I don't know why that isn't working. 我不知道为什么这不起作用。 I haven't been able to locate the exact answer I need on Stack Overflow or elsewhere. 我无法在Stack Overflow或其他地方找到所需的确切答案。 Can anyone assist? 有人可以协助吗?
Thanks 谢谢
Global variables affect only the module in which they are defined. 全局变量仅影响定义它们的模块。 To access it, you need to get the module object: 要访问它,您需要获取模块对象:
#Script2
import script1
script1.function_1()
myvar2 = script1.myvar + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.