为什么列表可以与 Python 中的整数进行比较
Why can a list be compared with an integer in Python
我搜索了一下,似乎没有人有这个具体问题。为什么 Python 让我比较一个列表和一个整数?例如,
[] < 10
评估为 False
和
[] > 10
计算结果为 True
这些操作不是定义错误吗?Python 不应该为这些操作抛出异常吗?
从 Python 3.x 开始,你是对的,这不再被允许
>>> [] < 10
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
[] < 10
TypeError: unorderable types: list() < int()
至于为什么这在 Python 2.x、read here
中有效
我搜索了一下,似乎没有人有这个具体问题。为什么 Python 让我比较一个列表和一个整数?例如,
[] < 10
评估为 False
和
[] > 10
计算结果为 True
这些操作不是定义错误吗?Python 不应该为这些操作抛出异常吗?
从 Python 3.x 开始,你是对的,这不再被允许
>>> [] < 10
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
[] < 10
TypeError: unorderable types: list() < int()
至于为什么这在 Python 2.x、read here
中有效