仅当实例具有新的espn_player_id
时,我才尝试从数据集中创建实例。如果有已经创建为实例的行,我只想执行类方法。
td
表示总数据,为OrderedDict
。
但是,当涉及到else块时,我总是收到错误消息
'str' object has no attribute 'num_games'
如何使td[i]['espn_player_id']
被识别为要执行num_games
的现有实例的名称?
class Player:
QBR_h = None
QBR_l = 0
QBR_a = 0
ap = 0
ng = 0
def __init__(self,pid,fname,lname):
self.pid = pid
self.fname = fname
self.lname = lname
print(self.fname, self.lname,'Constructed')
def num_games(self):
self.ng = self.ng + 1
print(self.fname,self.lname,'number of games:',self.ng)
def max_QBR(self):
if self.QBR_h == None or self.QBR_h < td[i]['total_QBR']:
self.QBR_h = td[i]['total_QBR']
print(self.QBR_h)
def asdict(self):
return {'fname':self.fname, 'ng':self.ng}
lst_pid = list()
for i in range(len(td)):
print(td[i]['espn_player_id'])
if td[i]['espn_player_id'] not in lst_pid:
lst_pid.append(td[i]['espn_player_id'])
print(lst_pid)
td[i]['espn_player_id'] = Player(td[i]['espn_player_id'], td[i]['first_name'],td[i]['last_name'])
td[i]['espn_player_id'].num_games()
td[i]['espn_player_id'].max_QBR()
else:
td[i]['espn_player_id'].num_games()
td[i]['espn_player_id'].max_QBR()