|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import os
- import pickle
- class MyDes:
- saved = []
- def __init__(self, name = None):
- self.name = name
- self.filename = self.name + '.pkl'
- def __get__(self, instance, owner):
- if self.name not in MyDes.saved:
- raise AttributeError("%s 属性还没有赋值!" % self.name)
- with open(self.filename, 'rb') as f:
- value = pickle.load(f)
- return value
- def __set__(self, instance, value):
- with open(self.filename, 'wb') as f:
- pickle.dump(value, f)
- MyDes.saved.append(self.name)
- def __delete__(self, instance):
- os.remove(self.filename)
- MyDes.saved.remove(self.name)
复制代码
这是小甲鱼46讲的课后作业:第046讲:魔法方法:描述符(Property的原理) | 课后测试题及答案
https://fishc.com.cn/thread-56591-1-1.html
(出处: 鱼C论坛)
这是我的输入:
- class A:
- t=MyDes("hhh")
-
- t_=A()
- t_.t="hello"
- t_.t
- 'hello'
- MyDes.saved
- ['hhh']
- t_.t="hihihi"
- t_.t
- 'hihihi'
- MyDes.saved
- ['hhh', 'hhh']
复制代码
我就是将“hhh.pkl”里的内容换了,saved就有两个"hhh",是我的问题还是小甲鱼代码有漏洞
请求大佬帮助
hello? 发表于 2022-7-25 09:26
就相当于是文件名字没变,都是‘hhh.pkl’,不过我第一次将内容设置为“hello”(t_t=“hello”)
第二次 ...
是的,用 'w' 或 'wb' 打开任何文件都会导致这个文件被新建,文件的原有内容被永久清除。
|
|