I am trying to make a python class that keeps a list of uniformly distributed random numbers per class but the numbers must be unique to that class. However the list in each new object is copied exactly as is from the first object and if I manually append new values to the contents of the second objects list, the same change is applied to the first object's list and for every new class I create the list is again copied and it appends however many values I specified.
The code is as follows:
import random as random
class myClass:
myRandomList = []
def __init__(self, lengthOfList):
for i in range(0,lengthOfList):
self.myRandomList.append(random.uniform(-1.0,1.0))
a = myClass(2) #make a list with 2 random numbers
b = myClass(1) #make a list with 1 random number
print(a.myRandomList)
print(b.myRandomList)
The output of the above code gives the following:
[0.3640932307078235, 0.8858169279430881, 0.18712932281033723]
[0.3640932307078235, 0.8858169279430881, 0.18712932281033723]
When it should be giving something like this rather:
[0.3640932307078235, 0.8858169279430881]
[0.18712932281033723]
How can I make it so that when a new class is created it creates an entirely new list of values for that class only without copying the list from a previously instantiated object and without affecting previous object's lists?
classsynonymously withobject. You should edit your post for clarification.