子类化 Pythondict按预期工作:
>>> class DictSub(dict):
... def __init__(self):
... self[1] = 10
...
>>> DictSub()
{1: 10}
但是,用 a 做同样的事情是collections.OrderedDict行不通的:
>>> import collections
>>> class OrdDictSub(collections.OrderedDict):
... def __init__(self):
... self[1] = 10
...
>>> OrdDictSub()
(…)
AttributeError: 'OrdDictSub' object has no attribute '_OrderedDict__root'
因此,OrderedDict 实现使用了一个私有__root属性,它可以防止子类OrdDictSub表现得像DictSub子类。为什么?如何从 OrderedDict 继承?