简体   繁体   English

为什么是int(50)

[英]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: 那么,我的问题有几个部分:

  1. Why does Python 2.x (< 3?) allow this behavior? 为什么Python 2.x(<3?)允许这种行为?
    • (And who thought it was a good idea to allow this to begin with???) (谁认为允许这个开头是个好主意?)
  2. What does it mean that an int is less than a str ? int小于str是什么意思?
    • Is it referring to ord / chr ? 是指ord / chr
    • Is there some binary format which is less obvious? 是否有一些二进制格式不太明显?
  3. Is there a difference between '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

From the docs : 来自文档

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'开头, strs开头,因此任何 int都小于任何 str .

  1. I have no idea. 我不知道。
    • A drunken master. 一个醉酒的主人。
  2. It means that a formal order has been introduced on the builtin types. 这意味着已在内置类型上引入了正式的订单。
    • It's referring to an arbitrary order. 它指的是任意顺序。
    • No. 没有。
  3. No. strings and unicode objects are considered the same for this purpose. 为此目的,编号字符串和unicode对象被认为是相同的。 Try it out. 试试看。

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: 把它分解为你的观点:

  1. Because it makes sort do something halfway usable where it otherwise would make no sense at all (mixed lists). 因为它使得排序做了一半可用的东西,否则根本就没有意义(混合列表)。 It's not a good idea generally, but much in Python is designed for convenience over strictness. 这通常不是一个好主意,但Python中的很多内容是为了方便而非严格。
  2. Ordered by type name. 按类型名称排序。 This means things of the same type group together, where they can be sorted. 这意味着同一类型的东西组合在一起,可以对它们进行排序。 They should probably be grouped by type class, such as numbers together, but there's no proper type class framework. 它们应该按类型分组,例如数字,但是没有合适的类型框架。 There may be a few more specific rules in there (probably is one for numeric types), I'd have to check the source. 那里可能有一些更具体的规则(可能是一个用于数字类型),我必须检查源。
  3. One is string and the other is unicode. 一个是字符串,另一个是unicode。 They may have a direct comparison operation, however, but it's conceivable a non-comparable type would get grouped between them, causing a mess. 然而,他们可能有一个直接的比较操作,但可以想象一个不可比较的类型会在它们之间分组,造成混乱。 I don't know if there's code to avoid this. 我不知道是否有代码可以避免这种情况。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM