question about subclassing dict

Russell E. Owen owen at astrono.junkwashington.emu
Mon Feb 4 20:31:24 EST 2002


In article <mailman.1012623183.14665.python-list at python.org>,
 "Jason Orendorff" <jason at jorendorff.com> wrote:

>Russell E. Owen wrote:
>> I started by defining __iter__. I was then pleased to find that the 
>> methods iterkeys, itervalues and iteritems all worked just fine.
>
>They don't seem to return the objects in the right order (which
>I gather is the whole point):

Thanks for the heads-up. I must have done the testing manually (I 
certainly did the testing somewhere), but probably on a list of only 
three elements, which is hardly a definitive test. I was planning
to override the other methods after asking here if it was necessary.

I've added the missing methods for the iter... methods using your nice 
generator code. I hadn't realized it was quite that trivial. (I first 
implementing iterator classes, but this was just messy enough that I 
decided to qeury the newsgroup before actually using them, hence my 
original posting.)


Laura Creighton pointed out that another ordered dictionary is available 
from <http://newcenturycomputers.net/projects/rbtree.html>. It is based 
on a tree structure and I suspect scales better to large dictionaries. 
In any case, it is well worth a look. Based on it I realized I was 
missing the copy method.


I've put the revised version (including iter..., copy and __str__ 
methods and improved testing code) up as OrderedDict.py at 
<http://www.astro.washington.edu/owen/> in case anybody is interested. 

Thank you both very much for the helpful replies!

-- Russell



More information about the Python-list mailing list