Python为什么以这种方式将数组存储在函数中



我在检查小脚本时注意到:

def foo( a = [] ):
a += [1]
print(a)

foo() # prints [1]
foo() # prints [1,1]
foo() # prints [1,1,1]

为什么会发生这种情况而不是只打印[1]

您基本上将元素追加到列表中。例如[1]+[1]将是[1,1]。

最新更新