[Python-Dev] Creating dicts from dict subclasses (original) (raw)
Guido van Rossum guido at python.org
Wed Dec 13 23:26:17 CET 2006
- Previous message: [Python-Dev] Creating dicts from dict subclasses
- Next message: [Python-Dev] Creating dicts from dict subclasses
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 12/13/06, Walter Dörwald <walter at livinglogic.de> wrote:
Guido van Rossum wrote: > On 12/13/06, Walter Dörwald <walter at livinglogic.de> 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). >> >> The simplest solution is to replace the PyDictCheck() call with >> PyDictCheckExact(), but this might slow things down too much, because >> the fallback code basically does: >> >> 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) >> >> instead? > > The only reason I can think of is backwards compatibility: not all > "mappings" created pre-2.2 would support iteration. Maybe you could > check for a tpiter slot and if non-NULL use the latter otherwise use > the original fallback?
This doesn't seem to work. It breaks testupdate() in testdict.py which does this: d = {} class SimpleUserDict: def init(self): self.d = {1:1, 2:2, 3:3} def keys(self): return self.d.keys() def getitem(self, i): return self.d[i] d.update(SimpleUserDict()) self.assertEqual(d, {1:1, 2:2, 3:3}) This fails with KeyError: 0 because SimpleUserDict doesn't implement iter, so it gets an iterator implementation via getitem. So maybe this only makes sense for Python 3.0 where we can demand that dict-like classes implement iter?
Ah, right. But I think you should still use PyDict_CheckExact, and slow fallbacks be damned. (I guess you could look for iterkeys first.)
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] Creating dicts from dict subclasses
- Next message: [Python-Dev] Creating dicts from dict subclasses
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]