简体   繁体   English

从另一个程序在一个程序中的函数中调用全局变量

[英]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.

相关问题 从一个函数调用变量到另一个函数 - Calling variable from one function into another function 在Python中调用一个函数的变量到另一个函数 - Calling a variable from one function to another function in Python 在另一个函数中使用一个函数中的变量然后调用它 - Using a Variable from One Function Inside Another Function then Calling It 从另一个函数调用一个函数内部定义的变量 - Calling variable defined inside one function from another function 从另一个函数调用变量 - Calling a variable from another function 如何使用在一个 function 中声明的局部变量并将其实现到另一个 function? (没有全局变量或调用函数) - How can I use a local variable declared in one function and implement it to another function? (Without global variables or calling the function) Python 2.6,全局变量不会改变,在另一个函数中调用函数 - Python 2.6, global variable wont change, calling functions in another function 在烧瓶中从一个应用程序调用变量到另一个应用程序 - calling a variable from one app to another in flask 如何在不使用全局变量的情况下将列表从一个 function 传递到另一个? - How to pass list from one function to another without using global variable? 从Python 3.3中的另一个函数调用变量? - calling a variable from another function in python 3.3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM