【问题标题】:Why can a list be compared with an integer in Python [duplicate]为什么列表可以与Python中的整数进行比较[重复]
【发布时间】:2015-03-08 09:31:34
【问题描述】:

我已经搜索过了,似乎没有人有这个特定的问题。为什么 Python 让我将列表与整数进行比较?例如,

[]

评估为假

[] > 10

评估为真

这些操作不是定义不明确吗,Python 不应该为这些操作抛出异常吗?

【问题讨论】:

标签: python strong-typing typing dynamic-typing


【解决方案1】:

从 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 3 明确禁止它。有道理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 2021-08-21
  • 2021-10-26
  • 1970-01-01
  • 2018-09-10
  • 2018-02-19
相关资源
最近更新 更多