對於類A,爲什麼在對象和對象b之間共享一個成員變量映射?爲什麼在對象之間共享python關聯映射成員變量
>>> class A:
... aMap = {}
>>> a = A()
>>> a.aMap["hello"] = 1
>>> b = A()
>>> b.aMap["world"] = 2
>>> c = []
>>> c.append(a)
>>> c.append(b)
>>> for i in c:
... for j in i.aMap.items():
... print j
('world', 2)
('hello', 1)
('world', 2)
('hello', 1)
我相信http://stackoverflow.com/questions/1132941/least-astonishment- in-python-the-mutable-default-argument解釋了這一點。 – aem 2010-02-19 21:47:53
[我如何避免在實例間共享Python類數據?](http://stackoverflow.com/questions/1680528/how-do-i-avoid-having-python-class-data-shared-among - 物質) – 2013-02-23 15:41:45