动态更改表示类对象的实例变量-Python
我有一个类对象,我将它定义为另一个类对象的实例变量。我希望能够动态地更改变量对象(如下所述)。我理解为什么它不起作用(至少,我想我是这样做的..python使用字典式的对象修改,所以这就像是一个改变?),但我只是想找出一个解决办法 我想如果我在循环中创建对象,它会解决问题,但我没有运气。。理想情况下,我不想在循环中创建它,因为这是我的代码的一个真正简化版本,在我的实际代码中,创建对象需要5分钟(初始化时会调用大量数据/修改来运行)。。因此,如果我必须在循环中创建它,那么每次迭代将需要5分钟。。。这真的不太理想 以下是我的完整代码:动态更改表示类对象的实例变量-Python,python,class,object,instance-variables,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']
你的代码是完美的,除了一个地方
new_schedules.append(altered)
所发生的事情是,因为changed
本身就是一个参考,对于所有的重复都是如此
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))
我已经盯着这个看了好几个小时了,你现在是我真正的英雄了,谢谢@ReblochonMasque我是一个新用户,所以我对他投了票,但我的投票在我获得一定分数之前不会公开显示。当时我没有选择接受这个答案,但后来我接受了——谢谢你的帮助!