矩阵方法中参数的错误 class - Python

Error of argument in method of matrix class - Python

我在 python 3 中创建矩阵 class(到目前为止我只创建了一种方法):

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]])

但它给出了这个错误:

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

我输入了 'rows' 参数。显然,我不需要输入'self'。我将 VS Code 用于 IDE。感谢您的帮助。

你的功能一切正常。

你只是在实例化时忘记了括号m=Matrix()。所以解释器认为你必须指定 self,因为它不识别 class.

编辑: 我刚刚认识到另一个问题。您实际上用那些 while 循环创建了一个无限循环。如果您不 addition-assign i 和 j,它们将始终分别低于 len(rows[0])len(rows)

所以:

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]])