I've got a list of objects of type Person
. Each person
in the list has a report
field. I want to iterate over my list of people, and as I do I want to individually update each person's report. The code for this is as follows:
class People:
def __init__(self, name, age, report = list()):
self.name = name
self.age = age
self.report = report
def get_all_people():
p = []
p.append(People('Eve', 25))
p.append(People('Fred', 19))
p.append(People('Gale', 104))
p.append(People('Harry', 39))
return p
def list_of_people():
friends = ['alice', 'bob', 'carla', 'dave']
people_list = get_all_people()
for person in people_list:
person.report.append(f'This is the first line I added for {person.name}')
for i, friend in enumerate(friends):
person.report.append(f'{friend} is {person.name}s {str(i)} friend')
print(f'FRIEND REPORT FOR: {person.name}\n {person.report}')
if __name__ == '__main__':
list_of_people()
However, when I run this I get the following output:
FRIEND REPORT FOR: Eve
['This is the first line I added for Eve', 'alice is Eves 0 friend', 'bob is Eves 1 friend', 'carla is Eves 2 friend', 'dave is Eves 3 friend']
FRIEND REPORT FOR: Fred
['This is the first line I added for Eve', 'alice is Eves 0 friend', 'bob is Eves 1 friend', 'carla is Eves 2 friend', 'dave is Eves 3 friend', 'This is the first line I added for Fred', 'alice is Freds 0 friend', 'bob is Freds 1 friend', 'carla is Freds 2 friend', 'dave is Freds 3 friend']
FRIEND REPORT FOR: Gale
['This is the first line I added for Eve', 'alice is Eves 0 friend', 'bob is Eves 1 friend', 'carla is Eves 2 friend', 'dave is Eves 3 friend', 'This is the first line I added for Fred', 'alice is Freds 0 friend', 'bob is Freds 1 friend', 'carla is Freds 2 friend', 'dave is Freds 3 friend', 'This is the first line I added for Gale', 'alice is Gales 0 friend', 'bob is Gales 1 friend', 'carla is Gales 2 friend', 'dave is Gales 3 friend']
FRIEND REPORT FOR: Harry
['This is the first line I added for Eve', 'alice is Eves 0 friend', 'bob is Eves 1 friend', 'carla is Eves 2 friend', 'dave is Eves 3 friend', 'This is the first line I added for Fred', 'alice is Freds 0 friend', 'bob is Freds 1 friend', 'carla is Freds 2 friend', 'dave is Freds 3 friend', 'This is the first line I added for Gale', 'alice is Gales 0 friend', 'bob is Gales 1 friend', 'carla is Gales 2 friend', 'dave is Gales 3 friend', 'This is the first line I added for Harry', 'alice is Harrys 0 friend', 'bob is Harrys 1 friend', 'carla is Harrys 2 friend', 'dave is Harrys 3 friend']
From looking at this you can see that each report seems to start where the last one left off. Why is the compiler doing this? And, importantly, what do I need to do to my code to make it update each list individually?
Thanks!