|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 牛牛pp 于 2019-11-28 15:16 编辑
在写入的时候,参考答案直接写入了localtime的月份,我想到的解决办法是用一个列表把月份输入进去,在根据元素的序号调用。
但是运行程序的时候,却提示我说这个列表(mounth)没有被定义,但是我再类属性里确实定义了mounth[]啊。去掉这个列表单独写入time[1]是可以运行的。
不知道是哪里出了问题?
test.x
File "C:/Users/Administrator/Downloads/046.1.py", line 15, in __get__
f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被读取,%s = %s\n' % (self.name, mounth[time[1]-1], time[2], time[3], time[4], time[5], time[0], self.name, self.val))
NameError: name 'mounth' is not defined
- import time as t
- class Record :
- time = None
- mounth = ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
-
- def __init__(self, value, name):
- self.val = value
- self.name = name
- open('record.txt', 'a')
- def __get__(self, instance, owner):
- time = t.localtime()
- with open('record.txt', 'a') as f:
- f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被读取,%s = %s\n' % (self.name, mounth[time[1]-1], time[2], time[3], time[4], time[5], time[0], self.name, self.val))
- print('已记录。')
- return self.val
-
- def __set__(self, instance, value):
- time = t.localtime()
- self.val = value
- with open('record.txt', 'a') as f:
- f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被修改,%s = %s\n' % (self.name, mounth[time[1]-1], time[2], time[3], time[4], time[5], time[0], self.name, self.val))
- print('已记录。')
-
- def __delete__(self, instance):
- time = t.localtime()
- with open('record.txt', 'a') as f:
- f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被删除。\n' % (self.name, mounth[time[1]-1], time[2], time[3], time[4], time[5], time[0]))
- print('已记录。')
- del self.val
- print('已删除')
- class Test:
- x = Record(10, 'x')
- y = Record(8.8, 'y')
复制代码
- def __get__(self, instance, owner):
- // self.time = t.localtime() // 类定义中的time
- time = t.localtime() //局部变量time
- // print(self.time == time) // 不是同一个,输出 False
- with open('record.txt', 'a') as f:
- f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被读取,%s = %s\n' % (
- self.name, self.mounth[time[1] - 1], time[2], time[3], time[4], time[5], time[0], self.name, self.val))
- print('已记录。')
- return self.val
复制代码
|
|