【问题标题】:Error of argument in method of matrix class - Python矩阵类方法中的参数错误 - Python
【发布时间】:2021-02-18 02:30:36
【问题描述】:

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

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\2-11-2.py", line 14, in <module>
    m.SetRows([[0,8,56],[98,568,89]])
TypeError: SetRows() missing 1 required positional argument: 'rows'

我输入了“行”参数。显然,我不需要输入“自我”。我将 VS Code 用于 IDE。感谢您的帮助。

【问题讨论】:

    标签: python class methods arguments


    【解决方案1】:

    你的功能一切正常。

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

    编辑: 我刚刚认识到另一个问题。你实际上用那些while 循环创建了一个无限循环。如果您不添加分配 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]])
    

    【讨论】:

      猜你喜欢
      • 2019-11-29
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多