Python 全局命名空间中的变量未被 class 命名空间中的函数中的扩充赋值更新
Python variable in global namespace not being updated by augmented assignment in function in class namespace
每次某个值保持不变时,我正在尝试将计数器变量更新一个。
主要条件是这样的:
streak = 0
entryno_counter = 1
class OtherDataGet:
streak_type = 3
uod_state = 3
@staticmethod
def uod():
global streak
if entryno_counter == 1:
pass
else:
if values[1] > values_cache[1]: # If value went up
if OtherDataGet.uod_state == 1 or 2: # If it was previously down or same
OtherDataGet.uod_state = 0 # Set state to up
streak = 0 # Reset streak
OtherDataGet.streak_type = 0 # Set streak type to up
elif OtherDataGet.uod_state == 0: # If last state was up
streak += 1 # Add one to streak counter
return 0
elif values[1] < values_cache[1]:
if OtherDataGet.uod_state == 0 or 2:
OtherDataGet.uod_state = 1
streak = 0
OtherDataGet.streak_type = 1
elif OtherDataGet.uod_state == 1:
streak += 1
return 1
elif values[1] == values_cache[1]:
if OtherDataGet.uod_state == 0 or 1:
OtherDataGet.uod_state = 2
streak = 0
OtherDataGet.streak_type = 2
elif OtherDataGet.uod_state == 2:
streak += 1
return 2
正在更新的变量在全局命名空间中,随处可见,更新应该没有问题。
比如第一次返回a2时,连胜计数器设置为0,第二次返回a2,应该设置为1,第三次返回a2,应该成为 3 等
1,125519,0,182701,4,404,0,1
2,125519,2,182702,4,404,2,1
3,125518,1,182703,4,404,1,1
4,125519,0,182704,4,404,0,1
5,125519,2,182705,4,404,2,1
6,125519,2,182706,4,404,2,1
7,125519,2,182706,4,404,2,1
8,125519,2,182707,4,404,2,1
9,125517,1,182708,4,404,1,1
10,125518,0,182709,4,404,0,1
11,125517,1,182710,4,404,1,1
这是输出数据,您只需查看最后两列即可。倒数第二个是OtherDataGet.uod
的返回值,最后一个应该是streak
。如果你看第 5-8 行,有一个 2s 的连胜,并且该行的最后一个值应该分别是 1、2、3、4,但即使它应该被重置为 0,它仍然保持为 1。
当您尝试在函数内对全局变量赋值时,它会创建一个局部变量。为防止这种情况发生,请在函数中使用 global
关键字。
在这种情况下,将是:
def uod():
global streak, entryno_counter
其余如常。
只需在您要使用它的任何函数中全局声明该变量。例如:
def uod():
global streak
每次某个值保持不变时,我正在尝试将计数器变量更新一个。
主要条件是这样的:
streak = 0
entryno_counter = 1
class OtherDataGet:
streak_type = 3
uod_state = 3
@staticmethod
def uod():
global streak
if entryno_counter == 1:
pass
else:
if values[1] > values_cache[1]: # If value went up
if OtherDataGet.uod_state == 1 or 2: # If it was previously down or same
OtherDataGet.uod_state = 0 # Set state to up
streak = 0 # Reset streak
OtherDataGet.streak_type = 0 # Set streak type to up
elif OtherDataGet.uod_state == 0: # If last state was up
streak += 1 # Add one to streak counter
return 0
elif values[1] < values_cache[1]:
if OtherDataGet.uod_state == 0 or 2:
OtherDataGet.uod_state = 1
streak = 0
OtherDataGet.streak_type = 1
elif OtherDataGet.uod_state == 1:
streak += 1
return 1
elif values[1] == values_cache[1]:
if OtherDataGet.uod_state == 0 or 1:
OtherDataGet.uod_state = 2
streak = 0
OtherDataGet.streak_type = 2
elif OtherDataGet.uod_state == 2:
streak += 1
return 2
正在更新的变量在全局命名空间中,随处可见,更新应该没有问题。
比如第一次返回a2时,连胜计数器设置为0,第二次返回a2,应该设置为1,第三次返回a2,应该成为 3 等
1,125519,0,182701,4,404,0,1
2,125519,2,182702,4,404,2,1
3,125518,1,182703,4,404,1,1
4,125519,0,182704,4,404,0,1
5,125519,2,182705,4,404,2,1
6,125519,2,182706,4,404,2,1
7,125519,2,182706,4,404,2,1
8,125519,2,182707,4,404,2,1
9,125517,1,182708,4,404,1,1
10,125518,0,182709,4,404,0,1
11,125517,1,182710,4,404,1,1
这是输出数据,您只需查看最后两列即可。倒数第二个是OtherDataGet.uod
的返回值,最后一个应该是streak
。如果你看第 5-8 行,有一个 2s 的连胜,并且该行的最后一个值应该分别是 1、2、3、4,但即使它应该被重置为 0,它仍然保持为 1。
当您尝试在函数内对全局变量赋值时,它会创建一个局部变量。为防止这种情况发生,请在函数中使用 global
关键字。
在这种情况下,将是:
def uod():
global streak, entryno_counter
其余如常。
只需在您要使用它的任何函数中全局声明该变量。例如:
def uod():
global streak