本文介绍了Python的丰富比较行为(或者,当Decimal('100.0')< .01)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个衬垫:

  import decimal; h =十进制('100.0'); (h> .01,h   

它是一个十进制对象持有100.0,并将其与.01(浮动)以各种方式进行比较。



是:

 >>>导入十进制; h = decimal.Decimal('100.0'); (h> .01,h (False,True,NotImplemented,NotImplemented)

从文档:丰富的比较方法可能返回单例NotImplemented,如果它不实现给定的参数对的操作。



这里真的有三个问题。


  1. 丰富的比较方法返回NotImplemented,会发生什么?为什么不引发异常?


  2. 当它获得NotImplemented时,为什么在第一种情况下返回False, bool(NotImplemented)应该是一个常数。


  3. 它只是回退到id似乎没有(或是,但是向后):


(忽略此行,格式化被修复, it)

 从小数导入十进制
h =十进制('100.0')
f = .01
print h< f,id(h) id(f)
print h> f,id(h)>我的结果经过测试:

  Python 2.6.4(r264:75708,Oct 26 2009,08:23:19)[MSC v.1500 32位(Intel)] on win32 
Python 2.6.5(r265:79096,2010年3月19日,21:48:26)[MSC v.1500 32位(Intel)] on win32

编辑:有关订购的文档:

解决方案

它委托给反方法(例如 float )中的运算符> )RHS时, - 在这种情况下也返回NotImplemented - 最后回到Python 2的异类比较的愚蠢的旧规则。

bool 涉及 - 由于比较的两边返回NotImplemented(由于故意设计决定不支持小数和浮点之间的任何操作),愚蠢的旧规则被用作后备(在近期足够版本会比较类型,而不是实例 - id 与它无关,因此)。在Python 3中,这种不受支持的异构比较将会失败,并引发一个明显的异常,但是在Python 2中,为了向后兼容性,这不可能发生 - 它必须保持在Python 2生命周期中的行为。



介绍向后不兼容性来解决现在被认为是设计错误的问题,例如关于het比较的部分, 引入Python 3的核心原因。只要你坚持Python 2(例如,因为它有更多的第三方扩展等),你需要咧嘴和承担这些缺陷,只有在Python 3中修复。


So I have a one liner:

import decimal; h = decimal.Decimal('100.0'); (h > .01, h < .01, h.__gt__(.01), h.__lt__(.01))

All it does is make a Decimal object holding 100.0, and compares it to .01 (the float) in various ways.

My result is:

>>> import decimal; h = decimal.Decimal('100.0'); (h > .01, h < .01, h.__gt__(.01), h.__lt__(.01))
(False, True, NotImplemented, NotImplemented)

From the docs: "A rich comparison method may return the singleton NotImplemented if it does not implement the operation for a given pair of arguments."

So really there are three questions here.

  1. When a rich comparison method returns NotImplemented, what happens? Why doesn't it raise an Exception?

  2. When it gets NotImplemented, why does it return False in the first case, and True in the second? bool(NotImplemented) should be a constant.

  3. Does it simply fall back to id() checking? It seems no (or yes, but backwards):

(ignore this line, formatting is screwed up and this fixes it)

from decimal import Decimal
h = Decimal('100.0')
f = .01
print h < f, id(h) < id(f)
print h > f, id(h) > id(f)

My results were tested on:

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32

Edit: Documentation about ordering: http://docs.python.org/library/stdtypes.html#comparisons

解决方案

it delegates to the converse method (e.g., __lt__ when the operator is >) RHS in the comparison (the float) -- which in this case also returns NotImplemented -- and finally falls back to Python 2's silly old rules for heterogeneous comparisons.

No bool involved -- since both sides of the comparison return NotImplemented (due to a deliberate design decision to NOT support any operation between decimals and floats), the silly old rules are used as a fallback (and in a recent-enough version will be comparing the types, not the instances -- id has nothing to do with it, therefore). In Python 3, such an unsupported heterogeneous comparison would fail, and raise a clear exception, but in Python 2, for backwards compatibility, that just can't happen -- it must keep behaving in the silly way it's behaved throughout Python 2's lifetime.

Introducing backwards incompatibilities to fix what are now considered design errors, like this part about het comparisons, was the core reason to introduce Python 3. As long as you're sticking to Python 2 (e.g. because it has more third party extensions, etc), you need to grin and bear with these imperfections that are fixed in Python 3 only.

这篇关于Python的丰富比较行为(或者,当Decimal('100.0')&lt; .01)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 20:20