While working on understanding string permutations and its implementation in python (regarding to this post) I stumbled upon something in a for
loop using range()
I just don't understand.
Take the following code:
def recursion(step=0):
print "Step I: {}".format(step)
for i in range(step, 2):
print "Step II: {}".format(step)
print "Value i: {}".format(i)
print "Call recursion"
print "\n-----------------\n"
recursion(step + 1)
recursion()
That gives the following output:
root@host:~# python range_test.py
Step I: 0
Step II: 0
Value i: 0
Call recursion
-----------------
Step I: 1
Step II: 1
Value i: 1
Call recursion
-----------------
Step I: 2
Step II: 0 <---- WHAT THE HECK?
Value i: 1
Call recursion
-----------------
Step I: 1
Step II: 1
Value i: 1
Call recursion
-----------------
Step I: 2
root@host:~#
As you can see the variable step
gets a new value after a certain for
loop run using range()
- see the WHAT THE HECK
mark.
Any ideas to lift the mist?