0

I'm having some troubles when it comes to multiplying two matrices. An AttributeError appears when I'm trying to perform the addition part

Traceback (most recent call last): File "MatrixClass.py", line 189, in <module> main() File "MatrixClass.py", line 184, in main mat.multiplyMatrixes(mat1,mat2) File "MatrixClass.py", line 176, in multiplyMatrixes self[i][j] += (m1[i][k])*(m2[k][j]) AttributeError: matrix instance has no attribute '__getitem__' 

I tried saving the new matrix in another instance called for example m3 but the I thought it would be better to use self instead.

Here's my code:

def multiplyMatrices(self,m1,m2): if m1.getRows() == m2.getColumns() and m1.getColumns() == m2.getRows(): self.setRows() self.setColumns() for i in range(m1.getRows()): for j in range(m2.getColumns()): for k in range(m1.getColumns()): self[i][j] += (m1[i][k])*(m2[k][j]) 

I created the instance of self in main(), before calling multiplyMatrices()

8
  • Does your matrix class actually have a __getitem__ method? Commented Aug 19, 2013 at 20:51
  • How can I know that? I implemented getValue(file,column), getRows() & getColumns() Commented Aug 19, 2013 at 20:53
  • 1
    Well, you wrote it, didn't you? Did you write a __getitem__ method? (If you did, is it still there when you look for it? And is the name right?) Commented Aug 19, 2013 at 20:54
  • 1
    This is confusing. If you get this to work, the call m0.multiplyMatrices(m1, m2) is going to end up replacing the contents of m0 with m1 * m2 Why would you want that? Can you show us (a stripped-down version of) your class definition, and describe what the API is supposed to be? Commented Aug 19, 2013 at 21:00
  • Note that getter and setter functions (especially this usage) are unpythonic - prefer raw attributes or @propertys Commented Aug 19, 2013 at 21:21

1 Answer 1

1

According to the AttributeError, you never defined the __getitem__ method in your class. This is how you can control object[key] access. I would suggest reading up on the python data model in general if you are deciding to make a more advanced class (like this one) in python. Although it is a bit strange to have the multiplication of two other matrices being stored in self. I'd probably just create a new matrix in the method and return that.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.