为什么是> 0在Python 2中正确吗? [英] Why is '' > 0 True in Python 2?

查看:163
本文介绍了为什么是> 0在Python 2中正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 2.x中:

In Python 2.x:

>>> '' > 0
True

那是为什么?

推荐答案

允许对任意对象进行顺序比较的原始设计动机是允许对异构列表进行排序-有用的做法是,将所有字符串彼此并排放置.按字母顺序排列,所有数字都按数字顺序排列,尽管该语言不能保证首先使用两个块中的哪个.例如,这允许在O(N log N)最坏情况下的时间

The original design motivation for allowing order-comparisons of arbitrary objects was to allow sorting of heterogeneous lists -- usefully, that would put all strings next to each other in alphabetical order, and all numbers next to each other in numerical order, although which of the two blocks came first was not guaranteed by the language. For example, this allowed getting only unique items in any list (even one with non-hashable items) in O(N log N) worst-case time

多年来,这种务实的安排被侵蚀了.第一次破解是在很早以前的版本中取消了对复数进行阶数比较的功能时.突然,对任何列表进行排序的能力消失了:如果列表包含复数,可能还有其他类型的项,则不再适用.然后Guido开始更普遍地不喜欢异类列表,因此开始认为,是否可以对此类列表进行有效排序不是很重要……因为此类列表最初不应该存在,根据他的新思维.他没有采取任何措施禁止他们,但也不愿意接受任何妥协来支持他们.

Over the years, this pragmatic arrangement eroded. The first crack came when the ability to order-compare complex numbers was taken away, quite a few versions ago. Suddenly, the ability to sort any list disappeared: it did not apply any more if the list contained complex numbers, possibly together with items of other types. Then Guido started disliking heterogeneous lists more generally, and thus started thinking that it didn't really matter if such lists could be usefully sorted or not... because such lists should not exist in the first place, according to his new thinking. He didn't do anything to forbid them, but was not inclined to accept any compromises to support them either.

请注意,这两个更改都使平衡稍微偏离了Python Zen的实用性胜过纯度"项目(该内容在较早的时候就已经写好了,当复数仍然可以被按顺序比较时返回). ;-)–纯度更高,实用性更低.

Note that both changes move the balance a little bit away from the "practicality beats purity" item of the Zen of Python (which was written earlier, back when complex numbers still could be order-compared ;-) – a bit more purity, a bit less practicality.

尽管如此,对两个任意对象进行排序(只要它们都不是复数;-)的能力就保留了很长一段时间,因为Guido大约在同一时间开始真正坚持保持强大的向后兼容性(这种变化是<强大和实用纯;-).

Nevertheless the ability to order-compare two arbitrary objects (as long as neither was a complex number ;-) remained for a long time, because around that same time Guido started really insisting on maintaining strong backwards compatibility (a shift that's both practical and pure ;-).

因此,仅在Python 3中,该命令明确并有意消除了强大的向后兼容性的约束,从而允许进行一些长期渴望但向后不兼容的增强功能(尤其是简化和删除执行某些任务的过时,多余的方式),比较不同类型的实例成为错误.

So, it's only in Python 3, which explicitly and deliberately removed the constraint of strong backwards compatibility to allow some long-desired but backwards incompatible enhancements (especially simplifications and removal of obsolete, redundant way to perform certain tasks), that order comparison of instances of different types became an error.

因此,这一历史和哲学论着基本上是真正回答您的为什么"问题的唯一方法……! :-)

So this historical and philosophical treatise is basically the only way to truly respond to your "why" question...! :-)

这篇关于为什么是&gt; 0在Python 2中正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆