I have the following class:
import random as rd
class Human():
def __init__(self,name,testList=[]):
self.name = name
self.testList = testList
def fill_list(self):
self.testList.append(rd.choices([0,1],weights= [0.75,0.25],k=10))
and a corresponding main file that initiates two instances of the class and puts them in a list:
from testtest import Human
foo = Human("Bar")
faa = Human("Bor")
check = []
check.append(foo)
check.append(faa)
for entry in check:
entry.fill_list()
for entry in check:
print(entry.name,entry.testList)
when I run the main file I get the following output:
Bar: [[0, 0, 0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]]
Bor: [[0, 0, 0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]]
Two questions:
- Why do both instances have the exact same list even-though they are randomly generated?
- At what point does it call the method twice per instance?
Thanks a lot for your feedback in advance!