|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:
import time as t
class Record:
def __init__(self,val,arg):
self.arg = val
def __get__(self,instance,owner):
time = t.localtime()
ti = t.asctime(time)
return self.arg
with open('record.txt','a',encoding='utf-8') as f:
f.write(ti)
def __set__(self,instance,value):
self.t2 = []
self.t2.append(t.asctime([t]))
self.arg = value
按照以下方式实现的:
>>> class Test:
x = Record(10, 'x')
y = Record(8.8, 'y')
>>> test = Test()
>>> test.x
10
>>> test.y
8.8
但是却没有record文件生成,这是为什么呢?哪里出错了呢?
- return self.arg
- with open('record.txt','a',encoding='utf-8') as f:
- f.write(ti)
复制代码
把 return这句 放到这个函数的最后面
- with open('record.txt','a',encoding='utf-8') as f:
- f.write(ti)
- return self.arg
复制代码
|
|