python 46课1题 小甲鱼源代码感觉有误
本帖最后由 huyanmin 于 2021-6-20 09:08 编辑import time
class Record:
def __init__(self, initval=None, name=None):
self.val = initval
self.name = name
self.filename = "record48-9.txt"
def __get__(self, instance, owner):
with open(self.filename, 'a',encoding='utf-8') as f:
f.write("%s 变量于北京时间 %s 被读取,%s = %s\n" % \
(self.name, time.ctime(), self.name, str(self.val)))
return self.val
def __set__(self, instance, value):
filename = "%s_record.txt" % self.name #这条语句感觉没有任何作用,我去掉了这条语句,结果不变
with open(self.filename, 'a',encoding='utf-8') as f:
f.write("%s 变量于北京时间 %s 被修改, %s = %s\n" % \
(self.name, time.ctime(), self.name, str(value)))
self.val = value
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.89
test.y ='I love python'
页:
[1]