On Mon, 20 Jul 2009 09:34:24 +0000, Sion Arrowsmith wrote: > Terry Reedy <tjre...@udel.edu> wrote: >>Sion Arrowsmith wrote: >>> Jack Diederich <jackd...@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 >>> True >>Try reversing the definitions of d1 and d2. The dicts are probably being >>compared by id (address), which is the 2.x CPython default. > > Like this? > >>>> d1 = dict((str(i), i) for i in range (20)) >>>> d2 = dict((str(i), i) for i in range (10)) >>>> d1 < d2 > False >>>> id(d1) < id(d2) > True > > 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.
I don't think comparisons other than equality use id. That would be rather insane. If anyone can demonstrate such comparisons in a built-in or standard library class, I'd like to see it. For the record, dicts have been comparable with < and > since at least Python 1.5: $ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> >>> {1: 'a', 2: 'b'} < {2: 'b', 3: 'a'} 1 Reading the docs: http://docs.python.org/library/stdtypes.html#comparisons http://docs.python.org/library/stdtypes.html#mapping-types-dict I can only suggest that dicts compare in an arbitrary but consistent fashion. What that is based on, I don't know, but based on some very limited testing, I'd guess it is like this: If the two dicts have unequal length, the longer dict compares greater than the shorter dict. If the two dicts are equal in length, (key, item) pairs are compared, probably in lexicographic order, because the order of insertion of keys doesn't appear to matter. -- Steven -- http://mail.python.org/mailman/listinfo/python-list