Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> None > 0
False
>>> None == 0
False
>>> None < 0
True
- Is comparing
Noneusing arithmetic operators well defined for built-in types (integers in this case)? - Is the difference between the first two and the third comparison part of language specification (Python’s specification – you must be kidding :)) or is it CPython’s implementation detail?
The only meaningful comparison you can really use with
Noneisif obj is None:(orif obj is not None:).Comparison between different types has been removed from Python 3 for good reasons – they were a common source of errors and lead to confusion. For example
In Python 3, you get a
TypeErrorwhen comparing values of different types likestrvs.intor anything vs.None.(I mean “comparing” in the sense of trying to determine which of two values is larger/smaller. Comparison for equality is OK – it will always return
Falseif two object are of different types.)I haven’t found a reference in the docs for this, but in Learning Python, 4th edition, Mark Lutz writes on page 204: