Python 3.0 - is this true?
Stefan Behnel
stefan_ml at behnel.de
Sun Nov 9 10:41:04 EST 2008
Duncan Booth wrote:
> 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()
However, for an arbitrary ordering of types, id(type(o1)) <= id(type(o2)) will
work well enough.
Stefan
More information about the Python-list
mailing list