This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ysj.ray
Recipients bolt, r.david.murray, ysj.ray
Date 2010-05-19.02:08:03
SpamBayes Score 0.00015951469
Marked as misclassified No
Message-id <1274234886.79.0.669424449161.issue8762@psf.upfronthosting.co.za>
In-reply-to
Content
Here is the explanation from Python Language Reference 7.6: Function Definitions
"""
When one or more top-level parameters have the form parameter = expression, the function is said to have ``default parameter values.'' For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter's default value is substituted. If a parameter has a default value, all following parameters must also have a default value -- this is a syntactic restriction that is not expressed by the grammar. 
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 parameter 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. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.: 

def whats_on_the_telly(penguin=None):
    if penguin is None:
        penguin = []
    penguin.append("property of the zoo")
    return penguin
"""
History
Date User Action Args
2010-05-19 02:08:06ysj.raysetrecipients: + ysj.ray, r.david.murray, bolt
2010-05-19 02:08:06ysj.raysetmessageid: <1274234886.79.0.669424449161.issue8762@psf.upfronthosting.co.za>
2010-05-19 02:08:05ysj.raylinkissue8762 messages
2010-05-19 02:08:03ysj.raycreate