|
|
|
|
|
by darkfirefly
1502 days ago
|
|
Python's default arguments are evaluated and stored once. This can cause issues, for example the naive "default to []" program: >>> def function(b, a=[]):
... a.append(b)
... print(a)
...
...
>>> function(1)
[1]
>>> function(2)
[1, 2]
>>> function(3, [4])
[4, 3]
>>> function(5)
[1, 2, 5]
A correct implementation would be: >>> def function2(b, a=None):
... if a is None: a = []
... a.append(b)
... print(a)
|
|