鱼C论坛

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

python 44课 魔法方式:简单定制 疑惑求解

[复制链接]
发表于 2016-4-7 21:57:33 | 显示全部楼层 |阅读模式
3鱼币
  1. import time as t

  2. class MyTimer():
  3.         def __init__(self):
  4.                 self.unit = ['年','月','天','小时','分钟','秒']
  5.                 self.prompt = '未开始计时!'
  6.                 self.lasted = []
  7.                 self.begin = 0
  8.                 self.end = 0
  9.                
  10.         def __str__(self):
  11.                 return self.prompt

  12.         __repr__ = __str__

  13.         def __add__(self,other):
  14.                 prompt = '总共运行了'
  15.                 result = []
  16.                 for index in range(6):
  17.                         result.append(self.lasted[index] + other.lasted[index])
  18.                         if result[index]:
  19.                                 prompt += (str(result[index])) + self.unit[index]
  20.                 return prompt
  21.   
  22.         
  23.         #开始计时
  24.         def start(self):
  25.                 self.begin = t.localtime()
  26.                 self.prompt = '提示:请先调用stop()停止计时'
  27.                 print('开始计时...')

  28.         #停止计时
  29.         def stop(self):
  30.                
  31.                 if not self.begin:
  32.                     print('提示:请先调用start()进行计时')
  33.                 else:
  34.                      self.end = t.localtime()
  35.                      self._calc()
  36.                      print('停止计时...')

  37.         #内部方法计算运行时间
  38.         def _calc(self):
  39.                
  40.                 self.prompt = '总共运行了'
  41.                 for index in range(6):
  42.                         self.lasted.append(self.end[index] - self.begin[index])
  43.                         if self.lasted[index]:
  44.                                 self.prompt += str(self.lasted[index]) + self.unit[index]
  45.         
  46.                 #为下一轮计时初始化
  47.                 self.begin = 0
  48.                 self.end = 0
复制代码



有两个问题不解:
1、
  1. def __str__(self):
  2.                 return self.prompt

  3.         __repr__ = __str__
复制代码

    这个的功能应该是和直接写 __repr__ 一样的吧?为啥还要像上面那样写?
2、
  1. #为下一轮计时初始化
  2.                 self.begin = 0
  3.                 self.end = 0
复制代码

   这个不初始化也不影响啊,为什么还要初始化

最佳答案

查看完整内容

是的,但一般为了明晰,把两个方法都定义了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-4-7 21:57:34 | 显示全部楼层
伙夫和甲鱼 发表于 2016-4-9 14:56
1、
这一段的输出是:
所以__repr__就直接可以实现 t 和 print(t)的输出

是的,但一般为了明晰,把两个方法都定义了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-4-7 23:02:14 | 显示全部楼层
1.  __rept__ = __str__ 相当于
  1. def __rept__(self):
  2.                 return self.prompt
复制代码

2. 将两个值复位是良好的习惯,其实这里只用到了self.begin,你看看第35行的判断
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-4-9 11:12:11 | 显示全部楼层
冬雪雪冬 发表于 2016-4-7 23:02
1.  __rept__ = __str__ 相当于

2. 将两个值复位是良好的习惯,其实这里只用到了self.begin,你看看第35 ...

__repr__ 实现的功能 和
  1. def __str__(self):
  2.                 return self.prompt

  3.         __repr__ = __str__
复制代码
一样,为啥还要加上 __str__,  我的问题是这个意思

另外,不初始化也不影响35行的判断吧?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-4-9 14:22:54 | 显示全部楼层
伙夫和甲鱼 发表于 2016-4-9 11:12
__repr__ 实现的功能 和 一样,为啥还要加上 __str__,  我的问题是这个意思

另外,不初始化也不影响3 ...

1.见例子,这里把程序简化了。

  1. class MyTimer:
  2.     def __repr__(self):
  3.         return 'repr'
  4.     def __str__(self):
  5.         return 'str'

  6. t = MyTimer()
  7.   
复制代码


注意输出:
  1. >>> t
  2. repr
  3. >>> print(t)
  4. str
复制代码


2.运行过计算,如果不清零,则不执行start,先stop时35行判断为flase,不会提示请先调用start()进行计时
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-4-9 14:56:15 | 显示全部楼层
冬雪雪冬 发表于 2016-4-9 14:22
1.见例子,这里把程序简化了。

1、
  1. class MyTimer:
  2.         def __repr__(self):
  3.                 return 'repr'
复制代码

这一段的输出是:
  1. >>> t = MyTimer()
  2. >>> t
  3. repr
  4. >>> print(t)
  5. repr
复制代码

所以__repr__就直接可以实现 t 和 print(t)的输出

2、初始化的这个问题我明白了,如果是同一个实例化对象重复进行计时操作,不进行初始化的话就会有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-4-9 21:26:17 | 显示全部楼层
谢谢这个问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-4-10 17:37:22 | 显示全部楼层
谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-5-16 23:29:50 | 显示全部楼层
48行的if语句怎么解释啊,兄弟,赋值的过程是怎样的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-20 14:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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