I'm trying to understand the walrus assignment operator.
Classic while loop breaks when condition is reassigned to False within the loop.
x = True
while x:
print('hello')
x = False
Why doesn't this work using the walrus operator? It ignores the reassignment of x producing an infinite loop.
while x := True:
print('hello')
x = False
x := True
is evaluated at the top of the loop, every time the top of the loop is reached. So the value ofx
tested is alwaysTrue
.x = False
was not ignored.x := True
changes the binding ofx
toTrue
regardless of whatx
was bound to. – Subtlety