2013-10-19 114 views
2

我知道這個標題看起來很熟悉一些舊問題,但我看過他們中的每一個,他們都沒有解決。 這裏是我的代碼:TypeError:init_animals()需要1個位置參數,但有2個被給出

class Island (object):E,W,R,P 
    def __init__(self, x, y): 
    self.init_animals(y) 
    def init_animals(y): 
    pass 

isle = Island(x,y) 

但是,我得到了以下錯誤:

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "<stdin>", line 3, in __init__ 
TypeError: init_animals() takes 1 positional arguments but 2 were given 

請告訴我,如果我有任何錯誤,我好這個困惑。 最好的問候

+4

'def init_animals(self,y):' –

回答

6

您需要添加selfinit_animals第一個參數:

def init_animals(self, y): 
    pass 

self(類似於Java this)是類的實例。每次您在班級中調用方法時,都會將self作爲第一個參數發送給該方法。

+0

因此,爲了做出這樣的錯誤,謝謝你這麼多 – libra

相關問題