鱼C论坛

 找回密码
 立即注册
查看: 3090|回复: 0

[萌新报道] python 46课1题 小甲鱼源代码感觉有误

[复制链接]
发表于 2021-6-20 09:07:43 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 huyanmin 于 2021-6-20 09:08 编辑
  1. import time

  2. class Record:
  3.     def __init__(self, initval=None, name=None):
  4.         self.val = initval
  5.         self.name = name
  6.         self.filename = "record48-9.txt"

  7.     def __get__(self, instance, owner):
  8.         with open(self.filename, 'a',encoding='utf-8') as f:
  9.             f.write("%s 变量于北京时间 %s 被读取,%s = %s\n" % \
  10.                     (self.name, time.ctime(), self.name, str(self.val)))
  11.         return self.val

  12.     def __set__(self, instance, value):
  13.         filename = "%s_record.txt" % self.name                        #这条语句感觉没有任何作用,我去掉了这条语句,结果不变
  14.         with open(self.filename, 'a',encoding='utf-8') as f:
  15.             f.write("%s 变量于北京时间 %s 被修改, %s = %s\n" % \
  16.                     (self.name, time.ctime(), self.name, str(value)))
  17.         self.val = value


  18. class Test:
  19.         x = Record(10, 'x')
  20.         y = Record(8.8, 'y')

  21. test =Test()
  22. print(test.x)
  23. print(test.y)
  24. test.x=123
  25. test.x =1.89
  26. test.y ='I love python'
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 13:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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