Python 3.0 - is this true?
Roy Smith
roy at panix.com
Sun Nov 9 07:57:28 EST 2008
In article <mailman.3701.1226203224.3487.python-list at python.org>,
Terry Reedy <tjreedy at udel.edu> wrote:
> Yes, key= lets you sort anything anyway you want.
> >>> l=[1, '2', 3j]
> >>> sorted(l, key = str)
> [1, '2', 3j]
The problem here is that str() is degenerate, i.e. a != b does not imply
str(a) != str(b).
> >>> sorted(l, key = id)
> ['2', 3j, 1]
And of course, this has to opposite problem, a == b does not imply id(a) ==
id(b). Whether either of these "problems" is really a problem obviously
depends on what you're trying to do.
In 3.0, can you still order types? In 2.x, you can do:
>>> t1 = type(1)
>>> t2 = type(1j)
>>> t1 < t2
False
If this still works in 3.0, then you can easily do something like:
def total_order(o1, o2):
"Compare any two objects of arbitrary types"
try:
return o1 <= o2
except UncomparableTypesError: # whatever the right name is
return type(o1) <= type(o2)
and get the same effect as you had in 2.x.
More information about the Python-list
mailing list