2016-09-09 56 views
-2

新的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 

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

回答

0

问题是你用一个元素重复创建一个列表。 for循环之前,您创建列表,并在for循环中,您应该将元素附加到该列表。

cell_=[] 

for i in range(1,100): 

    cell_.append(a_cell()) 
+0

啊,谢谢。肯定表明我不知道Python或其方法。 – Mace

+0

如何访问,比如cell_ object索引号15,属性'kind'? – Mace

+0

您可以使用cell_ [index_number] .kind进行访问 – bekirzahid