What is the difference between a MappingView container and a Sized container? Any examples on how to implement a MappingView container?
I might be misunderstanding ABCs and the docs entirely, but a MappingView container is any container that inherits from Sized, right? If so, then why doesn't my dummy example work?
import collections
class MySized:
def __len__():
pass
class MyMappingView(MySized):
pass
print(issubclass(MySized, collections.Sized)) # True
print(issubclass(MyMappingView, collections.MappingView)) # False
Because you don't subclass from it (virtually or otherwise).
MappingViewis used as a base class for thekeys/items/valuesviews.Sizedis used for any object for which getting its size (len) makes sense.These
ABCs are used to check if certain objects in Python follow the interface they define. Instead of creating a customMappingViewyou can justregisterany new classes that conform to it.Suffice to say, you wouldn't
registera class toMappingViewsince it used solely as a base class to provide a default__len__,__repr__and__init__toKeys/Values/Itemsviews. If you have defined a new mapping type that returns custom views, simply register these views to the appropriateKeys/Values/Itemsviews.