Issue 11333: Add empty slots to collections.abc abstract base classes (original) (raw)
Currently instances of classes which inherit an ABC in collections.abc will have a dict. This can be a problem for example a tree-like data structure. It would make sense to inherit for example MutableMapping, but that would possibly mean, that every node in the tree would have a dict, which is probably a waste of memory.
A workaround for this problem is not inheriting the ABC, and using ABCMeta.register (and optionally adding the mixin methods explicitly), but this feels like a hack.
The attached patch adds an empty slots to the ABCs in collections.abc. I excluded the mapping views (MappingView, KeysView, ItemsView and ValuesView), because they can't have an empty slots, and I think using a nonempty slots possibly can cause problems in some cases with multiple inheritance.
I like the idea, and it seems to work as expected (i.e. an empty slots doesn't conflict with inheritance from a C defined type or a type with non-empty slots).
However, slots is one of the sections of the type machinery I'm least familiar with, so hopefully Guido will weigh in.