I've searched and can't quite get an answer to this. I want a simple subclass of Matrix from sympy with specific dimensions. When I run this code in python 2.7:
我已经搜索过了,但却无法得到答案。我想要一个简单的Matrix子类来与特定维度交流。当我在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])
I get the error
我收到了错误
/Users/me/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:4:
DeprecationWarning: object.__init__() takes no parameters
I get the same error whether I use the code as is, or replace the __init__
in the line I commented out.
无论我是否按原样使用代码,或者在我注释掉的行中替换__init__,我都会得到同样的错误。
2 个解决方案
#1
3
The problem arises from the fact that calling super(JVec,self).__init__(*args)
will find the __init__
defined by object
since neither of the base classes define an __init__
method.
问题来自于调用super(JVec,self).__ init __(* args)将找到由object定义的__init__这一事实,因为这两个基类都没有定义__init__方法。
sympy
's code is using a different mechanism to create new instances. You should re-write you function to be:
sympy的代码使用不同的机制来创建新实例。你应该重写你的功能:
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
That's based on the way they are creating the RayTransferMatrix instances.
这是基于他们创建RayTransferMatrix实例的方式。
#2
0
I can produce the message as an error
with:
我可以将消息生成为错误:
>>> class Foo(object):
... def __init__(self,*args):
... super(Foo,self).__init__(*args)
...
>>> Foo()
<__main__.Foo object at 0xb744370c>
>>> Foo('one')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
TypeError: object.__init__() takes no parameters
That's not exactly your problem, but may give ideas of how to dig further.
这不完全是你的问题,但可能会提出如何进一步挖掘的想法。
#1
3
The problem arises from the fact that calling super(JVec,self).__init__(*args)
will find the __init__
defined by object
since neither of the base classes define an __init__
method.
问题来自于调用super(JVec,self).__ init __(* args)将找到由object定义的__init__这一事实,因为这两个基类都没有定义__init__方法。
sympy
's code is using a different mechanism to create new instances. You should re-write you function to be:
sympy的代码使用不同的机制来创建新实例。你应该重写你的功能:
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
That's based on the way they are creating the RayTransferMatrix instances.
这是基于他们创建RayTransferMatrix实例的方式。
#2
0
I can produce the message as an error
with:
我可以将消息生成为错误:
>>> class Foo(object):
... def __init__(self,*args):
... super(Foo,self).__init__(*args)
...
>>> Foo()
<__main__.Foo object at 0xb744370c>
>>> Foo('one')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
TypeError: object.__init__() takes no parameters
That's not exactly your problem, but may give ideas of how to dig further.
这不完全是你的问题,但可能会提出如何进一步挖掘的想法。