2
我有一个简单的类来存储简单的数据。这个班级如下。类对象的动态实例互相覆盖
class DataFormater:
def __init__(self, N, P, K, price):
self.N = N
self.P = P
self.K = K
self.price = price
调用这个类是
from DataFormater import DataFormater
#global variables
ObjectList = [0,1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50]
ObjectListCounter = 0
# main
print "enter you N-P-K values, followed by a coma, then the price"
print "example ----> 5 5 5 %50 "
print "return as many values as you want to sort, then enter, 'done!' when done."
while True:
RawData = raw_input()
if RawData == 'done!':
break
else:
ObjectList[ObjectListCounter] = DataFormater
ObjectList[ObjectListCounter].N = int(RawData[0])
# very simple test way of putting first indice in ObjectList[ObjectListCounter].N
ObjectListCounter += 1
print ObjectList[0].N
print ObjectList[1].N
我的想法是,ObjectList[0]
将创建一个对象“1”是我能与1.N
但是打电话,当我把这些代码,似乎我已经覆盖了以前的例子。
这是打印......
return as many values as you want to sort, then enter, 'done!' when done.
12
1
done!
1
1
非常感谢!我知道我的帖子很杂乱,我不知道如何让它更“漂亮”
'ObjectList [ObjectListCounter] = DataFormater.N AttributeError:class DataFormater没有属性'N''是什么让我吐口水。你有什么想法吗? – Nova
非常感谢。我很感激! – Nova
不用担心,很高兴我能帮到你! – MrName