for i in range(5): def test(i=i): print(i) test() test() test() test() test()
Does it print 4 every time? Can someone help me figure this out.
You redefine test4 times:
test
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)
therefore you only have 1 test()last.
test()
The function is testredefined on each iteration of the loop.
By the time the loop completes, it's testsimple:
def test(i=4): print(i)
script , for 4
test(), 4
,
for i in range(5): print(i) def test(i=i): print("test") print(i) test() test() test() test() test()
:
0 1 2 3 4 test 4 test 4 test 4 test 4 test 4
Source: https://habr.com/ru/post/1649378/More articles:How to set default MessageDialog button in QML? - buttonWhat is wrong with the in_array (...) PHP function? - arraysEncode IP address using all printable characters in Python 2.7.x - pythonLogging in via Jsoup post method does not work - javaCan I run multiple threads on the same heroku processor (python)? - pythonFile No Exception Found in COMPS Main Application - javaUsing JSOUP to log into ConEd site - javaSaving Enum type in unboxed Vector - haskellОшибка FSCL на простом примере - f#ZeroMQ: how to prioritize sockets in .poll () method? - pythonAll Articles