鱼C论坛

 找回密码
 立即注册
查看: 1361|回复: 8

[已解决]老版python046讲的第1题类属性的定义问题。

[复制链接]
发表于 2019-11-28 15:15:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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


  1. import time as t

  2. class Record :
  3.     time = None
  4.     mounth = ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
  5.    
  6.     def __init__(self, value, name):
  7.         self.val = value
  8.         self.name = name
  9.         open('record.txt', 'a')

  10.     def __get__(self, instance, owner):
  11.         time = t.localtime()
  12.         with open('record.txt', 'a') as f:
  13.             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))
  14.             print('已记录。')
  15.         return self.val
  16.         

  17.     def __set__(self, instance, value):
  18.         time = t.localtime()
  19.         self.val = value
  20.         with open('record.txt', 'a') as f:
  21.             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))
  22.             print('已记录。')
  23.         

  24.     def __delete__(self, instance):
  25.         time = t.localtime()
  26.         with open('record.txt', 'a') as f:
  27.             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]))
  28.             print('已记录。')
  29.         del self.val
  30.         print('已删除')

  31. class Test:
  32.         x = Record(10, 'x')
  33.         y = Record(8.8, 'y')
复制代码
最佳答案
2019-11-29 18:08:32
  1. def __get__(self, instance, owner):
  2.         // self.time = t.localtime() // 类定义中的time
  3.         time = t.localtime()  //局部变量time
  4.         // print(self.time == time)  // 不是同一个,输出 False
  5.         with open('record.txt', 'a') as f:
  6.             f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被读取,%s = %s\n' % (
  7.             self.name, self.mounth[time[1] - 1], time[2], time[3], time[4], time[5], time[0], self.name, self.val))
  8.             print('已记录。')
  9.         return self.val
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-11-28 15:39:11 | 显示全部楼层
mounth,还是month,我都没看代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-28 18:03:12 | 显示全部楼层
mounth 也要用 self.mounth
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-28 20:28:38 | 显示全部楼层
mounth 要改成 self.mounth,因为 mounth 是类属性。

  1. import time as t


  2. class Record:
  3.     time = None
  4.     mounth = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']

  5.     def __init__(self, value, name):
  6.         self.val = value
  7.         self.name = name
  8.         open('record.txt', 'a')

  9.     def __get__(self, instance, owner):
  10.         time = t.localtime()
  11.         with open('record.txt', 'a') as f:
  12.             f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被读取,%s = %s\n' % (
  13.             self.name, self.mounth[time[1] - 1], time[2], time[3], time[4], time[5], time[0], self.name, self.val))
  14.             print('已记录。')
  15.         return self.val

  16.     def __set__(self, instance, value):
  17.         time = t.localtime()
  18.         self.val = value
  19.         with open('record.txt', 'a') as f:
  20.             f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被修改,%s = %s\n' % (
  21.             self.name, self.mounth[time[1] - 1], time[2], time[3], time[4], time[5], time[0], self.name, self.val))
  22.             print('已记录。')

  23.     def __delete__(self, instance):
  24.         time = t.localtime()
  25.         with open('record.txt', 'a') as f:
  26.             f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被删除。\n' % (
  27.             self.name, self.mounth[time[1] - 1], time[2], time[3], time[4], time[5], time[0]))
  28.             print('已记录。')
  29.         del self.val
  30.         print('已删除')


  31. class Test:
  32.     x = Record(10, 'x')
  33.     y = Record(8.8, 'y')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-29 10:02:26 | 显示全部楼层
zltzlt 发表于 2019-11-28 20:28
mounth 要改成 self.mounth,因为 mounth 是类属性。

那为什么同样是类属性的time就没有问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-29 10:20:39 | 显示全部楼层
本帖最后由 lff 于 2019-11-29 10:26 编辑
牛牛pp 发表于 2019-11-29 10:02
那为什么同样是类属性的time就没有问题

楼上收的有错的,mounth 应该是实例属性,不是类属性
time 是你每个函数内的局部变量,不是实例属性的time
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-29 15:35:13 | 显示全部楼层
lff 发表于 2019-11-29 10:20
楼上收的有错的,mounth 应该是实例属性,不是类属性
time 是你每个函数内的局部变量,不是实例属性的ti ...

这里有点不太理解,两个东西都是定义在函数外面的,为什么一个可以作为局部变量,另一个就变成了实例属性必须加self.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-29 17:59:26 | 显示全部楼层
本帖最后由 lff 于 2019-11-29 18:04 编辑
牛牛pp 发表于 2019-11-29 15:35
这里有点不太理解,两个东西都是定义在函数外面的,为什么一个可以作为局部变量,另一个就变成了实例属性 ...
  1. import time as t


  2. class Record:
  3.     time = None   // 实例属性,需要 self.time访问,属于对象的
  4.     mounth = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']  //实例属性,需要 self.mounth访问,属于对象的

  5.     def __init__(self, value, name):
  6.         self.val = value
  7.         self.name = name
  8.         open('record.txt', 'a')

  9.     def __get__(self, instance, owner):
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-29 18:08:32 | 显示全部楼层    本楼为最佳答案   
  1. def __get__(self, instance, owner):
  2.         // self.time = t.localtime() // 类定义中的time
  3.         time = t.localtime()  //局部变量time
  4.         // print(self.time == time)  // 不是同一个,输出 False
  5.         with open('record.txt', 'a') as f:
  6.             f.write('%s 变量与北京时间 %s %s %s:%s:%s %s 被读取,%s = %s\n' % (
  7.             self.name, self.mounth[time[1] - 1], time[2], time[3], time[4], time[5], time[0], self.name, self.val))
  8.             print('已记录。')
  9.         return self.val
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 03:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表