I have a class I've made to hold all information about my data points. Data points are often collected in a series of measurements, so I want to keep them together: hence, DataPoints and LotsOfDataPoints
#this is simplified code, but reproduces my issue
class DataPoint:
def __init__(self, value):
self.value = value
def add_number(self, number):
self.value += number
def __repr__(self):
return str(self.value)
class LotsOfDataPoints:
def __init__ (self, *values):
self.lst = []
for value in values:
self.lst.append(DataPoint(value))
def add_one(self):
self.lst = [d.add_number(1) for d in self.lst]
I can successfully create a DataPoint:
d = DataPoint(1)
print(d)
> 1
d.add_number(1)
print(d)
> 2
I can successfully make LotsOfDataPoints:
ds = LotsOfDataPoints(1,2,3,4,5)
print(ds.lst)
> [1, 2, 3, 4, 5]
I can call the DataPoint method on an individual DataPoint
ds.lst[0].add_number(1)
print(ds.lst)
> [2, 2, 3, 4, 5]
However, when I try to add_one() to my LotsOfDataPoints, they all turn to None.
ds.add_one()
print(ds.lst)
> [None, None, None, None, None]
What have I done wrong here?
DataPoint.add_number()doesn't return anything; it's therefore pointless to use it in a list comprehension, and if you do so anyway it's useless to assign the resulting list of Nones anywhere.