如何在python中重置全局变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3657163/
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
How to reset global variable in python?
提问by user12345
SOME_VARIABLE = []
def some_fun:
append in SOME_VARIABLE
s = []
s = SOME_VARIABLE
SOME_VARIABLE = [] // Not setting to empty list.
return s
How to reset SOME_VARIABLEto empty.
如何重置SOME_VARIABLE为空。
采纳答案by Rodrigue
If you read a variable, Python looks for it in the entire scope chain. This mean that:
如果您读取一个变量,Python 会在整个作用域链中查找它。这意味着:
GLOB_VAR = "Some string"
def some_fun():
print GLOB_VAR
will print Some string
将打印 Some string
Now, if you write to a variable, Python looks for it in the local scope, and if it cannot find a variable with the name you gave at the local level, then it creates one.
现在,如果您写入一个变量,Python 会在本地范围内查找它,如果在本地级别找不到具有您指定名称的变量,则它会创建一个。
This means that in your example, you have created a variable named SOME_VARIABLElocal to your some_funfunction, instead of updating the global SOME_VARIABLE. This is a classic python gotcha.
这意味着在您的示例中,您为函数创建了一个名为SOME_VARIABLElocal的变量some_fun,而不是更新 global SOME_VARIABLE。这是一个经典的 Python 陷阱。
If you want to write to your global, you have to explicitly tell Python that you are talking about a global variable that already exists. To do so, you need to use the globalkeyword. So, the following:
如果你想写入你的全局变量,你必须明确地告诉 Python 你在谈论一个已经存在的全局变量。为此,您需要使用global关键字。因此,以下内容:
GLOB_VAR = "Some string"
def some_fun():
global GLOB_VAR
GLOB_VAR = "Some other string"
some_fun()
print GLOB_VAR
will print Some other string.
将打印Some other string。
Note: I see it as a way of encouraging people to keep global variables read-only, or at least to think about what they're doing.
注意:我认为这是一种鼓励人们将全局变量保持为只读的方式,或者至少考虑他们在做什么。
The behaviour is the same (just a bit more surprising) when you try to read first and then write to a global. The following:
当您尝试先读取然后写入全局时,行为是相同的(只是有点令人惊讶)。下列:
GLOB_VAR = False
def some_fun():
if GLOB_VAR:
GLOB_VAR = False
some_fun()
will raise:
将提高:
Traceback (most recent call last):
File "t.py", line 7, in <module>
some_fun()
File "t.py", line 4, in some_fun
if GLOB_VAR:
UnboundLocalError: local variable 'GLOB_VAR' referenced before assignment
because since we will modify GLOB_VAR, it is considered a local variable.
因为因为我们将修改GLOB_VAR,它被认为是一个局部变量。
Update: Ely Benderskyhas a related in-depth postabout this that is worth a read for more formal details.
更新:Ely Bendersky有一篇相关的深入文章,值得一读以了解更正式的细节。
回答by Ignacio Vazquez-Abrams
SOME_VARIABLEis global, so rebinding it won't take effect unless you use global. But since it's a mutable object, just mutate it appropriately.
SOME_VARIABLE是全局的,因此除非您使用global. 但由于它是一个可变对象,只需适当地对其进行变异即可。
del SOME_VARIABLE[:]
回答by atzz
You need to tell the interpreter that you are talking about a global variable:
你需要告诉解释器你在谈论一个全局变量:
def some_fun:
global SOME_VARIABLE
...
SOME_VARIABLE = []
回答by psoares
if you don't need the SOME_VARIABLE anymore you could use:
如果您不再需要 SOME_VARIABLE,您可以使用:
del SOME_VARIABLE
if you want a empty list:
如果你想要一个空列表:
del SOME_VARIABLE[:]

