[New-bugs-announce] [issue8762] default value in constructor not unique across objects

andrew report at bugs.python.org
Wed May 19 02:35:35 CEST 2010


New submission from andrew <atcuno at gmail.com>:

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 at x:~# python pythonbug.py
['apples', 'oranges']
['apples', 'oranges']
135923500
135923500
root at 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)

----------
messages: 106018
nosy: bolt
priority: normal
severity: normal
status: open
title: default value in constructor not unique across objects
type: behavior
versions: Python 2.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8762>
_______________________________________


More information about the New-bugs-announce mailing list