Python 2如何比较string和int?为什么列表比较的结果大于数字,而元组的结果大于列表?

问题:Python 2如何比较string和int?为什么列表比较的结果大于数字,而元组的结果大于列表?

以下代码段带有输出注释(如ideone.com所示):

print "100" < "2"      # True
print "5" > "9"        # False

print "100" < 2        # False
print 100 < "2"        # True

print 5 > "9"          # False
print "5" > 9          # True

print [] > float('inf') # True
print () > []          # True

有人可以解释为什么这样的输出吗?


实施细节

  • 语言规范规定了这种行为,还是由实施者决定?
  • 任何主要的Python实现之间都有区别吗?
  • Python语言的版本之间有区别吗?

The following snippet is annotated with the output (as seen on ideone.com):

print "100" < "2"      # True
print "5" > "9"        # False

print "100" < 2        # False
print 100 < "2"        # True

print 5 > "9"          # False
print "5" > 9          # True

print [] > float('inf') # True
print () > []          # True

Can someone explain why the output is as such?


Implementation details

  • Is this behavior mandated by the language spec, or is it up to implementors?
  • Are there differences between any of the major Python implementations?
  • Are there differences between versions of the Python language?

回答 0

python 2手册

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

当您对两个字符串或两个数字类型进行排序时,将以预期的方式进行排序(字符串的字典顺序,整数的数字顺序)。

订购数字类型和非数字类型时,数字类型优先。

>>> 5 < 'foo'
True
>>> 5 < (1, 2)
True
>>> 5 < {}
True
>>> 5 < [1, 2]
True

当您订购两个都不兼容的类型(其中两个都不是数字)时,将按其类型名的字母顺序对其进行排序:

>>> [1, 2] > 'foo'   # 'list' < 'str' 
False
>>> (1, 2) > 'foo'   # 'tuple' > 'str'
True

>>> class Foo(object): pass
>>> class Bar(object): pass
>>> Bar() < Foo()
True

一个exceptions是旧样式类,它总是先于新样式类。

>>> class Foo: pass           # old-style
>>> class Bar(object): pass   # new-style
>>> Bar() < Foo()
False

语言规范规定了这种行为,还是由实施者决定?

没有语言规范。该语言参考说:

否则,不同类型的对象总是比较不相等,并且被一致地,任意地排序。

因此,这是一个实现细节。

任何主要的Python实现之间都有区别吗?

我无法回答这一问题,因为我只使用了官方的CPython实现,但是还有其他Python实现,例如PyPy。

Python语言的版本之间有区别吗?

在Python 3.x中,行为已更改,因此尝试对整数和字符串进行排序将引发错误:

>>> '10' > 5
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    '10' > 5
TypeError: unorderable types: str() > int()

From the python 2 manual:

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.

When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).

When you order a numeric and a non-numeric type, the numeric type comes first.

>>> 5 < 'foo'
True
>>> 5 < (1, 2)
True
>>> 5 < {}
True
>>> 5 < [1, 2]
True

When you order two incompatible types where neither is numeric, they are ordered by the alphabetical order of their typenames:

>>> [1, 2] > 'foo'   # 'list' < 'str' 
False
>>> (1, 2) > 'foo'   # 'tuple' > 'str'
True

>>> class Foo(object): pass
>>> class Bar(object): pass
>>> Bar() < Foo()
True

One exception is old-style classes that always come before new-style classes.

>>> class Foo: pass           # old-style
>>> class Bar(object): pass   # new-style
>>> Bar() < Foo()
False

Is this behavior mandated by the language spec, or is it up to implementors?

There is no language specification. The language reference says:

Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.

So it is an implementation detail.

Are there differences between any of the major Python implementations?

I can’t answer this one because I have only used the official CPython implementation, but there are other implementations of Python such as PyPy.

Are there differences between versions of the Python language?

In Python 3.x the behaviour has been changed so that attempting to order an integer and a string will raise an error:

>>> '10' > 5
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    '10' > 5
TypeError: unorderable types: str() > int()

回答 1

字符串字典顺序比较,不同类型由它们的类型的名称进行比较("int"< "string")。3.x通过使它们不可比来解决了第二点。

Strings are compared lexicographically, and dissimilar types are compared by the name of their type ("int" < "string"). 3.x fixes the second point by making them non-comparable.