In this article I list out some tricks in Python programming. I will keep updating this post each time I find anything interesting.
Traps in default value
def append_list(num, res=[]):
res.append_list(num)
return res
l1 = append_list(1)
l2 = append_list(2)
l3 = append_list(3)
print(l1)
print(l2)
print(l3)
The code seem fine because every time the function itself would initialize an empty list and append a new value to it. However the result is very surprising:
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
This is because the default value, which is an empty list here keeps using the same address in the memory. Whenever value is specified for res
, it simply use that same list on the specific memory address. These three list, l1
, l2
and l3
are thus actually manipulating the same list.
The correct way to do this is as follows, which would kinda avoid this problem.
def append_list(num, res=None):
if res == None:
l3 = append_list(3)
print(l1)
print(l2)
print(l3)