为什么十进制数不是实数抽象基类的实例?

4
Python的Decimal对象当前未被指定为“Real”抽象基类的子类:
from numbers import Real
from decimal import Decimal

isinstance(Decimal("1.0"), numbers.Real) #  False

这可以通过将Decimal注册为一个子类来轻松更改:
Real.register(Decimal)

但这让我想问一个问题:为什么一开始没有以这种方式注册Decimal?有实际原因或设计原因吗?假设这样做会产生什么问题?


我认为你可以把这看作是那些ABC技术采用率低的标志。 - user2357112
@user2357112 说得好。我是唯一一个尝试利用它们的人类。 - Rick
我发誓我搜索了这个问题,但没有找到任何答案。我怎么错过了那个重复的问题?唉,算了。 - Rick
1个回答

4

答案在numbers模块源代码中:

## Notes on Decimal
## ----------------
## Decimal has all of the methods specified by the Real abc, but it should
## not be registered as a Real because decimals do not interoperate with
## binary floats (i.e.  Decimal('3.14') + 2.71828 is undefined).  But,
## abstract reals are expected to interoperate (i.e. R1 + R2 should be
## expected to work if R1 and R2 are both Reals).

他们可能会把这个加入文档中,我认为。

源代码的第503行基本上也是同样的内容:_pydecimal.py。但它引用了你上面粘贴的numbers - Error - Syntactical Remorse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接