In Python, you can set default values for parameters, like so:
The result of which is a function you can call either with or without a parameter. This seems simple enough, and works very nicely. However what if you want to use something more complex as the default parameter? Say, a list?
This works just fine, exactly like the previous example. But one thing that may or may not immediately be obvious is every call to this function without an explicit parameter will share the same list. Python is smart enough not to re-parse the default parameters ever time the function is called, meaning that only one empty list is ever created in defining or using
func(). Usually this has no effect on the programmer - if your default values are primitive or immutable types, which the vast majority of default values likely are, there is no loss to the function sharing the value between different calls - it won't be changed. And even when mutable types are used, it's rare to gain any benefit from changing a mutable data structure the rest of the program cannot access, and as such most cases of mutable types as default parameters are used simply for accessing, not mutating, the data.
But one example where that is not the case is in a constructor. Consider:
When constructing a BlogPost object, I don't want to take the time to explicitly tell it there are no comments, so I would usually not even bother populating that parameter. On its own, this isn't enough to get me into trouble. Neither is creating multiple BlogPosts, nor is accessing, changing, or otherwise working with a BlogPost dangerous. And so you could go quite some time developing before you find yourself in a situation where you have constructed multiple BlogPost objects and where you update the comments array of one of them. As soon as you do that, however, all other existing BlogPost objects will also have the same comment in their comments list, because it's actually the same list!
Once you think about it this behavior is not terribly shocking - you might even go so far as to say it's intuitive. However if you aren't thinking about it, it's a very bizarre problem to debug when it happens to you.
Post a Comment