python - 在 PEP 3141 中,为什么 Number 没有 add 方法?

标签 python python-3.x

PEP 3141使用 Complex.__add__ 但没有 Number.__add__ 定义数字层次结构。这似乎是一个奇怪的选择,因为(实际上)从 Number 派生的其他数字类型 Decimal 也实现了 add 方法。

为什么会这样呢?如果我想在我的代码中添加类型注释或断言,我应该使用 x:(Complex, Decimal) 吗?或者 x:Number 而忽略这个声明实际上毫无意义的事实?

最佳答案

我相信可以在Rejected Alternatives中找到答案。 :

The initial version of this PEP defined an algebraic hierarchy inspired by a Haskell Numeric Prelude [3] including MonoidUnderPlus, AdditiveGroup, Ring, and Field, and mentioned several other possible algebraic types before getting to the numbers. We had expected this to be useful to people using vectors and matrices, but the NumPy community really wasn't interested ...

有更复杂的数字系统,显然不支持加法。他们本可以更详细地介绍他们的类层次结构(并且最初打算这样做),但是社区缺乏兴趣。因此,对于任何想要变得更复杂的人来说,不指定 Numbers 会更容易。

请注意,Monoids 是一个示例,其中只定义了一个二元运算。

关于python - 在 PEP 3141 中,为什么 Number 没有 add 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42742499/

相关文章:

python - 如何将函数导入到 Python 包中而不同时导入包含该函数的文件?

python3操作系统错误: [Errno 107] Transport endpoint is not connected

python - 理解这一行 : list_of_tuples = [(x, y) for x, y, label in data_one]

python - 我可以将值列表设置为字典中的键列表吗?

python - 从 SOAP 响应中解析多个值

python - 数字列表的格式化输出

python-3.x - 通过创建单独的函数来使try-except变通办法适用于单行中的许多语句

python - 如何通过 python 使用 Graphviz 从预构建的 .gv 文件创建有向图?

python - 使用 mysql 在 python 中的 while 循环中选择

python - 处理 IPython 集群中的各种资源需求