在一个类中,我有以下代码:
class Player (object):
def __init__ (self, first, last):
'''the constructor for a player'''
self.first = first
self.last = last
self.rating = 0
self.info = []
def update(self, pos, team, year, att, yards, tds, fumbles ):
'''create a list of information for this player for this year and
append it to the info field. Then call calcrating.'''
self.info = self.info.append(year, pos, team, att, yards, tds, fumbles)
在程序中,我使用以下代码调用类的update函数:
^{pr2}$playerDict
和{playerName
,它会给我AttributeError: 'list' object has no attribute 'info'
在Python的网站指南中,它的构造函数中的空列表代码和更新代码正是我的代码,但是我的代码不起作用。有没有办法绕过这个错误?在
您没有实例化
Player
类。因此,当您调用update
时,实例self
不会自动作为参数传递给update
方法,因此其中一个参数(列表)将取代它。在这应该是有效的:
注意:AFAIK,这只在python3中发生。在python2.x中,
self
有一个类型检查。在顺便说一下,这条线也不行。在
^{pr2}$检查^{} 文档。在
相关问题 更多 >
编程相关推荐