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
, andField
, 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/