【发布时间】:2020-04-29 16:08:11
【问题描述】:
本质上,变量“存在”不会返回。输出是“假,真,假”,但我不明白为什么会这样。从代码中可以看出,我尝试使变量“存在”global 无效。
我环顾四周,发现了两个类似的问题-Python--Function not returning value、Python: Return possibly not returning value,但不明白为什么我的代码不起作用。
我知道这个问题非常具体——如果我错过了一个愚蠢的错误,那么我会删除它,如果是 Python 警告,那么我会看看我是否可以改写它。
import time
global exists
exists= False
def in_thread(exists):
print(exists)
time.sleep(2)
exists= True
print(exists)
return(exists)
in_thread(exists)
print(exists)
【问题讨论】:
-
如果你修改了一个全局变量的值,你需要在你的函数的开头调用它。这可能是问题的一部分
标签: python-3.x