我在网上偶然发现了这些有趣的代码:
http://code.activestate.com/recipes/66531/
class Borg:
__shared_state = {}
def __init__(self):
self.__dict__ = self.__shared_state
# and whatever else you want in your class -- that's all!
我理解什么是单例,但我不理解剪切的特定代码。你能解释一下"__shared_state"是如何/在哪里更改的吗?
我在ipython:中尝试过
In [1]: class Borg:
...: __shared_state = {}
...: def __init__(self):
...: self.__dict__ = self.__shared_state
...: # and whatever else you want in your class -- that's all!
...:
In [2]: b1 = Borg()
In [3]: b2 = Borg()
In [4]: b1.foo="123"
In [5]: b2.foo
Out[5]: '123'
In [6]:
但不能完全理解这是怎么发生的。
因为类的实例的__dict__
被设置为等于__share_state
dict。它们指向同一对象。(Classname.__dict__
保存所有类属性)
当你这样做:
b1.foo = "123"
您正在修改b1.__dict__
和Borg.__shared_state
都引用的dict
。
在实例化任何对象后调用的__init__
方法将新创建对象的__dict__
属性替换为类属性__shared_state
。
a.__dict__
、b.__dict__
和Borg._Borg__shared_state
都是相同的对象。注意,当从类外部访问私有属性时,我们必须添加隐式前缀_Borg
。
In [89]: a.__dict__ is b.__dict__ is Borg._Borg__shared_state
Out[89]: True
实例是单独的对象,但通过将其__dict__
属性设置为相同的值,实例具有相同的属性字典。Python使用属性字典来存储对象上的所有属性,因此实际上这两个实例的行为方式相同,因为对其属性的每次更改都是对共享属性字典进行的。
然而,如果使用is
来测试相等性(浅相等性),对象仍然会比较不相等,因为它们仍然是不同的实例(很像博格无人机,它们共享思想,但物理上不同)。