在Python中,None的评估值小于零? [重复]

时间:2022-09-06 20:06:46

This question already has an answer here:

这个问题在这里已有答案:

In Python, None evaluates to less than zero?

在Python中,None的评估值小于零?

ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> None < 0
True
>>> None == 0
False
>>> None > 0
False
>>>

Is this expected?

这是预期的吗?

I would have guessed that None would either be equal to zero (through type coercion), or that all of these statements would return False.

我猜想None将等于零(通过类型强制),或者所有这些语句都返回False。

3 个解决方案

#1


13  

See the manual:

参见手册:

Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result).

除了不同的数字类型和不同的字符串类型之外,不同类型的对象永远不会相等;这些对象是一致但任意排序的(因此排序异构数组会产生一致的结果)。

and

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.

CPython实现细节:除了数字之外的不同类型的对象按其类型名称排序;不支持正确比较的相同类型的对象按其地址排序。

#2


4  

From the docs:

来自文档:

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.

CPython实现细节:除了数字之外的不同类型的对象按其类型名称排序;不支持正确比较的相同类型的对象按其地址排序。

NoneType compares as smaller than int since the comparison appears to be case-sensitive.

NoneType比较小于int,因为比较似乎区分大小写。

>>> type(0)
<type 'int'>
>>> type(None)
<type 'NoneType'>
>>> 'NoneType' < 'int'
True

#3


3  

It is intentional to make operations like sorting and dictionary comparison well defined.

有意识地定义排序和字典比较等操作。

[citing from the Language Reference]

[引用语言参考]

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrary.

运算符<,>,==,> =,<=和!=比较两个对象的值。对象不需要具有相同的类型。如果两者都是数字,则将它们转换为通用类型。否则,不同类型的对象总是比较不相等,并且一致但是任意地排序。

#1


13  

See the manual:

参见手册:

Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result).

除了不同的数字类型和不同的字符串类型之外,不同类型的对象永远不会相等;这些对象是一致但任意排序的(因此排序异构数组会产生一致的结果)。

and

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.

CPython实现细节:除了数字之外的不同类型的对象按其类型名称排序;不支持正确比较的相同类型的对象按其地址排序。

#2


4  

From the docs:

来自文档:

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.

CPython实现细节:除了数字之外的不同类型的对象按其类型名称排序;不支持正确比较的相同类型的对象按其地址排序。

NoneType compares as smaller than int since the comparison appears to be case-sensitive.

NoneType比较小于int,因为比较似乎区分大小写。

>>> type(0)
<type 'int'>
>>> type(None)
<type 'NoneType'>
>>> 'NoneType' < 'int'
True

#3


3  

It is intentional to make operations like sorting and dictionary comparison well defined.

有意识地定义排序和字典比较等操作。

[citing from the Language Reference]

[引用语言参考]

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrary.

运算符<,>,==,> =,<=和!=比较两个对象的值。对象不需要具有相同的类型。如果两者都是数字,则将它们转换为通用类型。否则,不同类型的对象总是比较不相等,并且一致但是任意地排序。