I have a class:
class MATRIX:
# convenience function
def getDimensions(self,M):
r = len(M)
c = len(M[0])
return r,c
def add(self,M1,M2):
res = []
r1,c1 = self.getDimensions(M1)
r2,c2 = self.getDimensions(M2)
if (r1 != r2) or (c1 != c2):
print("dimensions not the same")
return res
for i in range(len(M1)):
row=[]
for j in range(len(M1[i])):
element=M1[i][j] + M2[i][j]
row.append(element)
res.append(row)
return res
Simple class, saves no state, just has a function which adds two matrices. I am a beginner at OOP in Python, so, unless I am wrong, all class functions must begin with a self parameter.
The function is meant to be called like:
Z = MATRIX.add(X,Y)
Where X and Y are matrices
When I try to do this, I get the following error:
Traceback (most recent call last):
File "temp.py", line 82, in <module>
Z = MATRIX(X,Y)
TypeError: object() takes no parameters
In this instance:
X = [[1,1,1], [2,2,2], [3,3,3]]
Y = [[4,4,4], [5,5,5], [6,6,6]]
Why is this error showing up? How can I fix it?
MATRIX(X,Y)with parameters.. or fix the incorrect call.Z = MATRIX.add(X, Y)but the error saysZ = MATRIX(X, Y). Which is it?