Re: bug or feature?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: bug or feature?

From: Fredrik Lundh <fredrik@pythonware.com>
Date: Wed Oct 05 2005 - 13:16:40 CEST

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>
Received on Sat Oct 15 04:10:07 2005