34

I wanted to create a class out of two: collections.OrderedDict and collections.DefaultDict. So that I can get an ordered dictionary and have a default value for non existing keys being accessed. Whats are some ways to do this?

My solution was to create another class around the 2 classes I stated above. This makes an error due to a method in each class having the same name I think?

from collections import defaultdict, OrderedDict
class owndic(OrderedDict, defaultdict):
    pass

producing

TypeError: multiple bases have instance lay-out conflict

Cheers!

Mazdak
  • 105,000
  • 18
  • 159
  • 188
user6046760
  • 454
  • 1
  • 4
  • 6
  • What version of Python are you using? I can't reproduce the error on Python 3.4.2 – DeepSpace Jan 07 '18 at 09:57
  • Regardless of the cause of your `TypeError` if you want to use both functionalities you better use `OrderedDict` and it's `setdefault()` attribute which has a same functionality as `defaultdict`. – Mazdak Jan 07 '18 at 09:58
  • The error seems to be because both `defaultdict` and `OrderedDict` inherit from built-in `dict` type (their only base type) and this not allowed in python. – Mazdak Jan 07 '18 at 10:10
  • Its python 3 in hackerrank - Doesn't say the exact version. Lovely I will try `setdefault` thanks! – user6046760 Jan 07 '18 at 10:16

1 Answers1

42

Instance lay-out conflict is a fancy way of saying that you're trying to inherit from multiple--in this case built-in--types that cannot cooperate with each other quite well. More technically these classes have conflict in functionality of their common attributes.

In this case both OrderedDict and defaultdict are two dictionary-like types with their own unique __setitem__ attributes and also different ways of handling the keys and values.

At C level this error happens when the best_base function fails to calculate the best base amongst multiple base classes. There are multiple other reasons that can cause this function fail to calculate the best base or winner among other bases but in this case the error occurs when neither of following conditions happen. winner is not None, winner is not a subtype of candidate*, candidate is not a subtype of winner.

candidate = solid_base(base_i);
if (winner == NULL) {
    winner = candidate;
    base = base_i;
}
else if (PyType_IsSubtype(winner, candidate))
    ;
else if (PyType_IsSubtype(candidate, winner)) {
    winner = candidate;
    base = base_i;
}
else {
    PyErr_SetString(
        PyExc_TypeError,
        "multiple bases have "
        "instance lay-out conflict");
    return NULL;

}

However, if you want to benefit from both defaultdict() and OrderedDict()'s functionalities, you can simply use an OrderedDict and its built-in setdefault attribute.

Here is an example:

In [13]: d = OrderedDict()

In [14]: for i, j in enumerate(['k', 'r', 'k', 'j', 'j', 't']):
             d.setdefault(j, []).append(i)
   ....:     

In [15]: d
Out[15]: OrderedDict([('k', [0, 2]), ('r', [1]), ('j', [3, 4]), ('t', [5])])

* `candidate` is the solid base of `base_i`,one of the bases passed to the class caller, that is calculated using [`solid_base`](https://github.com/python/cpython/blob/ce5b0e9db1b9698e6ffc43ae41cf3a22ca5a6ba6/Objects/typeobject.c#L2086) function.
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • 1
    I've got this when I started to introduce `__slots__` to `pypdf` – Martin Thoma Dec 31 '22 at 10:03
  • 1
    Got into this issue when I tried to give, `class D(C,B)` and `class C(A)` and `class B(A)` all `__slots__` including `class A`. I could resolve it by changing the `mro` so that it turned to be `class D(C)` and `class C(B)` and `class B(A)`. This way I was able to implement `__slots__` or I had to drop `__slots__` in `class A`. – Thingamabobs Jun 11 '23 at 09:15
  • @Thingamabobs Interesting! – Mazdak Jun 11 '23 at 11:44