【发布时间】:2012-10-30 23:02:31
【问题描述】:
我正在尝试编写一个采用 3 个关键字参数的类方法。我以前使用过关键字参数,但似乎无法让它在我的课堂上工作。以下代码:
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' 未定义
如果我取出关键字参数并使它们成为简单的位置参数,它就可以正常工作。因此,我不确定我的问题出在哪里。提前感谢您的帮助。
【问题讨论】:
标签: python class arguments keyword