Why doesn't OrderedDict use super?
<p>We can create an <code>OrderedCounter</code> trivially by using multiple inheritance:</p>

<pre><code>&gt;&gt;&gt; from collections import Counter, OrderedDict
&gt;&gt;&gt; class OrderedCounter(Counter, OrderedDict): 
...     pass
...
&gt;&gt;&gt; OrderedCounter('Mississippi').items()
[('M', 1), ('i', 4), ('s', 4), ('p', 2)]
</code></pre>

<p>Correct me if I'm wrong, but this crucially relies on the fact that <a href="https://github.com/python/cpython/blob/761d139852063b9aa3caf58023184c4a399d594f/Lib/collections/__init__.py#L533" rel="nofollow"><code>Counter</code> uses <code>super</code></a>:</p>

<pre><code>class Counter(dict):
    def __init__(*args, **kwds):
        ...
        super(Counter, self).__init__()
        ...
</code></pre>

<p>That is, the magic trick works because </p>

<pre><code>&gt;&gt;&gt; OrderedCounter.__mro__
(__main__.OrderedCounter,
 collections.Counter,
 collections.OrderedDict,
 dict,
 object)
</code></pre>

<p>The <code>super</code> call must delegate according to 'siblings before parents' rule of the <a href="https://www.python.org/download/releases/2.3/mro/" rel="nofollow">mro</a>, whence the custom class uses an <code>OrderedDict</code> as the storage backend.  </p>

<p>However a colleague recently pointed out, to my surprise, that <code>OrderedDict</code> <a href="https://github.com/python/cpython/blob/761d139852063b9aa3caf58023184c4a399d594f/Lib/collections/__init__.py#L107-L119" rel="nofollow">doesn't</a> use super: </p>

<pre><code>def __setitem__(self, key, value,
                dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
    ... 
    # &lt;some weird stuff to maintain the ordering here&gt;
    dict_setitem(self, key, value)
</code></pre>

<p>At first I thought it could be because <code>OrderedDict</code> came first and Raymond didn't bother to change it later, but it seems that <code>super</code> predates <code>OrderedDict</code>.  </p>

<p><strong>Why does <code>OrderedDict</code> call <code>dict.__setitem__</code> explicitly?</strong></p>

<p>And why does it need to be a kwarg?  Doesn't this cause trouble when using <code>OrderedDict</code> in diamond inheritance situations, since it passes directly to the parent class instead of delegating to the next in line in the mro?</p>

<p>It's a microoptimization. Looking up a <code>dict_setitem</code> argument is slightly faster than looking up <code>dict.__setitem__</code> or <code>super().__setitem__</code>.</p>

<p>This might cause problems with multiple inheritance if you have another class that overrides <code>__setitem__</code>, but <code>OrderedDict</code> isn't designed for that kind of diamond-structured method overriding anyway. For <code>OrderedDict</code> to support that, it'd have to make very careful guarantees about what another class's methods might see if they try to index the <code>OrderedDict</code> while the ordering information is inconsistent with the dict structure. Such guarantees would be way too messy to make.</p>

python oop multiple-inheritance super python-collections