操作类对象实例列表

时间:2016-09-09 17:14:24

标签: list object indexing attributes

Python新手,但对旧语言有经验:

class a_cell:
    'Basic class structure of a cell'
    def __init__(self):
        self.number = 0
        self.row = 1
        self.column = 1
        self.kind = "variable"
        self.value = 4

# -- cell_ is list of a_cell class objects  --
for i in range (1,100):
    cell_ =[a_cell()] # for i in range (1,100)]  
    print("cell_",str(i), cell_[i-1].kind)

 # do I need both of the "for i in range..." creatipm loops

**列表索引的错误超出了最后一行的范围以及如何访问我的对象属性?

1 个答案:

答案 0 :(得分:0)

问题是你一遍又一遍地创建一个包含一个元素的列表。在for循环之前,您创建列表,在for循环中,您应该将元素附加到该列表。

cell_=[]

for i in range(1,100):

    cell_.append(a_cell())