主函数python中的全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16502377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
global variable inside main function python
提问by Dnaiel
I wanted to define a global variable in main, i.e., a variable that can be used by any function I call from the main function.
我想在 main 中定义一个全局变量,即我从 main 函数调用的任何函数都可以使用的变量。
Is that possible? What'd be a good way to do this?
那可能吗?这样做的好方法是什么?
Thanks!
谢谢!
采纳答案by Levon
A variable created insidea method (e.g., main) is local by definition. However, you can create a global variable outside the method, and access and change its value from side any other method.
根据定义,在方法内部创建的变量(例如,main)是局部变量。但是,您可以在该方法之外创建一个全局变量,并从任何其他方法中访问和更改其值。
To change its value use the globalkeyword.
要更改其值,请使用global关键字。
回答by Marcin
What you want is not possible*. You can just create a variable in the global namespace:
你想要的是不可能的*。您可以在全局命名空间中创建一个变量:
myglobal = "UGHWTF"
def main():
global myglobal # prevents creation of a local variable called myglobal
myglobal = "yu0 = fail it"
anotherfunc()
def anotherfunc():
print myglobal
DON'T DO THIS.
不要这样做。
The whole point of a function is that it takes parameters. Just add parameters to your functions. If you find that you need to modify a lot of functions, this is an indication that you should collect them up into a class.
函数的全部意义在于它接受参数。只需向您的函数添加参数即可。如果您发现需要修改很多函数,则表明您应该将它们收集到一个类中。
*To elaborate on why this isn't possible: variables in python are not declared - they are created when an assignment statement is executed. This means that the following code (derived from code posted by astronautlevel) will break:
*详细说明为什么这是不可能的:python 中的变量没有声明——它们是在执行赋值语句时创建的。这意味着以下代码(源自 astronautlevel 发布的代码)将中断:
def setcake(taste):
global cake
cake = taste
def caketaste():
print cake #Output is whatever taste was
caketaste()
Traceback (most recent call last):
File "prog.py", line 7, in <module>
caketaste()
File "prog.py", line 5, in caketaste
print cake #Output is whatever taste was
NameError: global name 'cake' is not defined
This happens because when caketasteis called, no assignment to cakehas occurred. It will only occur after setcakehas been called.
发生这种情况是因为当caketaste被调用时,没有cake发生赋值。它只会在setcake被调用后发生。
You can see the error here: http://ideone.com/HBRN4y
您可以在此处查看错误:http: //ideone.com/HBRN4y
回答by astronautlevel
You need to use globalstatements. These are relatively simple. To do this, simply define a variable as global before defining the variable itself.
For example:
您需要使用global语句。这些都比较简单。为此,只需在定义变量本身之前将变量定义为全局变量。例如:
def setcake(taste):
global cake
cake = taste
def caketaste():
print cake
setcake('tasty')
caketaste() #Output is tasty
EDIT: Sorry, it seems that I misread your question. Allow me to try answering it properly now.
编辑:抱歉,我似乎误读了您的问题。现在请允许我尝试正确回答。
def printcake():
print cake #This function prints the taste of cake when called
def setcake(taste, printq):
global cake #This makes sure that cake can be called in any function
cake = taste #sets cake's taste
if printq: #determines whether to print the taste
printcake()
setcake('good', True) #Calls the function to set cake. Tells it to print result. The output is good
The output, as seen in code: http://ideone.com/dkAlEp
输出,如代码所示:http: //ideone.com/dkAlEp

