Barry A. Warsaw added the comment:
While it's true that it can be confusing to users, it's not a bug.
http://docs.python.org/2/reference/compound_stmts.html#function
and a nice treatise on the subject by the Effbot:
http://effbot.org/zone/default-values.htm
--
nosy: +barry
__
Eric V. Smith added the comment:
It's by design. Search for "mutable default arguments", for example
http://docs.python-guide.org/en/latest/writing/gotchas.html#mutable-default-arguments
--
nosy: +eric.smith
resolution: -> invalid
stage: -> committed/rejected
status: open -> closed
_
New submission from Poul-Henning Kamp:
I'd like to nominate this piece of code as candidate for the next round of
"Most unexpected python behaviour" awards:
def foo(a, x = []):
x.append(a)
return x
print(foo(1))
print(foo(2))
I expected the output to be:
[1]