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