Python:我不能为类属性分配一个已定义的列表?

时间:2017-01-16 14:47:54

标签: python computer-science

class Runner:
    """
    information of registered runners
    Attributes:
        @type email: str
        email of the registered runner
        @type category: str
        the speed they estimate that they can finish the race
        @type list: clist
        the list of runners in the same category

    """
    under_twenty_min = []
    under_thirty_min = []
    under_forty_min = []
    forty_and_above = []

    def __init__(self, email, category):
        """Register the email and the speed estimation of runners

            @type self: Runner
            @type email: str
            @type speed: int
            @type category:str
            @type clist: list
            @rtype: list

        >>>runner1=Runner('gerhard@mail.utoronto.ca','under 40 min')
        >>>runner1.email
        'gerhard@gmail.utoronto.ca'
        >>>runner1.category
        'under 40 min'
        """
        self.email = email
        self.category = category
        if category=='under 20 min':
            self.clist=under_twenty_min
        elif category=='under 30 min':
            self.clist = under_twenty_min
        elif  category=='under 40 min':
            self.clist = under_forty_min
        elif category=='40 min and over':
            self.clist = forty_and_over
        renew(self,clist)
        return clist

基本上我必须在初始化跑步者时返回具有相同速度类别的跑步者列表,但我不能将上面定义的列表分配给类属性,无论如何要修复它吗?

4 个答案:

答案 0 :(得分:4)

访问类变量时必须显式指定类:

if category == 'under 20 min':
    self.clist = Runner.under_twenty_min
elif category == 'under 30 min':
    self.clist = Runner.under_twenty_min
elif category == 'under 40 min':
    self.clist = Runner.under_forty_min
elif category == '40 min and over':
    self.clist = Runner.forty_and_over

答案 1 :(得分:1)

您可以使用dict,将每个catgory名称设置为键,值为该类别中所有参赛者的列表。
这是一个简单的实现

from collections import defaultdict

#runners is a list of all your runners

def get_runners_by_cat():
    d = defaultdict(list)
    for runner in runners:
        dict[runner.category].append(runner)
    return d

答案 2 :(得分:0)

您需要在代码中修改以下几项内容:

  1. init方法的缩进需要在类定义中缩进。
  2. 正如@Tamas所指出的,你需要明确声明变量是类变量。
  3. 正如@taoufik所提到的,改为定义字典可能比较方便。
  4. init方法不应返回任何内容(None除外)。由于您的要求是返回跑步者,您可以添加@taofik建议的其他方法(或者您可以在创建后打印它们,如下所示)。
  5. 这是代码的更新版本。希望它有所帮助。

    class Runner:
        """
        information of registered runners
        Attributes:
            @type email: str
            email of the registered runner
            @type category: str
            the speed they estimate that they can finish the race
            @type list: clist
            the list of runners in the same category
    
        """
        clist = {
            'under_twenty_min': [],
            'under_thirty_min': [],
            'under_forty_min':[],
            'forty_and_above': []
        }
    
        def __init__(self, email, category):
            """Register the email and the speed estimation of runners
    
                @type self: Runner
                @type email: str
                @type speed: int
                @type category:str
                    @type clist: list
                @rtype: list
    
            >>>runner1=Runner('gerhard@mail.utoronto.ca','under 40 min')
            >>>runner1.email
            'gerhard@gmail.utoronto.ca'
                >>>runner1.category
            'under 40 min'
            """
            self.email = email
            self.category = category
    
            if category=='under 20 min':
                Runner.clist['under_twenty_min'].append(self)
                print Runner.clist['under_twenty_min']
            elif category=='under 30 min':
                Runner.clist['under_thirty_min'].append(self)
                print Runner.clist['under_thirty_min']
            elif  category=='under 40 min':
                Runner.clist['under_forty_min'].append(self)
                print Runner.clist['under_forty_min']
            elif category=='40 min and over':
                Runner.clist['forty_and_above'].append(self)
                print Runner.clist['forty_and_above']
    

答案 3 :(得分:0)

#get class name, convert it to class object
if category=='under 20 min':
    self.clist= eval(self.__class__.__name__).under_twenty_min
elif category=='under 30 min':
    self.clist = eval(self.__class__.__name__).under_thirty_min
elif  category=='under 40 min':
    self.clist = eval(self.__class__.__name__).under_forty_min
elif category=='40 min and over':
    self.clist = eval(self.__class__.__name__).forty_and_above