Python __init __()需要1个位置参数,但3个被赋予

时间:2016-02-27 22:38:50

标签: python oop

enter image description here

enter image description here

我为我的作业写了一个矩阵课。

class Matrix():
'''A class to represent a mathematical matrix'''

    def __init__(self, m, n, default=0):
        '''(Matrix, int, int, float) -> NoneType
        Create a new m x n matrix with all values set to default
        '''
        self._head = MatrixNode(None)
        self._m = m
        self._n = n
        self._default = default

但是,测试期间出现了错误。

if __name__ == '__main__':
    m1 = Matrix(3,3)
    print(m1.get_val(0, 0))
    m1.set_val(0,0, 3)
    m1.set_val(2, 2, 5)

这是错误。

Traceback (most recent call last):
   File "/Users/Xueli/Desktop/a1.py", line 336, in <module>
   m1 = Matrix(3,3)
builtins.TypeError: __init__() takes 1 positional argument but 3 were given

我真的没有收到此错误报告。

1 个答案:

答案 0 :(得分:0)

class Matrix():
    '''A class to represent a mathematical matrix'''

    def __init__(self, m, n, default=0):
        '''(Matrix, int, int, float) -> NoneType
        Create a new m x n matrix with all values set to default
        '''
        self._head = MatrixNode(None)
        self._m = m
        self._n = n
        self._default = default

没有正确的缩进def __init__()不会被识别为class Matrix()构造函数。请参阅上面修改过的代码。