重複の可能性:
Python の「最小の驚き」: 変更可能な既定の引数
この回答にあるコードをいじってみると、次のような奇妙な点が見つかりました。これが私のクラス宣言であり、まったく自明な MutableSequence サブクラスです。
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)
さて、奇妙な点は次のとおりです。
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>
インスタンスがプロパティを共有しているように見えるのはなぜですか?