代码之家  ›  专栏  ›  技术社区  ›  fixxxer

不同类型对象比较说明

  •  0
  • fixxxer  · 技术社区  · 14 年前

    以下句子是我困惑的原因(来自guido关于python.org的教程):

    “请注意,比较 不同的类型是合法的。结果 是确定性的但武断的 类型按名称排序。因此, 列表总是小于 字符串,字符串总是较小的 比一个元组,等等。“比一个元组,等等。”

    这意味着:

    a=[90]
    b=(1)
    a<b
    

    结果应该是 True . 但事实并非如此! 你能帮我一下吗?而不是一个元组等。”

    还有,“结果是确定的,但武断的”是什么意思?

    2 回复  |  直到 14 年前
        1
  •  6
  •   Ignacio Vazquez-Abrams    14 年前

    (1) int (1,) tuple

        2
  •  3
  •   Daniel Kluev    14 年前

    >>> set([1]) > [1]
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    TypeError: can only compare to a set
    

    >>> [1,2] > (3,4)
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    TypeError: unorderable types: list() > tuple()
    >>> [1,2] > "1,2"
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    TypeError: unorderable types: list() > str()