5. Mutable default arguments
This one bites beginners over and over again. It's really a variant of #2, combined with unexpected behavior of default arguments. Consider this function:
>>> def popo(x=[]): ... x.append(666) ... print x ... >>> popo([1, 2, 3]) [1, 2, 3, 666] >>> x = [1, 2] >>> popo(x) [1, 2, 666] >>> x [1, 2, 666]
This was expected. But now:
>>> popo() [666] >>> popo() [666, 666] >>> popo() [666, 666, 666]
Maybe you expected that the output would be [666] in all cases... after all, when popo() is called without arguments, it takes [] as the default argument for x, right? Wrong. The default argument is bound *once*, when the function is *created*, not when it's called. (In other words, for a function
f(x=[])
, x is *not* bound whenever the function is called. x
got bound to [] when we defined f
, and that's it.) So if it's a mutable object, and it has changed, then the next function call will take this same list (which has different contents now) as its default argument.Solution: This behavior can occasionally be useful. In general, just watch out for unwanted side effects.
No comments:
Post a Comment