baris a global variable. You should put global barinside example():
def example():
global bar
while True:
time.sleep(5)
bar = True
- When reading a variable, it is first searched inside the function, and if it is not found, then outside. Therefore, do not put it
global barinside foo(). - , ,
global. global bar example()