全局变量在主函数python中

时间:2022-06-29 00:41:21

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中定义一个全局变量,也就是。,这个变量可以由我从主函数调用的任何函数来使用。

Is that possible? What'd be a good way to do this?

这有可能吗?有什么好办法呢?

Thanks!

谢谢!

3 个解决方案

#1


2  

A variable created inside a 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 global keyword.

要更改其值,请使用global关键字。

#2


9  

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中的变量没有声明——它们是在执行赋值语句时创建的。这意味着以下代码(源自宇航人员发布的代码)将被破坏:

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 caketaste is called, no assignment to cake has occurred. It will only occur after setcake has been called.

这是因为在调用caketaste时,没有发生对cake的赋值。它只会在调用setcake之后发生。

You can see the error here: http://ideone.com/HBRN4y

您可以在这里看到错误:http://ideone.com/HBRN4y

#3


1  

You need to use global statements. These are relatively simple. To do this, simply define a variable as global before defining the variable itself. For example:

您需要使用全局语句。这些都是相对简单的。为此,只需在定义变量本身之前将变量定义为全局变量。例如:

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

#1


2  

A variable created inside a 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 global keyword.

要更改其值,请使用global关键字。

#2


9  

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中的变量没有声明——它们是在执行赋值语句时创建的。这意味着以下代码(源自宇航人员发布的代码)将被破坏:

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 caketaste is called, no assignment to cake has occurred. It will only occur after setcake has been called.

这是因为在调用caketaste时,没有发生对cake的赋值。它只会在调用setcake之后发生。

You can see the error here: http://ideone.com/HBRN4y

您可以在这里看到错误:http://ideone.com/HBRN4y

#3


1  

You need to use global statements. These are relatively simple. To do this, simply define a variable as global before defining the variable itself. For example:

您需要使用全局语句。这些都是相对简单的。为此,只需在定义变量本身之前将变量定义为全局变量。例如:

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