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.

classification
Title: default value in constructor not unique across objects
Type: behavior Stage: resolved
Components: Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: bolt, r.david.murray, ysj.ray
Priority: normal Keywords:

Created on 2010-05-19 00:35 by bolt, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg106018 - (view) Author: andrew (bolt) Date: 2010-05-19 00:35
After debugging for a while I finally released that I stumbled across a Python bug (at least I believe it is). Here is a proof of concept that produces the issue:

!/usr/bin/python

class blah:

    def __init__(self, items=[]):
        self.items = items

a = blah()
b = blah()

a.items.append("apples")
b.items.append("oranges")

print a.items
print b.items
print id(a.items)
print id(b.items)


and here is the output when the program is run:

root@x:~# python pythonbug.py
['apples', 'oranges']
['apples', 'oranges']
135923500
135923500
root@x:~#

as you can see the 'items' reference is the same for both objects even though they are different objects. I checked the manual and I couldn't find anything explaining such behavior. Can this possibly be correct?

My python info:

Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48)
msg106027 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-05-19 02:05
This is not a bug, it's how Python works.  Default values are computed at function definition time, so there's only one list across all the function invocations.
msg106029 - (view) Author: ysj.ray (ysj.ray) Date: 2010-05-19 02:08
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
2022-04-11 14:57:01adminsetgithub: 53008
2010-05-19 02:08:05ysj.raysetnosy: + ysj.ray
messages: + msg106029
2010-05-19 02:05:20r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg106027

resolution: not a bug
stage: resolved
2010-05-19 00:35:32boltcreate