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] 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -