我正在嘗試編寫一個需要3個關鍵字參數的類方法。我之前使用過關鍵字參數,但似乎無法讓它在我的課程中發揮作用。下面的代碼:類方法中的Python關鍵字參數
def gamesplayed(self, team = None, startyear = self._firstseason,
endyear = self._lastseason):
totalGames = 0
for i in self._seasons:
if((i.getTeam() == team or team == "null") and
i.getYear() >= startyear and i.getYear() <= endyear):
totalGames += i .getGames()
return totalGames
產生錯誤:
NameError: name 'self' is not defined
如果我參加了關鍵字參數,讓他們簡單的位置的,它工作正常。因此我不確定我的問題在哪裏。預先感謝您的幫助。
謝謝您的深入解釋。它使一切變得更加清晰。 –
正如右下方指出的那樣,需要注意的是,None還沒有被用作用戶可以傳遞的有效值,例如在這個代碼中,如果None不是指不限制。 –