Is there any existing implementation of Python2 where ordering is transitive? That is, where it's impossible to see this behaviour without creating user-defined types:
>>> x < y < z < x
True
CPython is not transitive because of this counterexample
x = 'b'
y = ()
z = u'ab'
However, this ordering in CPython is documented as only an implementation detail.
Every mainstream Python implementation fails in one way or another except for Skulpt, but it's arguably an incomplete implementation.
CPython (and variants), PyPy, and Jython:
IronPython:
IronPython internally compares the .NET
Object.GetHashCode()hashes of unlike objects, so you can break it by abusing the special handling ofintandfloatcomparisons and the fact that the internal hash representation offloat('+inf')is less than the hash of[](I'm not sure how stable this is, so it might not work on every installation of IronPython):CLPython
Skulpt
If you count Skulpt as a complete implementation of Python 2 (it can't compare dictionaries and a few other inconvenient types, and has no
unicodetype), it actually does work by copying CPython's rules for comparison and conveniently leaving out theunicodetype:For CPython 2, you would actually have
[t]uple < [u]nicode, but becauseunicodeandstrcomparisons are handled as a special case, you lose transitivity. Although it's unlikely that Python 2 will get a patch to fix this "bug", I think you can ensure transitivity by just explicitly changing the order from:To:
That way, the special case of
strtounicodecomparisons doesn't break anything.