我是新人这是python文档的示例。
def funtest(num, L=[]):
L.append(num)
return L
print(funtest(2))
print(funtest(3))
print(funtest(4))
我认为非常函数调用应该执行相同的操作,这意味着非常函数调用中的L = []。所以我虽然输出应该是这样的:
[2]
[3]
[4]
但真正的答案在下面。看起来funtest()继承了上一个函数调用的输出。然后连续将其传递给下一个函数调用。我对此感到困惑。
[2]
[2, 3]
[2, 3, 4]
当我将代码更改为以下情况时:
def funtest(L=[]):
L.append(L)
return L
print(funtest([2]))
print(funtest([3]))
print(funtest([4]))
实际的输出是这样的:
[2, [...]]
[3, [...]]
[4, [...]]
输出结果与前一个完全不同。我现在比较困惑。有人能修复我的头脑吗?非常感谢