for i in range(5):
def test(i=i):
print(i)
test()
test()
test()
test()
test()
这每次打印 4?谁能帮助我理解这一点。
最佳答案
您重新定义了 test
4 次:
同于:
#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)
所以你只有 1 个 test()
最后一个。
关于循环内带有默认参数的 Python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38632891/