I’ve been using ABCs almost since they were added to the language, but there is one point about the built-in ABCs from
collections.abc that it occurs to me has never been completely clear to me, and that does not appear to be clearly documented anywhere I can find (at least in the
collections.abc docs or in PEP 3319
The question is, in order to safely be considered a
Mapping, is it only necessary for a class to implement the required abstract methods (
__iter__), or is it also necessary to implement all of the so-called “mixin methods”?
I know that if one subclasses
Mapping, it’s sufficient to implement just the abstract methods, and then the
Mapping class provides implementations of the other methods for free.
But if I have an arbitrary class that does not subclass
Mapping, but that at a minimum implements a
__len__, and an
__iter__ (with the appropriate semantics, that it iterates over the keys), is it safe to then register it with
Mapping.register(MyMappingIshClass)? Or must it also implement
I can’t find this stated explicitly anywhere…