0

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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • `stockTemp.stockValues` is a list, so its address in memory is only the address of the first element (broadly)... an since you keep appending to it, the address of the head doesn't change. – TDk Sep 14 '17 at 13:28
  • 1
    How are you determining the memory address, and why do you even care? The point of higher-level languages like Python is that you don't need to muck around with low-level details like that. – jwodder Sep 14 '17 at 13:29
  • This seems very much like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What makes you think that looking at memory addresses in Python — a language that takes care of memory management for you — will tell you what's wrong with your code? – jwodder Sep 14 '17 at 13:42
  • Yep, definitely and XY Problem sorry for that, anyone got and idea on how to solve the code ? – Vinicius Ilidio Sep 14 '17 at 13:51
  • Wild guess (since you couldn't be bothered to show the actually relevant code): you have `stockValues` as a *class attribute* (rather than an *instance attribute*), so there's only one list shared by all instances of the `Stock` class. – jasonharper Sep 14 '17 at 13:56
  • @jasonharper thanks man, solved the problem, if you want to post an answer to help others – Vinicius Ilidio Sep 14 '17 at 14:07

1 Answers1

0

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 = []