2

A subclass that subclasses ABC and OrderedDict does not act as a true abstract class:

>>> from abc import ABC, abstractmethod
>>> from collections import OrderedDict
>>> class AbstractOrderedDict(OrderedDict, ABC):
...     @abstractmethod
...     def abstract_method(self):
...         pass
... 
>>> AbstractOrderedDict()
AbstractOrderedDict()

One would expect an instantiation of AbstractOrderedDict to fail, right?

Further experimentation showed that subclassing object and ABC raises a TypeError on instantiation, as expected (obviously), but subclassing any other basic class (dict, int, list, etc.) does not (behaves as above).

Questions:

  1. Why does it behave this way?
  2. Is it a bad idea, in theory, to make an abstract subclass of OrderedDict (or others)?
    1. Why?
    2. Alternatives?
Asker
  • 105
  • 8
  • I have just now created a similar [question](https://stackoverflow.com/questions/57407909/why-doesnt-the-abc-abcmeta-abstract-instantiation-check-work-on-derivatives-of) without realizing that you had posted the same question. I am going to let that one sit because of some other info I have added. – LazyLeopard Aug 08 '19 at 08:47
  • Yours got more traction :) I'm glad you asked it and let me know here. – Asker May 22 '20 at 00:32

0 Answers0