[Python-Dev] Creating dicts from dict subclasses (original) (raw)

Armin Rigo arigo at tunes.org
Thu Dec 14 08:41:34 CET 2006


Hi Walter,

On Wed, Dec 13, 2006 at 05:57:16PM +0100, Walter D?rwald wrote:

I tried to reimplement weakref.WeakValueDictionary as a subclass of dict. The test passes except for one problem: To compare results testweakref.py converts a weakdict to a real dict via dict(weakdict). This no longer works because PyDictMerge() does a PyDictCheck() on the argument and then ignores all overwritten methods. (The old version worked because UserDict.UserDict was used).

This is an instance of a general problem in Python: if you subclass a built-in type, then your overridden methods may or may not be used in various situations. In this case you might have subtle problems with built-in functions and statements that expect a dict and manipulate it directly, because they will see the underlying dict structure. It is also quite fragile: e.g. if a future version of CPython adds a new method to dicts, then your existing code will also grow the new method automatically - but as inherited from 'dict', which produces quite surprizing results for the user.

for key in iter(arg.keys()): self[key] = arg.getitem(key)

Why can't we use: for key in iter(arg): self[key] = arg.getitem(key)

The latter would allow 'arg' to be a sequence instead of a mapping. It may even not crash but produce nonsense instead, e.g. if 'arg' is a list of small integers. Moreover there are multiple places in the code base that assume that mappings are "something with a 'keys' and a 'getitem'", so I suppose any change in that should be done carefully.

A bientot,

Armin



More information about the Python-Dev mailing list