0
나는 피연산자 무시에 관한 질문이 있습니다. 난 그냥 사용자 정의 클래스에 __add__(self, other)
연산자를 재정의하려고, 그 요소 (numpy 배열)에 다른 numpy 배열에 추가 할 수 있습니다. 합계를 가능하게하기 위해서 나는 __add__
과 __radd__
연산자를 선언했다. 작은 예 :파이썬 클래스 numpy 배열로 합계
import numpy as np
class MyClass():
def __init__(self, x):
self.x = x
self._mat = self._calc_mat()
def _calc_mat(self):
return np.eye(2)*self.x
def __add__(self, other):
return self._mat + other
def __radd__(self, other):
return self._mat + other
def some_function(x):
return x + np.ones(4).reshape((2,2))
def some_other_function(x):
return np.ones(4).reshape((2,2)) + x
inst = MyClass(3)
some_function(x=inst)
some_other_function(x=inst)
는 이상하게 I는 두 개의 서로 다른 출력을 얻는다. some_function
에서 첫 번째 OUPUT, 그냥 예상 같다 :
Out[2]:
array([[array([[ 4., 1.],
[ 1., 4.]]),
array([[ 4., 1.],
[ 1., 4.]])],
[array([[ 4., 1.],
[ 1., 4.]]),
array([[ 4., 1.],
[ 1., 4.]])]], dtype=object)
누군가가 그 이유 아이디어가 있습니까 :
Out[1]
array([[ 4., 1.],
[ 1., 4.]])
두 번째 출력은 나에게 뭔가 이상한을 제공합니다?
감사합니다, 마르쿠스