我正在尝试实施医生的每周计划约会系统。我想知道如何使用货架来存储字典对象。 如果有人可以查看我的代码并提出我可以进一步改进它的建议,那就太好了。 如何将时隙字典存储在书架上? (我已经将星期一,星期二,星期三,星期四和星期五的对象附加到时隙字典中,现在不确定如何将这些对象在字典中存储到货架上。)
class default_timeslots:
def __init__(self,day,time):
self.__day = day
self.__time = time
class Store:
timeslots = {}
def __init__(self):
if len(__class__.timeslots) <= 0:
self.create_default_timeslots()
def create_default_timeslots(self):
Monday = default_timeslots('Mon',time={"8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00"})
Tuesday = default_timeslots('Tue',
time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
"17:00", "18:00"})
Wednesday = default_timeslots('Wed',
time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
"17:00", "18:00"})
Thursday = default_timeslots('Thursday',
time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
"17:00", "18:00"})
Friday = default_timeslots('Friday',
time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
"17:00", "18:00"})
Store.timeslots[Monday.day] = Monday
Store.timeslots[Tuesday.day] = Tuesday
Store.timeslots[Wednesday.day] = Wednesday
Store.timeslots[Thursday.day] = Thursday
Store.timeslots[Friday.day] = Friday
免责声明我是python和书架的初学者。
感谢您的帮助!
答案 0 :(得分:0)
我修复了您的代码,因为它无法运行:
class default_timeslots:
def __init__(self,day,time):
self.day = day
self.time = time
class Store:
def __init__(self):
self.timeslots = {}
self.create_default_timeslots()
def create_default_timeslots(self):
Monday = default_timeslots('Mon',time={"8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00"})
Tuesday = default_timeslots('Tue',
time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
"17:00", "18:00"})
Wednesday = default_timeslots('Wed',
time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
"17:00", "18:00"})
Thursday = default_timeslots('Thursday',
time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
"17:00", "18:00"})
Friday = default_timeslots('Friday',
time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
"17:00", "18:00"})
self.timeslots[Monday.day] = Monday
self.timeslots[Tuesday.day] = Tuesday
self.timeslots[Wednesday.day] = Wednesday
self.timeslots[Thursday.day] = Thursday
self.timeslots[Friday.day] = Friday
您试图在default_timeslots类内使用双下划线来使用私有属性,但这是一种不好的做法。 +您正在尝试在课堂外访问它们。我删除了下划线。
我不知道为什么要使用类属性和类似于类方法的方法。我让您阅读this以了解为什么类属性不会保存在您的书架中。这就是为什么我用实例属性替换了您的类属性。
类似地,当腌制类实例时,其类的代码和 数据不随它们一起腌制。仅实例数据是 腌制这是有目的的,因此您可以修复类中的错误或 向类添加方法,并仍然加载使用创建的对象 该课程的早期版本
要创建货架:
import shelve
store = Store()
output_shelve = shelve.open('test')
output_shelve['store'] = store
output_shelve.close()
要货架阅读:
input_shelve = shelve.open('test')
store = input_shelve['store']
当然,当您尝试从货架读取时(如果该内容不在同一脚本中),您需要导入Store类。
store.timeslots['Mon'].time
将返回:
{'10:00',
'11:00',
'12:00',
'13:00',
'14:00',
'15:00',
'16:00',
'17:00',
'18:00',
'8:00',
'9:00'}
答案 1 :(得分:0)