考慮這個例子,其中類A
的所有實例的__dict__
將指向全局字典shared
。如何在覆蓋其類的__dict__屬性後訪問實例字典?
shared = {'a': 1, 'b': 2}
class A(object):
def __init__(self):
self.__dict__ = shared
現在讓我們來測試的幾件事情:
>>> a = A()
>>> b = A()
>>> a.a, a.b, b.a, b.b
(1, 2, 1, 2)
>>> b.x = 100
>>> shared
{'a': 1, 'x': 100, 'b': 2}
>>> a.x
100
>>> c = A()
>>> c.a, c.b, c.x
(1, 2, 100)
>>> shared['foo'] = 'bar'
>>> a.foo, b.foo, c.foo
('bar', 'bar', 'bar')
>>> a.__dict__, b.__dict__, c.__dict__
({'a': 1, 'x': 100, 'b': 2, 'foo': 'bar'},
{'a': 1, 'x': 100, 'b': 2, 'foo': 'bar'},
{'a': 1, 'x': 100, 'b': 2, 'foo': 'bar'}
)
全部按預期工作。
現在,讓我們添加一個名爲__dict__
屬性調整A
有點類。
shared = {'a': 1, 'b': 2}
class A(object):
__dict__ = None
def __init__(self):
self.__dict__ = shared
讓我們再次運行同一組的步驟:
>>> a = A()
>>> b = A()
>>> a.a, a.b, b.a, b.b
AttributeError: 'A' object has no attribute 'a'
>>> b.x = 100
>>> shared
{'a': 1, 'b': 2}
>>> b.__dict__ # What happened to x?
{'a': 1, 'b': 2}
>>> a.x
AttributeError: 'A' object has no attribute 'x'
>>> c = A()
>>> c.a, c.b, c.x
AttributeError: 'A' object has no attribute 'a'
>>> shared['foo'] = 'bar'
>>> a.foo, b.foo, c.foo
AttributeError: 'A' object has no attribute 'foo'
>>> a.__dict__, b.__dict__, c.__dict__
({'a': 1, 'b': 2, 'foo': 'bar'},
{'a': 1, 'b': 2, 'foo': 'bar'},
{'a': 1, 'b': 2, 'foo': 'bar'}
)
>>> b.x # Where did this come from?
100
基於以上信息,第一種情況和預期一樣,但第二個也沒有,所以我想知道在添加類級別__dict__
屬性後發生了什麼變化。我們現在可以以任何方式訪問正在使用的實例字典嗎?
你不應該加用開頭和結尾這是留給系統定義的名稱雙下劃線屬性。請參閱[**保留的標識符類**](https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers)。 – martineau
@martineau我意識到這一點。但這不是這個問題的關鍵。 –
所以你想知道一個解決方案,因爲你沒有遵循指導原則(因爲某種原因存在)而被破壞了。 – martineau