Immutable view classes - inherit from dict or from Mapping?

Peter Otten __peter__ at web.de
Tue Apr 13 08:47:50 EDT 2021


On 12/04/2021 18:01, Andreas R Maier wrote:
> Hi,
> I have written some classes that represent immutable views on collections (see "immutable-views" package on Pypi).
> 
> Currently, these view classes inherit from the abstract collection classes such as Mapping, Sequence, Set. However, they implement the read-only methods of dict, list and set, which provides some more methods compared to the abstract collection classes. For example, class "dict" provides copy() or __reversed__() and the newer OR-operator methods all of which are not defined on the abstract class "Mapping".
> 
> Note that the view classes do not provide any of the modifying methods of "dict" and "list", because after all the views are supposed to be immutable.
> 
> My question is, should the dict view class inherit from Mapping or from dict, and likewise, should the list view class inherit from Sequence or from list?

It's easy to add methods, and hard to remove them -- therefore I'd 
inherit from Mapping rather than dict.

As to the | operator, maybe it could be added to Mapping?




More information about the Python-list mailing list