How to subclass an OrderedDict?

我们两清 提交于 2019-12-04 09:55:45

问题


Subclassing a Python dict works as expected:

>>> class DictSub(dict):
...     def __init__(self):
...         self[1] = 10
...         
>>> DictSub()
{1: 10}

However, doing the same thing with a collections.OrderedDict does not work:

>>> import collections
>>> class OrdDictSub(collections.OrderedDict):
...     def __init__(self):
...         self[1] = 10
...         
>>> OrdDictSub()
(…)
AttributeError: 'OrdDictSub' object has no attribute '_OrderedDict__root'

Thus, the OrderedDict implementation uses a private __root atribute, which prevents the subclass OrdDictSub from behaving like the DictSub subclass. Why? How can one inherit from an OrderedDict?


回答1:


You need to invoke OrderedDict.__init__ from your __init__:

class OrdDictSub(collections.OrderedDict):
    def __init__(self):
        super(OrdDictSub, self).__init__()

You haven't given OrderedDict a chance to initialize itself. Technically, you want to do this for your dict subclass as well, since you want a fully initialized dict. The fact that dict works without it is just luck.




回答2:


Try initializing the superclass in the __init__ method:

def __init__(self):
    collections.OrderedDict.__init__(self)
    self[1] = 10

This is the normal way to initialize a subclass. You don't have to call the superclass's __init__ method in general, but if you have no knowledge of the superclass's implementation you really should call __init__.



来源:https://stackoverflow.com/questions/11174702/how-to-subclass-an-ordereddict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!