Is it possible to craft a Python dict with all (or most) of the properties of a dict with Abstract Base Classes?

StackOverflow https://stackoverflow.com/questions/21360937

  •  02-10-2022
  •  | 
  •  

Question

I am familiar with the concept of Abstract Base Classes (ABC's), as providing sets of properties of the builtin objects, but I don't have really any experience working with them. I can see that there's a Mapping ABC, and a MutableMapping that inherits from it, but I don't see a .fromkeys() method (the only thing missing off the top of my head.)

Would I be able to craft a dict with purely ABC's? What would that look like? Would that amount to nearly the same thing as subclassing dict? Would there be any benefit to doing that? What would be the use-case?

Was it helpful?

Solution

Would I be able to craft a dict with purely ABC's?

No. Subclassing an ABC requires you to implement its interface; for example, Mapping requires you to implement __getitem__, __iter__, and __len__. The mixin methods provide default implementations for certain things in terms of the parts you need to implement, but you still need to provide the core. Mapping won't automatically provide a hash table or BST implementation for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top