I have a class like;
class csv_logger(object):
def __init__(self,totalsDict):
self.columns = list(totalsDict.keys())
self.bufferDict = dict.fromkeys(self.columns,0)
self.logDict = dict.fromkeys(self.columns,[0])
def log(self, newTotalsDict):
for key in self.columns:
self.logDict[key].append(newTotalsDict[key] - self.bufferDict[key])
self.bufferDict = copy.deepcopy(newTotalsDict)
When i initialize it and call "log" method afterwards, append operation in the for loop of the method applies for all the lists kept in this dict in each step of loop. In other words, assume that len(self.columns)=2 and both 2 lists in self.logDict has 1 element in its lists. As "log" method finishes for once, all of the lists assigned to keys of self.logDict gets 2 elements (which must be 1) more. And the appended values are the same too. But as i change the .append line as;
self.logDict[key] = self.logDict[key] + [newTotalsDict[key] - self.bufferDict[key]]
Works just fine, yielding lists with their lenths increased by 1 as expected. Would be great if somebody can explain.