如何子类 mpmath 的 mpf?

问题描述 投票:0回答:1

我想在我自己的类中扩展

mpmath
的(https://mpmath.org/)数字类型
mpf
,但似乎不可能将其用作基类通常的方式。

import mpmath
class A(mpmath.mpf):
    def __init__(self, v):
        super().__init__(self, v)

你得到了

>>> A(0)
Traceback (most recent call last):
[...]
TypeError: object.__init__() takes exactly one argument (the instance to initialize)

在查看了

mpmath
的(相当复杂的)Python 源代码之后,我意识到
mpmath.mpf
的构造并不像“普通”Python 类。无论如何,是否有可能以某种方式正确继承它?

python mpmath
1个回答
0
投票

我假设

mpmath.mpf
使用
__new__
构造对象,而不是
__init__
,这就是为什么在调用
object.__init__
时调用
super().__init__

所以,你真的不必打电话给家长

__init__
。如果需要,只需保存该值即可。

class A(mpmath.mpf):
    def __init__(self, value: float):
        self.value = value
© www.soinside.com 2019 - 2024. All rights reserved.