将class属性作为函数参数传递?

时间:2017-02-14 19:22:39

标签: python

我可以传递我要查找的变量名作为参数吗?在下面的def中,stat参数用于更新函数中的 .stat 。 (见箭头)我一直收到错误:

 print("game result :" +(self._playerHxDic[name][str(i)].stat))
  

属性错误:' GameHx'对象没有属性' stat'

def getStats(self,name,num_of_games,stat):
    games = self._playerHxDic[name].keys()
    gameNums = []
    for i in games:
        gameNums.append(int(i))
    gameNums.sort()
    if len(gameNums) > num_of_games:
        gamelist = [int(x) for x in gameNums[-num_of_games:]]
        points = 0
        for i in gamelist:
            points += statDicint(self._playerHxDic[name][i].stat)#<-- 
        return points/num_of_games
    else: return None

1 个答案:

答案 0 :(得分:0)

getattr(self._playerHxDic[name][i], stat)

Documentation of getattr()