Python のサブクラス化はdict期待どおりに機能します。
>>> 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するのを防ぎます。なんで?OrderedDict からどのように継承できますか?OrdDictSubDictSub