Динамическое изменение переменной экземпляра, которая представляет объект класса - Python - PullRequest
1 голос
/ 14 июня 2019

У меня есть объект класса, который я определяю как переменную экземпляра другого объекта класса.Я хочу иметь возможность динамически изменять объект переменной (как описано ниже).Я понимаю, почему это не работает (по крайней мере, я так думаю .. 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 Ответ

2 голосов
/ 14 июня 2019

Ваш код идеален, кроме одного места

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