简体   繁体   中英

Comparison ordering of python built-in types

I'm looking for a good set of sentinel values, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM