import time as t#引用模块写在里面不能用,相当于只存储起来,需要放在外面
class Record:
#引入时间模块
def __init__(self,value,variable_name):
self.value=value#操作的值
self.variable_name=variable_name#操作的变量名(实例化对象的名字)
self.data='{} 变量于北京时间 {} 被创建,{}={}'.format(self.variable_name,self.get_clock(),self.variable_name,self.value)
with open('C:\\Users\\Liliya\\Desktop\\record.txt','a') as f :
f.write(self.data+'\n')
def get_clock(self):#获取时间
return (t.asctime(t.localtime()))
def __get__(self,instance,owner):
self.data='{} 变量于北京时间 {} 被读取,{}={}'.format(self.variable_name,self.get_clock(),self.variable_name,self.value)
with open('C:\\Users\\Liliya\\Desktop\\record.txt','a') as f :
f.write(self.data+'\n')
return self.value
def __set__(self,instance,value):
self.value=value
self.data='{} 变量于北京时间 {} 被修改,{}={}'.format(self.variable_name,self.get_clock(),self.variable_name,self.value)
with open('C:\\Users\\Liliya\\Desktop\\record.txt','a') as f :
f.write(self.data+'\n')
def __delete__(self,instance):
del self.value
self.data='{} 变量于北京时间 {} 被删除'.format(self.variable_name,self.get_clock())
with open('C:\\Users\\Liliya\\Desktop\\record.txt','a') as f :
f.write(self.data+'\n')
#主程序
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.x = 'hello world'
|