|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
感觉 寻找着解决一个问题 很有趣啊!
课后题的答案:
import pickle
import os
class Record:
def __init__(self, name):
self.name = name
self.filename = self.name + '.pkl'
def __get__(self, instance, owner):
if self.value:
self.file = open(self.filename, 'rb')
with self.file as f:
value = pickle.load(f)
return value
else:
print('没有该值。。。')
def __set__(self, instance, value):
self.value = value
self.file = open(self.filename, 'wb' )
with self.file as f:
pickle.dump(self.value, f)
def __delete__(self, instance):
os.remove(self.filename)
class Test:
x = Record('x')
y = Record('y')
|
|