bug or feature?

Fredrik Lundh fredrik at pythonware.com
Wed Oct 5 07:16:40 EDT 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].

it's there:

    http://www.python.org/doc/faq/general.html#why-are-default-values-shared-between-objects

(maybe "default values" should be changed to "default argument values")

it's also mentioned in chapter 4 of the tutorial:

    http://docs.python.org/tut/node6.html#SECTION006710000000000000000

     "*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
     instead)

and in the description of "def" in the language reference:

    http://docs.python.org/ref/function.html

   "*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)

</F>






More information about the Python-list mailing list