This code is creating a lot of data in stockTemp.stockValues, instead of just one data per stockTemp.
Please help, thanks in advance
class StockValues:
def __init__ (self, DD, MM, YYYY, averageValue):
self.date = map (str, [DD, MM, YYYY])
self.averageValue = averageValue
class Stock:
stockValues = []
def __init__ (self, name, code, currency):
self.name = name
self.code = code
self.currency = currency
Stocks = []
for index, stock in enumerate(StocksBufferToMemory):
#print ("{0:.0f}%".format(float(index)/len(StocksBufferToMemory) * 100))
stockTemp = Stock (stock.name, stock.code, stock.currency)
stockTempValues = StockValues (stock.date[0], stock.date[1], stock.date[2], stock.averageValue)
stockTemp.stockValues.append (stockTempValues)
Stocks.append (stockTemp)
print (stockTempValues)
EDIT1: Explaining the software: I'm taking a .txt files full of stock prices during a certain year, each stock has one price per day, so the idea is to create a list of stocks and each stock has a list of prices according to the day, my problem is that I'm using only one day of values to test and each stock ended up with multiplies entries for values. I've printed the mem address to see locate which variable as causing the problem, since it is a for loop it should change the memory address every time it iterates and stockTemp.stockValues isn't changing
Thanks @jasonharper for the answer, the problem is that my stockValues variable is a class attribute (same value for all class instances) instead of a instance attribute. Code fixed:
class Stock:
def __init__ (self, name, code, currency):
self.name = name
self.code = code
self.currency = currency
self.stockValues = []