Python functions with default parameters show unexpected behaviour
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. I'd like to know:
What is the mechanism for this behaviour?
What is the rationale behind this behaviour? (it seems confusing to me)
No comments:
Post a Comment