想请教一下
self.restore 和 settings.store 那里没看懂它们哪里跑出来的
实例化对象时为什么要执行self.restore()呢
from easygui import EgStore
class Settings(EgStore):
def __init__(self,filename):
self.author = ""
self.book = ""
self.filename = filename
self.restore()
settingsFilename = "settings.txt"
settings = Settings(settingsFilename)
author = '小甲鱼'
book = '《零基础入门学习Python》'
settings.author = author
settings.book = book
settings.store()
print('\n保存完毕\n')
settingsFilename = 'settings.txt'
settings = Settings(settingsFilename)
print(settings.author)
print(settings.book)
对 easygui 不怎么了解,刚刚去网上查了下没查到类似文章,去官方看了 easygui 的文档看了得到 restore 的说明文档:
restore()
Set the values of whatever attributes are recoverable from the pickle file.
Populate the attributes (the __dict__) of the EgStore object from the attributes (the __dict__) of the pickled object.
If the pickled object has attributes that have been initialized in the EgStore object, then those attributes of the EgStore object will be replaced by the values of the corresponding attributes in the pickled object.
If the pickled object is missing some attributes that have been initialized in the EgStore object, then those attributes of the EgStore object will retain the values that they were initialized with.
If the pickled object has some attributes that were not initialized in the EgStore object, then those attributes will be ignored.
IN SUMMARY:
After the recover() operation, the EgStore object will have all, and only, the attributes that it had when it was initialized.
Where possible, those attributes will have values recovered from the pickled object.
英语不行......,人机翻译了:
restore()
设置任何可从pickle文件恢复的属性的值。
从pickle对象的属性(__dict__)填充EgStore对象的属性(__dict__)。
如果pickle对象的属性已经在EgStore对象中初始化,那么这些EgStore对象的属性将被pickle对象中相应属性的值所取代。
如果pickle对象丢失了一些在EgStore对象中已经初始化的属性,那么这些EgStore对象的属性将保留初始化时的值。
如果pickle的对象有一些属性没有在EgStore对象中初始化,那么这些属性将被忽略。
总而言之:
在recover()操作之后,EgStore对象将拥有初始化时所拥有的全部且仅有的属性。
在可能的情况下,这些属性将具有从pickle对象中恢复的值。
|