Skip to content

Instantly share code, notes, and snippets.

@doctaphred
Last active September 19, 2019 22:13
Show Gist options
  • Save doctaphred/fcc621385e3ab198c190f9bb777c1d36 to your computer and use it in GitHub Desktop.
Save doctaphred/fcc621385e3ab198c190f9bb777c1d36 to your computer and use it in GitHub Desktop.
Some surprising comparison behaviors in Python 2
Some surprising comparison behaviors in Python 2.
You can confirm these behaviors via doctest:
python2 -m doctest python2-wyd
Careful, though: some of the results change if the filename is more than
13 characters long.
>>> a, b, c = str(), tuple(), unicode()
>>> a < b
True
>>> b < c
True
>>> a < b < c
True
>>> a < b and b < c
True
>>> a < c
False
>>> int() == long() == float() == complex()
True
>>> complex() < None
False
>>> complex() < True
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: no ordering relation is defined for complex numbers
>>> (
... None
... < int()
... < NotImplemented
... < type('ayy', (), {})()
... < iter
... < bytearray()
... < dict()
... < iter({})
... < Ellipsis
... < ArithmeticError()
... < BaseException()
... < Exception()
... < ZeroDivisionError()
... < frozenset()
... < (lambda: None)
... < iter('')
... < list()
... < iter([])
... < type('lmao', (), {})()
... < object()
... < property()
... < set()
... < iter(set())
... < slice(None)
... < str()
... < tuple()
... < iter(())
... < bool
... < property
... < BaseException
... < Exception
... < ArithmeticError
... < ZeroDivisionError
... < float
... < int
... < list
... < long
... < dict
... < set
... < slice
... < str
... < tuple
... < object
... < type
... < unicode
... < unicode()
... == str() # ಠ_ಠ
... )
True
>>> iter('') < iter('')
True
>>> iter('') < iter('')
False
>>> iter('') < iter('')
True
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
True
>>> iter('') < iter('')
False
>>> iter('') < iter('')
True
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
True
>>> iter('') < iter('')
False
>>> iter('') < iter('')
True
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
>>> iter('') < iter('')
False
(Repeat as desired.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment