如何在python中重置全局变量?

时间:2022-09-06 13:51:52
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_VARIABLE to empty.

如何将SOME_VARIABLE重置为空。

4 个解决方案

#1


7  

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

将打印一些字符串

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_VARIABLE local to your some_fun function, instead of updating the global SOME_VARIABLE. This is a classic python gotcha.

这意味着在您的示例中,您已经为some_fun函数创建了一个名为SOME_VARIABLE local的变量,而不是更新全局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 global keyword. 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.

将打印一些其他字符串。

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 Bendersky has a related in-depth post about this that is worth a read for more formal details.

更新:Ely Bendersky有一篇相关的深入帖子,对于更正式的细节值得阅读。

#2


6  

You need to tell the interpreter that you are talking about a global variable:

您需要告诉解释器您正在谈论全局变量:

def some_fun:
    global SOME_VARIABLE
    ...
    SOME_VARIABLE = []

#3


2  

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[:]

#4


0  

SOME_VARIABLE is 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是全局的,因此除非使用全局,否则重新绑定它不会生效。但由于它是一个可变对象,只需适当地改变它。

del SOME_VARIABLE[:]

#1


7  

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

将打印一些字符串

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_VARIABLE local to your some_fun function, instead of updating the global SOME_VARIABLE. This is a classic python gotcha.

这意味着在您的示例中,您已经为some_fun函数创建了一个名为SOME_VARIABLE local的变量,而不是更新全局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 global keyword. 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.

将打印一些其他字符串。

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 Bendersky has a related in-depth post about this that is worth a read for more formal details.

更新:Ely Bendersky有一篇相关的深入帖子,对于更正式的细节值得阅读。

#2


6  

You need to tell the interpreter that you are talking about a global variable:

您需要告诉解释器您正在谈论全局变量:

def some_fun:
    global SOME_VARIABLE
    ...
    SOME_VARIABLE = []

#3


2  

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[:]

#4


0  

SOME_VARIABLE is 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是全局的,因此除非使用全局,否则重新绑定它不会生效。但由于它是一个可变对象,只需适当地改变它。

del SOME_VARIABLE[:]