check1 = False
check2 = False
if x == y:
sending = check1
elif x == z:
sending = check2
if something is True:
if sending is False: #actually checks if check1 or check2 is False
do_stuff()
sending = True #1 - want to change the actual variable ( check1 or check2 )
await ask_for_confirmation()
else:
return
根据其他一些变量,我引用了check1
或check2
,之后,如果所选的是False
、do_stuff()
和ask_for_confirmation()
,我想将特定的所选变量更改为True
,使其不再是do_stuff()
(确认再次运行整个过程(。
我知道我可以像这样再次检查变量:
check1 = False
check2 = False
if x == y:
sending = check1
elif x == z:
sending = check2
if something is True:
if sending is False:
do_stuff()
if x == y:
check1 = True
elif x == z:
check2 = True
await ask_for_confirmation()
else:
return
但这似乎有很多不必要的代码,我觉得有更好的方法可以做到这一点。有没有一种方法可以用引用更改原始变量?(参见上部代码中的#1(
您遇到的问题是,当您重新分配sending
时,您只是将其指向一个不同的值,而不是修改它以前指向的值。
您要做的是将两个check
值放入一个可变容器中,例如一个列表,然后修改该列表。
checks = [False, False]
if x == y:
sending = 0
elif x == z:
sending = 1
# else:
# sending = ???
if something:
if checks[sending]:
return
do_stuff()
checks[sending] = True
await ask_for_confirmation()
或者将每个容器放入其自己的可变容器中(您仍然需要为容器添加下标以访问或修改其包含的值(:
check1 = [False]
check2 = [False]
if x == y:
sending = check1
elif x == z:
sending = check2
# else:
# sending = ???
if something:
if sending[0]:
return
do_stuff()
sending[0] = True
await ask_for_confirmation()