I have two classes Test_1
and Test_2
where test_2
includes the method setValues
.
class Test_1():
def __init__(self):
self.dim = float
self.list = Test_2()
def init(self, dim, valuesT, valuesW):
self.dim = dim
self.list = [Test_2()] * dim
for i in range(self.dim):
self.list[i].setValues(mean[i], valuesT[i], valuesW[i])
class Test_2():
def __init__(self):
self.mean = float
self.T = float
self.W = float
def setValues(self, mean, T, W):
self.mean = mean
self.T = abs(mean - T)
self.W = abs(W - mean)
When I create an instance of Test_1
and call the init
method of this class passing the params (3,[1,2,3],[3,4,5], [6,7,8])
I am expecting the creation of 3 instances of Test_2
class and for the T and W values to be initialised in the list objects. However this is not the case and the T and W values seem to be overwritten for all 3 Test_2
objects to the latest T and W values. The below code produces the values:
1 3 6
6 6 6
for i in range(self.dim):
self.list[i].setValues(mean[i], T[i], W[i])
print("List while in loop: ", self.list[i].mean)
for i in range(self.dim):
print("List after loop: ", self.list[i].mean)
Could anyone help shed some light on why this may be? I'm fairly new to OOP.