for i in range(5):
def test(i=i):
print(i)
test()
test()
test()
test()
test()
This prints 4 every time? Can someone help me understanding this.
The function test
is redefined every iteration of the loop.
By the time the loop is done, test
is simply:
def test(i=4):
print(i)
You redefine the test
4 times:
same as:
#define test
def test(i = 0):
print(i)
#redefine test
def test(i = 1):
print(i)
#redefine test
def test(i = 2):
print(i)
#redefine test
def test(i = 3):
print(i)
#redefine test
def test(i = 4):
print(i)
so you have only 1 test()
the last one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With