创建一个类列表并从另一个类访问它

时间:2016-06-05 15:45:03

标签: python list class attributes

我需要解决的是:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^member/profile/PARAMETER(.*)$ http://xxx.tld/PARAMETER$1 [r=301,nc]

我觉得这有点容易,但我仍然无法理解。

  1. 如何准确列出分数?
  2. 完成上述操作后,如何访问每位玩家的分数列表?
  3. 谢谢!

    我尝试了这个,但我知道我的做法是错误的。

    Create class "Scores" and "Player". 
    Class score should have attributes level, score, time. 
    Class player should have as information a name and a list of Scores, 
    In class player implement the method maxLevel(), 
        which returns the max level achieved by the player.
    

2 个答案:

答案 0 :(得分:0)

我建议创建代表一个对象而不是它们列表的类。我会更改课程的名称"分数"到"分数"然后我会制作尽可能多的得分对象。然后我可以创建一个名为score的列表。现在将每个得分对象附加到得分列表中。现在要访问每个播放器类型Player.scores的分数,这将显示整个列表,Players.scores [1]将选择列表中的第二个元素。

c

答案 1 :(得分:0)

我认为主要问题是您已将level, score, time定义为静态类变量。

class Score():
    #level = 0  "these should not be here"
    #score = 0
    #time = 0

    def __init__(self,level,score,time):
        #you are not defining them for a class, but for a class instance, so each individual instance of the object score
        self.level = level  
        self.score = score
        self.time = time

class Player():
    def __init__(self, name, scores):
        self.name = name
        self.scores = scores
    def maxLevel():
        ##Do stuff to calculate the max


John = Player("John", [Score(100,1456,50), Score(210,1490,100)])


John.maxLevel()

此外,如果分数类没有任何其他属性或特殊方法,请考虑使用namedtuple类。它更好地用于简单目的。

from collections import namedtuple

Score = namedtuple('Score', ['level', 'score', 'time'])