2016-02-27 44 views
-4

enter image description herePython的__init __()需要1周位置的說法,但3分別給予

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 

我真的沒有得到這個錯誤報告。

+0

複製您的代碼,修復縮進...代碼運行。 – idjaw

+2

這是你的代碼中的實際縮進嗎? – khelwood

+0

縮進在python中很重要。你的Matrix類沒有__init__方法,相反你有一個名爲__init__的頂級函數 – Daenyth

回答

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()構造。請參閱上述修改的代碼。

相關問題