I ran the following small code
def func2(xxx=[]):
xxx.append(1)
print xxx
return None
func2()
func2()
func2()
The output is the following:
[1]
[1, 1]
[1, 1, 1]
Which variable does the program save those lists [1], [1,1],[1,1,1] in? Why doesn't the computer clear the memory allocated to the function after the function is executed?
I also tried the following function
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()
The output is the following:
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
And it turns out that this function performs normally. What's the major difference between func and func2?