Вопрос

I'm looking for a good set of sentinel values, e.g. experimentally:

[] > "sdfasfg" > ()

And this seems to be documented:

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. reference

In the same fashion,

object() < str()

The "largest" builtin type seems to be unicode, what can I use as a "right" sentinel value for comparison with unicode types? That is a value that's larger than any unicode string?

Is comparison order of instances of built-in types really well defined in Python?

Это было полезно?

Решение

The comparison of arbitrary objects is called "rich comarisons". There is some documentation here, and here.

If you are working with Python 3, then check this out. Relevant quote from this source:

Having both the rich comparison methods and the __cmp__() method violates the principle that there should be only one obvious way to do it, so in Python 3 the support for __cmp__() has been removed. For Python 3 you therefore must implement all of the rich comparison operators if you want your objects to be comparable.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top