Parsa Saberi
Parsa Saberi

Reputation: 105

Error of argument in method of matrix class - Python

I create matrix class in python 3 (so far I have only create one method):

class Matrix() :
    __rows = []
    __columns = []
    def SetRows(self,rows) :
        self.__rows = rows
        i = 0
        while i < len(rows[0]) :
            a = []
            j = 0
            while j < len(rows) :
                a.append(rows[j][i])
                j += 1
            self.__columns.append(a)
            i += 1
m = Matrix
m.SetRows([[0,8,56],[98,568,89]])

But it gives this error:

Traceback (most recent call last):
  File "f:\PARSA\Programming\Python\2-11-2.py", line 14, in <module>
    m.SetRows([[0,8,56],[98,568,89]])
TypeError: SetRows() missing 1 required positional argument: 'rows'

I've entered 'rows' argument. Obviously, I don't need to enter 'self'. I use VS Code for IDE. Thanks for your help.

Upvotes: 2

Views: 84

Answers (1)

Marius
Marius

Reputation: 94

Everything's fine with your function.

You just forgot the brackets when instantiating m=Matrix(). So the interpreter thinks you have to specify self, since it didn't recognize the class.

EDIT: I've just recognized another problem. You actually created an infinite loop with those while loops. If you don't addition-assign i and j those will always stay below len(rows[0]) and len(rows) respectively.

So:

class Matrix() :
    __rows = []
    self.__columns = []
    def SetRows(self,rows) :
        self.__rows = rows
        i = 0
        while i < len(rows[0]) :
            a = []
            j = 0
            while j < len(rows) :
                a.append(rows[j][i])
                j += 1
            self.__columns.append(a)
            i += 1

m = Matrix()
m.SetRows([[0,8,56],[98,568,89]])

Upvotes: 2

Related Questions