|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import time
import os
os.chdir(r'C:\Users\黄腾\Desktop')
class Record:
def __init__(self,x,z):
self.x=x
self.promt=''
self.str=z
def __get__(self,instance,owner):
self.promt=self.str+'变量于北京时间'+str(time.asctime(time.localtime()))+'被读取,'+self.str+'='+str(self.x)+'\n'
with open('hh.txt','a') as self.f:
self.f.write(self.promt)
def __set__(self,instance,value):
self.x=value
self.promt=self.str+'变量于北京时间'+str(time.asctime(time.localtime()))+'被修改,'+self.str+'='+str(self.x)+'\n'
with open('hh.txt','a') as self.f:
self.f.write(self.promt)
class Test:
x=Record(10,'x')
y=Record(50,'y')
a=Test()
运行了几下 生成文件的内容是:
x变量于北京时间Sun Oct 7 11:21:52 2018被读取,x=10
y变量于北京时间Sun Oct 7 11:21:56 2018被读取,y=50
x变量于北京时间Sun Oct 7 11:31:05 2018被修改,x=21
y变量于北京时间Sun Oct 7 11:31:10 2018被修改,y=2
我不明白的是以'a'形式打开文件后面输入的字符串不是都应该在第一行的末尾么,他是怎么实现自动换行的 |
|