Why doesn't OrderedDict use super?

We can create OrderedCountertrivially using multiple inheritance:

>>> from collections import Counter, OrderedDict
>>> class OrderedCounter(Counter, OrderedDict): 
...     pass
...
>>> OrderedCounter('Mississippi').items()
[('M', 1), ('i', 4), ('s', 4), ('p', 2)]

Correct me if I am wrong, but it really depends on what uses : Countersuper

class Counter(dict):
    def __init__(*args, **kwds):
        ...
        super(Counter, self).__init__()
        ...

That is, the magic trick works because

>>> OrderedCounter.__mro__
(__main__.OrderedCounter,
 collections.Counter,
 collections.OrderedDict,
 dict,
 object)

The call supermust be delegated according to the "siblings before parents" mro rule , from where the user class uses OrderedDictas storage.

However, recently, my colleague noticed that he was OrderedDict not using :

def __setitem__(self, key, value,
                dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
    ... 
    # <some weird stuff to maintain the ordering here>
    dict_setitem(self, key, value)
At first I thought it was possible, because I OrderedDictcame first, and Raymond did not bother to change it later, but it seems to superprecede OrderedDict.

OrderedDict dict.__setitem__?

kwarg? OrderedDict , mro?

+4
1

. dict_setitem , dict.__setitem__ super().__setitem__.

, , __setitem__, OrderedDict , - . OrderedDict , , OrderedDict, dict. .

0

Source: https://habr.com/ru/post/1654506/


All Articles