老版零基础 46讲 动动手第1题
想增加删除属性的记录,在__delete__(self, instance)应该添加什么代码,现在del test.y删除不了。请各位大神指点下,谢谢了!代码如下:
import time
class Record:
def __init__(self, obj = None, name = None, fdel = None):
self.obj = obj
self.name = name
self.fdel = fdel
self.file = r'C:\Users\WL-GL-001-01\Desktop\test.txt'
def __get__(self, instance, owner):
with open(self.file, 'a') as f:
f.writelines('{}于北京时间{}被读取,{}={}'.format(self.name, time.ctime(), self.name, self.obj)+'\n')
return self.obj
def __set__(self, instance, value):
self.obj = value
with open(self.file, 'a') as f:
f.writelines('{}于北京时间{}被修改,{}={}'.format(self.name, time.ctime(), self.name, self.obj) + '\n')
#添加删除记录
def __delete__(self, instance):
with open(self.file, 'a') as f:
f.writelines('{}于北京时间{}被删除,{}={}'.format(self.name, time.ctime(), self.name, self.obj) + '\n')
#这里应该补什么代码,才能删除test的属性,比如test.y
class Test:
x = Record(10, 'x')
y = Record(8.8, 'y')
test =Test()
print(test.x)
print(test.y)
test.x = 123
test.x = 1.23
test.y = 'I love FishC.com!'
del test.y
test.y
delete 是描述符被删除时自动调用,你应该改成 delattr ,delattr才是属性被删除时自动调用
参考代码:
import time
class Record:
def __init__(self, obj=None, name=None, fdel=None):
self.obj = obj
self.name = name
self.fdel = fdel
self.file = r'C:\Users\WL-GL-001-01\Desktop\test.txt'
def __get__(self, instance, owner):
with open(self.file, 'a') as f:
f.writelines('{}于北京时间{}被读取,{}={}'.format(self.name, time.ctime(), self.name, self.obj) + '\n')
return self.obj
def __set__(self, instance, value):
self.obj = value
with open(self.file, 'a') as f:
f.writelines('{}于北京时间{}被修改,{}={}'.format(self.name, time.ctime(), self.name, self.obj) + '\n')
# 添加删除记录
def __delattr__(self, instance):
with open(self.file, 'a') as f:
f.writelines('{}于北京时间{}被删除,{}={}'.format(self.name, time.ctime(), self.name, self.obj) + '\n') Twilight6 发表于 2022-5-12 09:53
delete 是描述符被删除时自动调用,你应该改成 delattr ,delattr才是属性被删除时自动调用
参考代码 ...
改了以后报错
Traceback (most recent call last):
File "C:\Users\WL-GL-001-01\PycharmProjects\pythonProject\main.py", line 40, in <module>
del test.y
AttributeError: __delete__ kerln888 发表于 2022-5-12 10:11
改了以后报错
Traceback (most recent call last):
File "C:%users\WL-GL-001-01\PycharmProjects\py ...
是我错了,这里就是描述符类 Record,确实是重写 __delete__
在 __delete__ 下加上 self.__dict__.pop('name') 即可,参考代码:
import time
class Record:
def __init__(self, obj = None, name = None, fdel = None):
self.obj = obj
self.name = name
self.fdel = fdel
self.file = r'C:\Users\WL-GL-001-01\Desktop\test.txt'
def __get__(self, instance, owner):
with open(self.file, 'a') as f:
f.writelines('{}于北京时间{}被读取,{}={}'.format(self.name, time.ctime(), self.name, self.obj)+'\n')
return self.obj
def __set__(self, instance, value):
self.obj = value
with open(self.file, 'a') as f:
f.writelines('{}于北京时间{}被修改,{}={}'.format(self.name, time.ctime(), self.name, self.obj) + '\n')
def __delete__(self, instance):
with open(self.file, 'a') as f:
f.writelines('{}于北京时间{}被删除,{}={}'.format(self.name, time.ctime(), self.name, self.obj) + '\n')
self.__dict__.pop('name')
Twilight6 发表于 2022-5-12 10:28
是我错了,这里就是描述符类 Record,确实是重写 __delete__
在 __delete__ 下加上 self.__dic ...
谢谢版主 {:5_106:}
页:
[1]