collections.MutableSequence subclass appears to be singleton?

58 Views Asked by At

Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument

Playing around with code found in this answer, I found the following weirdness. Here's my class declaration, a totally trivial MutableSequence subclass:

import collections
class DescriptorList(MutableSequence):
    def __init__(self, items=[]):
        super(DescriptorList, self).__init__()
        self.l = items

    def __len__(self):
        return len(self.l)

    def __getitem_(self, index):
        return self.l[index]

    def __setitem__(self, index, value):
        self.l[index] = value

    def __delitem__(self, index):
        del self.l[index]

    def insert(self, index, value):
        self.l.insert(index, value)

Now, here's the weirdness:

Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import DescriptorList
>>> b=DescriptorList()
>>> c=DescriptorList
>>> c=DescriptorList()
>>> c.append(5)
>>> b[:]
[5]
>>> b.append(6)
>>> c[:]
[5, 6]
>>> c
<DescriptorList object at 0x100ccfc50>
>>> b
<DescriptorList object at 0x100a32550>

Why are the instances appearing to share properties?

0

There are 0 best solutions below