The following python code:
def test_function(a = []):
a.append('x')
print(a)
test_function()
test_function()
prints:
['x']
['x', 'x']
It seems like the a=[]
default assignment is only used once, after which a
is treated as a property of the function unless it is reassigned when the function is called again (e.g test_function(a=['hello'])
). The behaviour is shared between Python2.x and 3.x so I assume it is not perceived as a design flaw.
I'd like to know:
- What is the mechanism for this behaviour?
- What is the rationale behind this behaviour? (it seems confusing to me)