Python 3.0 - is this true?

Duncan Booth duncan.booth at invalid.invalid
Sun Nov 9 09:40:22 EST 2008


Roy Smith wrote:

> 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.

No, that won't work. You can compare types for equality/inequality, but
they are not orderable:

>>> type(1)==type('a')
False
>>> sorted([1, 'a'], key=lambda x:(type(x),x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: type() < type()




More information about the Python-list mailing list