gpt4 book ai didi

python - DeprecationWarning : object. __init__() 没有参数

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:22 25 4
gpt4 key购买 nike

我已经搜索过了,但无法完全找到答案。我想要一个来自 sympy 的具有特定维度的 Matrix 的简单子(monad)类。当我在 python 2.7 中运行此代码时:

from sympy import *
class JVec(Matrix):
def __init__(self, *args):
super(JVec,self).__init__(*args)
#Matrix.__init__(self, *args)
if self.shape != (2,1):
raise TypeError("JVec: shape must be (2,1)")
a = JVec([1,0])

我得到了错误

/Users/me/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:4: 
DeprecationWarning: object.__init__() takes no parameters

无论我按原样使用代码,还是替换我注释掉的行中的 __init__,我都会得到同样的错误。

最佳答案

问题是因为调用super(JVec,self).__init__(*args)会找到object定义的__init__ > 因为两个基类都没有定义 __init__ 方法。

sympy 的代码使用了 a different mechanism创建新实例。您应该将函数重写为:

class JVec(Matrix):
def __new__(cls, *args):
newobj = Matrix.__new__(cls, *args)
if newobj.shape != (2, 1):
raise TypeError("JVec: shape must be (2,1)")
return newobj

这是基于他们的方式 creating the RayTransferMatrix instances .

关于python - DeprecationWarning : object. __init__() 没有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33985173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com