a problem with subclassing a dict
David Eppstein
eppstein at ics.uci.edu
Sat Apr 6 19:25:56 EST 2002
In article <a8o0ro$1qoa at r02n01.cac.psu.edu>,
Rajarshi Guha <rxg218 at psu.edu> wrote:
> I was playing with some code from a tutorial by GvR
...
> Traceback (most recent call last):
> File "p2.py", line 22, in ?
> print md[2]
> KeyError: 2
Your modified __getitem__ function appears not to be called even for the
successful accesses (e.g. if you put a print statement before the try,
it never gets printed). The tutorial mentions a problem like this for
local and global scope dictionaries, but doesn't say that it happens for
other kinds of dictionaries too...
Someone more expert than I can tell you why this doesn't work, but
if you really need to define your own dictionary-lookalike class, you
might also want to look at
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117236>
--
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/
More information about the Python-list
mailing list