Python 3.x 如何在迭代期间单独附加类对象列表中包含的列表

Python 3.x 如何在迭代期间单独附加类对象列表中包含的列表,python-3.x,Python 3.x,我有一个Person类型的对象列表。列表中的每个人员都有一个报告字段。我想反复浏览我的人员列表,并且像我一样,我想逐个更新每个人的报告。其代码如下: class People: def __init__(self, name, age, report = list()): self.name = name self.age = age self.report = report def get_all_people(): p = [

我有一个
Person
类型的对象列表。列表中的每个
人员
都有一个
报告
字段。我想反复浏览我的人员列表,并且像我一样,我想逐个更新每个人的报告。其代码如下:

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()
但是,当我运行此命令时,会得到以下输出:

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']
从这里你可以看到,每一份报告似乎都是从最后一份报告开始的。编译器为什么要这样做?而且,重要的是,我需要对我的代码做什么才能让它单独更新每个列表


谢谢

我正在发布这篇文章,以防其他人也有同样的问题。我从ForceBru的评论中得到了解决方案,它将我引向了上一个Stackoverflow问题。答案基本上是修改
Person
类,将
report
变量的默认值从
list()
更改为
None
。然后检查该值是否为
None
,如果是,则将其设置为列表。代码如下

class Person:
    def __init__(self, name, age, report = None):
        self.name = name
        self.age = age
        if report is None:
            self.report = []

def get_all_people():
    p = []
    p.append(Person('Eve', 25))
    p.append(Person('Fred', 19))
    p.append(Person('Gale', 104))
    p.append(Person('Harry', 39))
    return p
    
def list_of_clients():
    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_clients()

这回答了你的问题吗?有点,但不清楚如何修改代码来解决这个问题?我实际使用的数据是从数据库中提取的,所以我将它们提取出来,将它们转换为对象,然后通过迭代传递它们。一个解决方案可能是将它们作为字典处理,但这似乎相当混乱。有什么想法吗?@ForceBru-brilliant,谢谢!因此,我在类定义中将默认值从
List
更改为
None
,并测试它是否为
None
,然后将其设置为列表。如果你在这里留下答案,我会把它标记为答案。我认为这对任何有同样问题的人都有帮助。您最初共享的链接有很多信息需要在您找到解决方案之前处理。