bug or feature?
fredrik at pythonware.com
Wed Oct 5 13:16:40 CEST 2005
Steve Holden wrote:
> Interestingly I couldn't find this in the FAQ, though it *is* a
> frequently-asked question [note: my not finding it doesn't guarantee
> it's not there].
(maybe "default values" should be changed to "default argument values")
it's also mentioned in chapter 4 of the tutorial:
"*Important warning*: The default value is evaluated only once. This
makes a difference when the default is a mutable object such as a list,
dictionary, or instances of most classes. "
(the text then illustrates this with examples, and shows how to do things
and in the description of "def" in the language reference:
"*Default parameter values are evaluated when the function definition
is executed*. This means that the expression is evaluated once, when the
function is defined, and that that same "pre-computed" value is used for
each call. This is especially important to understand when a default para-
meter is a mutable object, such as a list or a dictionary: if the function
modifies the object (e.g. by appending an item to a list), the default
value is in effect modified."
(the text then shows how to do things instead)
More information about the Python-list