Why aren't OrderedDicts comparable with < etc?
sion at viridian.paintbox
Mon Jul 20 11:34:24 CEST 2009
Terry Reedy <tjreedy at udel.edu> wrote:
>Sion Arrowsmith wrote:
>> Jack Diederich <jackdied at gmail.com> wrote:
>>> It isn't an OrderedDict thing, it is a comparison thing. Two regular
>>> dicts also raise an error if you try to LT them.
>> Python 2.5.2
>>>>> d1 = dict((str(i), i) for i in range (10))
>>>>> d2 = dict((str(i), i) for i in range (20))
>>>>> d1 < d2
>Try reversing the definitions of d1 and d2. The dicts are probably being
>compared by id (address), which is the 2.x CPython default.
>>> d1 = dict((str(i), i) for i in range (20))
>>> d2 = dict((str(i), i) for i in range (10))
>>> d1 < d2
>>> id(d1) < id(d2)
I didn't know that comparison for anything other than equality
defaulted to using id. That really is rather broken, and I'm
glad 3.0 fixed it.
More information about the Python-list