Python function with default argument inside loop -


for in range(5):    def test(i=i):       print(i)  test() test() test() test() test() 

this prints 4 every time? can me understanding this.

you redefine 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 have 1 test() last one.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -