[英]Why is int(50)<str(5) in python 2.x?
In python 3, int(50)<'2'
causes a TypeError
, and well it should. 在python 3中,
int(50)<'2'
会导致TypeError
,它应该是。 In python 2.x, however, int(50)<'2'
returns True
(this is also the case for other number formats, but int
exists in both py2 and py3). 但是,在python 2.x中,
int(50)<'2'
返回True
(对于其他数字格式也是这种情况,但是在py2和py3中都存在int
)。 My question, then, has several parts: 那么,我的问题有几个部分:
int
is less than a str
? int
小于str
是什么意思?
ord
/ chr
? ord
/ chr
? '5'
and u'5'
in this regard? '5'
和u'5'
之间有区别吗? It works like this 1 . 它的工作原理如下1 。
>>> float() == long() == int() < dict() < list() < str() < tuple()
True
Numbers compare as less than containers. 数字比较小于容器。 Numeric types are converted to a common type and compared based on their numeric value.
数字类型将转换为通用类型,并根据其数值进行比较。 Containers are compared by the alphabetic value of their names.
容器按其名称的字母值进行比较。 2
2
CPython implementation detail: Objects of different types except numbers are ordered by >their type names;
CPython实现细节:除了数字之外的不同类型的对象按其类型名称排序; objects of the same types that don't support proper comparison are >ordered by their address.
不支持正确比较的相同类型的对象按其地址排序。
Objects of different builtin types compare alphabetically by the name of their type
int
starts with an 'i' and
str
starts with an
s
so any
int
is less than any
str
.
不同内置类型的对象按字母顺序按类型名称进行比较
. int
以'i'开头,
str
以
s
开头,因此任何
int
都小于任何
str
。
。
In response to the comment about long < int
回应关于
long < int
的评论
>>> int < long
True
You probably meant values of those types though, in which case the numeric comparison applies. 您可能意味着这些类型的值,在这种情况下,数字比较适用。
1 This is all on Python 2.6.5 1这完全在Python 2.6.5上
2 Thank to kRON for clearing this up for me. 2感谢kRON为我解决这个问题。 I'd never thought to compare a number to a
dict
before and comparison of numbers is one of those things that's so obvious that it's easy to overlook. 我从没想过到一些比作
dict
之前和数字的对比是那些东西,是如此明显,它很容易被忽视的。
The reason why these comparisons are allowed, is sorting. 允许这些比较的原因是排序。 Python 2.x can sort lists containing mixed types, including strings and integers -- integers always appear first.
Python 2.x可以对包含混合类型的列表进行排序,包括字符串和整数 - 整数首先出现。 Python 3.x does not allow this, for the exact reasons you pointed out.
由于您指出的确切原因,Python 3.x不允许这样做。
Python 2.x: Python 2.x:
>>> sorted([1, '1'])
[1, '1']
>>> sorted([1, '1', 2, '2'])
[1, 2, '1', '2']
Python 3.x: Python 3.x:
>>> sorted([1, '1'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()
(And who thought it was a good idea to allow this to begin with???) (谁认为允许这个开头是个好主意?)
I can imagine that the reason might be to allow object from different types to be stored in tree-like structures, which use comparisons internally. 我可以想象,原因可能是允许来自不同类型的对象存储在树状结构中,这种结构在内部使用比较。
As Aaron said. 正如亚伦所说。 Breaking it up into your points:
把它分解为你的观点:
So, it doesn't make sense in the general case, but occasionally it's helpful. 因此,在一般情况下它没有意义,但偶尔它会有所帮助。
from random import shuffle
letters=list('abcdefgh')
ints=range(8)
both=ints+letters
shuffle(ints)
shuffle(letters)
shuffle(both)
print sorted(ints+letters)
print sorted(both)
Both print the ints first, then the letters. 两者首先打印整数,然后打印字母。
As a rule, you don't want to mix types randomly within a program, and apparently Python 3 prevents it where Python 2 tries to make vague sense where none exists. 通常,您不希望在程序中随机混合类型,显然Python 3会阻止它在Python 2试图模糊不清的地方。 You could still sort by
lambda a,b: cmp(repr(a),repr(b))
(or something better) if you really want to, but it appears the language developers agreed it's impractical default behaviour. 如果你真的想要,你仍然可以按照
lambda a,b: cmp(repr(a),repr(b))
(或更好的东西)进行排序,但似乎语言开发人员认为它是不切实际的默认行为。 I expect it varies which gives the least surprise, but it's a lot harder to detect a problem in the Python 2 sense. 我预计它会有所不同,这给出了最少的意外,但是在Python 2意义上检测问题要困难得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.