零基础python046课后作业
import timeclass Record:
def __init__(self, initval=None, name=None):
self.val = initval
self.name = name
self.filename = "record.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')
问题见#
#这一行是不是可有可无的?有作用的话,作用是啥?我没看出来
确实代码中没有用到这段代码。那么就是可有可无吧!
页:
[1]