鱼C论坛

 找回密码
 立即注册
查看: 2524|回复: 1

求解为什么我的str函数不能打印出return的值

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

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

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

x
  1. Traceback (most recent call last):
  2.   File "C:\Users\945\Desktop\MyTimer.py", line 3, in <module>
  3.     class MyTimer():
  4.   File "C:\Users\945\Desktop\MyTimer.py", line 7, in MyTimer
  5.     __repr__==__str__
  6. NameError: name '__repr__' is not defined
  7. >>>
  8. ================== RESTART: C:\Users\945\Desktop\MyTimer.py ==================
  9. >>> t1 = MyTimer
  10. >>> t1
  11. <class '__main__.MyTimer'>
  12. >>> print(t1)
  13. <class '__main__.MyTimer'>
  14. >>> print(t1)
  15. <class '__main__.MyTimer'>
  16. >>>
复制代码

以上为报错
  1. import time as t

  2. class MyTimer():
  3.     def __str__(self):
  4.         return self.info
  5.    
  6.     def __init__():
  7.         self.info = '请先调用start()开始计时'
  8.         self.begin = 0
  9.         self.end = 0
  10.         self.unit = ['年','月','天','小时','分','秒']
  11.    
  12.     def start(self):
  13.         self.begin = t.localtime()
  14.         self.info = '请调用stop()停止计时'
  15.         print("计时开始")

  16.     def stop(self):
  17.         if not self.begin:
  18.             print("请先调用start()开始计时")
  19.         else:
  20.             self.end = t.localtime()
  21.             print("计时结束")
  22.             self._calc()
  23.     def _calc(self):
  24.         self.leasted = []
  25.         for index in range(6):
  26.             self.leasted.append(self.end[index] - self.begin[index])
  27.         self.info = "总共运行了" + str(self.leasted[index]) + self.unit[index]
  28.         print(self.info)
  29.             
复制代码

这个是我写的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-21 10:01:23 | 显示全部楼层
是我没有在__init__的括号里加self
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-16 22:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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