动态更改表示 class 对象的实例变量 - Python

Dynamically changing an instance variable that represents a class object- Python

我有一个 class 对象,我将其定义为另一个 class 对象的实例变量。我希望能够动态更改变量对象(如下所述)。我明白为什么它不起作用(至少,我想我知道.. python 使用对象的字典样式修改所以这就像一个地方的变化?),但只是想找出解决办法..

我想如果我在循环中创建对象就可以解决问题,但我没有运气..理想情况下,我不想在循环中创建它,因为这是一个非常简化的我的代码版本,在我的实际代码中,对象的创建需要 5 分钟(很多 data/modifications 在初始化时被调用到 运行)。所以如果我必须创建它在循环中,每次迭代需要 5 分钟……这真的不理想。

这是我的完整代码:

class Employee:
    def __init__(self, name, unscheduled, shifts, hours_scheduled, appointments_with):
        self.name = name
        self.unscheduled = unscheduled
        self.shifts = shifts
        self.hours_scheduled = hours_scheduled
        self.appointments_with = appointments_with

    def drop_shift(self, person, hours):
        hours_with = self.shifts[person]
        new_hours = [x for x in hours_with if hours[0] not in x]
        self.shifts[person] = new_hours
        new_unscheduled = [x for x in hours_with if hours[0] in x]
        self.unscheduled = self.unscheduled + new_unscheduled[0]
        for person in list(self.shifts.keys()):
            if len(self.shifts[person]) == 0:
                del self.shifts[person]
                del self.appointments_with[self.appointments_with.index(person)]
        return self

    def add_shift(self, person, hours):
        self.unscheduled = [x for x in self.unscheduled if x not in hours]
        if person in list(self.shifts.keys()):
            self.shifts[person] = self.shifts[person] + hours
        else:
            self.shifts[person] = hours
            self.appointments_with = self.appointments_with + [person]
        self.hours_scheduled = self.hours_scheduled + hours
        return self


class Schedule:
    def __init__(self, all_employees):
        self.employees = {}
        for name in list(all_employees.keys()):
            self.employees.update({name: Employee(name, all_employees[name]['unsched'], all_employees[name]['shifts'],
                                                  all_employees[name]['hours_sched'], all_employees[name]['appts'])})

    def add_shift(self, employee, person, hours):
        employ_obj = self.employees[employee]
        self.employees[employee] = employ_obj.add_shift(person, hours)
        return self

    def drop_shift(self, employee, person, hours):
        employ_obj = self.employees[employee]
        self.employees[employee] = employ_obj.drop_shift(person, hours)
        return self


def get_changes():
    employees = {
        'Joe': {'unsched': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'shifts': {'Mark': [[11, 12, 13, 14], [21, 22, 23, 24, 25]], 'Jack': [[15, 16, 17, 18, 19, 20]]}, 'hours_sched': [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25], 'appts': ['Mark', 'Jack']}}


    to_drop = [('Joe', 'Mark', [11, 12, 13, 14]), ('Joe', 'Jack', [15, 16, 17, 18, 19, 20])]
    new_schedules = []
    for drop in to_drop:
        Full_Sched = Schedule(employees)
        altered = Full_Sched.drop_shift(drop[0], drop[1], drop[2])
        new_schedules.append(altered)

    for new in new_schedules:
        print(new.employees['Joe'].unscheduled)
        print(new.employees['Joe'].shifts)
        print(new.employees['Joe'].hours_scheduled)
        print(new.employees['Joe'].appointments_with)

    return ()


if __name__ == '__main__':
    get_changes()

我得到的输出:

   > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
   > {'Mark': [[21, 22, 23, 24, 25]]}
   > [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
   > ['Mark']
   > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
   > {'Mark': [[21, 22, 23, 24, 25]]}
   > [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
   > ['Mark']

我想要的输出:

> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
> {'Mark': [[21, 22, 23, 24, 25]], 'Jack': [[15, 16, 17, 18, 19, 20]]}
> [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
> ['Mark', 'Jack']
> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 18, 19, 20]
> {'Mark': [[11, 12, 13, 14], [21, 22, 23, 24, 25]]}
> [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
> ['Mark']

除了一个地方,你的代码很完美

new_schedules.append(altered)

发生的事情是因为 altered 本身就是一个引用,对于

中的所有重复
for new in new_schedules:
        print(new.employees['Joe'].unscheduled)
        print(new.employees['Joe'].shifts)
        print(new.employees['Joe'].hours_scheduled)
        print(new.employees['Joe'].appointments_with)

您正在获取最后更新的值。

为了纠正这个问题,您可以使用复制模块

import copy
...
new_schedules.append(copy.deepcopy(altered))