Python for loop - why does this not infinite loop? -
consider following snippet of python code:
x = 14 k in range(x): x += 1
at end of execution, x
equal 28.
my question: shouldn't code loop forever? @ each iteration, checks if k
less x
. however, x
incremented within loop, has higher value next comparison.
range(x)
not "command". creates range object 1 time, , loop iterates on that. changing x not change objects made using it.
>>> x = 2 >>> k = range(x) >>> list(k) [0, 1] >>> x += 1 >>> list(k) [0, 1]