Suppose I have:
def func(n):
for i in range(1,100000,2*n+3):
...
It is obvious that the step = 2*n+3
part is calculated once.
But is this guaranteed to be the same for xrange
?
According to this answer, xrange
is a sequence object that evaluates lazily.
So the question is basically - which part evaluates lazily?
Is it only the start <= stop
part, or also the step
part?
I tried a simple test in order to determine the answer:
n = 1
for a in xrange(0,100,n):
print a
n += 1
This test clearly shows that n
is not reevaluated at every iteration.
But I'm suspecting that perhaps the n
inside the xrange
expression "lives in a different scope" than that of the n
declared before the xrange
.
Thank you.