[Python-ideas] Make dict customisation easier

Simon Sapin simon.sapin at kozea.fr
Sun Jun 17 00:41:27 CEST 2012

Le 17/06/2012 00:28, Laurie Clark-Michalek a écrit :
> Eventually he was directed to dir(dict), and he seemed to accept that he
> would have to wrap most of the methods of the dict builtin. This seemed
> like the worse solution to me, and I couldn't see any real reason why
> python couldn't either defer to user implemented __getitem__ and
> __setitem__, or provide an alternative dict implementation that did
> allow easy customisation.
> I realise that python dicts are fairly high performance structures, and
> that checking for a custom implementation might have an unacceptable
> impact for a solution to what might be seen as a minor problem. Still, I
> think it is worth the effort to clean up what seems to me to be a slight
> wart on a very fundamental type in python.


The MutableMapping class in the collections module has default 
implementations for many methods, based on a few basic method. I think 
that inheriting from it and adding __len__, __iter__, __getitem__, 
__setitem__ and __delitem__ should be enough.

Then you can override more methods for performance, but the defaults 
should be correct and consistent.

Simon Sapin

More information about the Python-ideas mailing list