Python 3.0 - is this true?
Roy Smith
roy at panix.com
Sun Nov 9 10:49:04 EST 2008
In article <6nob2mFmds4kU1 at mid.individual.net>,
Duncan Booth <duncan.booth at invalid.invalid> 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()
Sigh. So if I really wanted to do that, I'd be forced to write
"str(type(o1)) < str(type(o2))"? To me, that sounds suspiciously like
"sudo type(o1) < type(o2)" :-)
More information about the Python-list
mailing list