我运行了以下小代码
def func2(xxx=[]):
xxx.append(1)
print xxx
return None
func2()
func2()
func2()
输出如下:
[1]
[1, 1]
[1, 1, 1]
程序保存哪些变量[1],[1,1],[1,1,1]? 执行该功能后,为什么计算机不清除分配给该功能的内存?
我也尝试了以下功能
def func(x=2):
print 'at the beginning of the function x = ', x
x=x+1
print 'after the value is changed x = ', x
return x
func()
func()
func()
输出如下:
at the beginning of the function x = 2
after the value is changed x = 3
at the beginning of the function x = 2
after the value is changed x = 3
at the beginning of the function x = 2
after the value is changed x = 3
事实证明这个功能正常运行。 func和func2之间的主要区别是什么?