考虑这个例子,其中类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