动态更改代表类对象的实例变量-Python

时间:2019-06-14 04:15:44

标签: python class object instance-variables

我有一个类对象,我将其定义为另一个类对象的实例变量。我希望能够动态更改变量对象(如下所述)。我了解为什么它不起作用(至少,我认为我可以。.python使用对象的字典样式修改功能,是否像更改位置一样?),但只是想办法解决。.

我认为,如果我在循环内创建对象可以解决问题,但是我没有运气..理想情况下,我希望不必在循环中创建它,因为这是的真正简化版本我的代码,在我的实际代码中,对象的创建需要5分钟(初始化时会调用很多数据/修改来运行)..因此,如果我必须在循环中创建它,则需要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']

1 个答案:

答案 0 :(得分:2)

除了一个地方,您的代码都是完美的

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))