python输出

def f(x,l=[]):
for i in range(x):
l.append(i*i)
print(l)

f(4)
f(3)
f(3,[3,2,1])
f(2)
f(5)

C:UserslkrPycharmProjectslianxivenvScriptspython.exe C:/Users/lkr/PycharmProjects/lianxi/test.py
[0, 1, 4, 9]
[0, 1, 4, 9, 0, 1, 4]
[3, 2, 1, 0, 1, 4]
[0, 1, 4, 9, 0, 1, 4, 0, 1]
[0, 1, 4, 9, 0, 1, 4, 0, 1, 0, 1, 4, 9, 16]

Process finished with exit code 0

第二个函数调用的结果就有些奇怪了。它使用了之前内存地址中存储的旧列表。这就是为什么它的前四个元素是0,1,4,9了。

原文地址:https://www.cnblogs.com/turningli/p/10474179.html