The only meaningful comparison you can really use with None
is if obj is None:
(or if 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
>>> "3" < 4
False
In Python 3, you get a TypeError
when comparing values of different types like str
vs. int
or anything vs. None
.
>>> None < 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < int()
(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 False
if 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:
[...] Comparisons of differently typed objects (e.g., a string and a
list) work — the language defines a fixed ordering among different
types, which is deterministic, if not aesthetically pleasing. That is,
the ordering is based on the names of the types involved: all integers
are less than all strings, for example, because "int"
is less than
"str"
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…