如何使用用户定义的类实例填充python中的列表?

时间:2011-05-16 07:43:50

标签: python

所以我试图用我自己定义的类的实例填充列表,但是当我尝试访问列表中的任何元素时,我一直收到错误。代码如下。

任何帮助将不胜感激!

class Population:
"""total population of workers"""
    workerl = []

    def __init__(self, p, workers):
        # probability of a worker becoming unemployed
        # low in booms, high in recessions, could be a proxy for i
        self.p = p
        self.workers = workers
        workerl = []
        for i in range(workers):
            print i #test
            x = Worker()
            workerl.append(x)

p = 0
workers = 0

def showUR():
    """displays number of unemployed workers in a population"""
    ur = 0
    for worker in workers:
        if worker.isemployed == true:
            ur = ur + 1
    print ur

def advance(time, p):
    """advances one unit of time"""



# population as an array of workers     
class Worker:
    """a worker in a population"""
    isemployed = True

x = Population(.2, 100) 
print x.p
print x.workerl
if x.workerl[0].isemployed:
    print "worker 1 is employed"

2 个答案:

答案 0 :(得分:1)

你的程序在很多层面都存在缺陷。

class Population:
"""total population of workers""" # <-- indentation error
    workerl = [] # <-- class attribute, never used

    def __init__(self, p, workers):
        # probability of a worker becoming unemployed
        # low in booms, high in recessions, could be a proxy for i
        self.p = p
        self.workers = workers
        workerl = [] # <-- local variable, should be self.workerl
        for i in range(workers):
            print i #test
            x = Worker()
            workerl.append(x)

p = 0 # <-- module variable, never used
workers = 0 # <-- module variable, never used

def showUR(): # <-- should be a method of population
    """displays number of unemployed workers in a population""" # <-- does the opposite, i.e., shows the number of EMPLOYED workers
    ur = 0
    for worker in workers: # should be self.workerl, not workers
        if worker.isemployed == true: # <-- typo, should be: True
            ur = ur + 1
    print ur

def advance(time, p):
    """advances one unit of time"""



# population as an array of workers # <-- Python lists are not arrays
class Worker:
    """a worker in a population"""
    isemployed = True # <-- class atrribute, when set to False ALL workers become unemployed at once

x = Population(.2, 100) 
print x.p
print x.workerl
if x.workerl[0].isemployed:
    print "worker 1 is employed"

这就是你的程序应该是这样的(没有评论):

class Worker(object):

    def __init__(self):
        self.is_employed = True


class Population(object):

    def __init__(self, probability, number_of_workers):
        self.probability = probability
        self.workers = [Worker() for each in range(number_of_workers)]

    def showUR(self):
        print sum(not worker.is_employed for worker in self.workers)


x = Population(.2, 100)
print x.probability
print len(x.workers)
print x.workers
if x.workers[0].is_employed:
    print "worker 1 is employed"

答案 1 :(得分:0)

您创建了函数局部变量workerl,正确填充并忘记了它。

请记住,您始终必须编写self.(或任何实例参数)来访问python中的成员。您也可以通过实例读取类成员,因此self.workerl / x.workerl会对其进行评估,但要设置它,您必须调用Population.workerl =,因为self.workerl =会覆盖它实例。