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 ncoghlan
Recipients daniel.urban, docs@python, eric.araujo, ncoghlan, r.david.murray, terry.reedy
Date 2011-06-25.00:32:08
SpamBayes Score 5.8441586e-08
Marked as misclassified No
Message-id <1308961929.27.0.494373911788.issue12374@psf.upfronthosting.co.za>
In-reply-to
Content
The other directly relevant part is in section 5:

5.1.3 Evaluating Default Arguments
In static languages such as C, function definitions are instructions to the compiler rather than executable statements. This leads to such languages making distinctions between events that occur at run-time and at compile time. In Python, function definitions are statements in their own right, so run-time events related to functions are further divided into events that occur at definition time and at execution time. Definition time refers to the point where the function is defined by the def statement. Execution time refers to the point where the function is called and the body of the function is executed.
Where these distinctions matter the most is in the evaluation of default arguments, as this occurs only once at the time the function is defined. For immutable default values like numbers or the constant value None, this doesn't have any perceptible effect. For mutable defaults like lists and dictionaries, however, it makes a significant difference, as it means a single instance of the object is created at definition time, and is then shared across all invocations of the function that use the default argument value. This can result in unintended side effects. The typical approach to handling this situation is to use None as the default argument, and include code in the body of the function to create the appropriate default value when the parameter is set to None. The following listing shows the difference between the two mechanisms.
[Grab the ODF file if you want to see the code listing]
History
Date User Action Args
2011-06-25 00:32:09ncoghlansetrecipients: + ncoghlan, terry.reedy, eric.araujo, r.david.murray, daniel.urban, docs@python
2011-06-25 00:32:09ncoghlansetmessageid: <1308961929.27.0.494373911788.issue12374@psf.upfronthosting.co.za>
2011-06-25 00:32:08ncoghlanlinkissue12374 messages
2011-06-25 00:32:08ncoghlancreate