鱼C论坛

 找回密码
 立即注册
查看: 1747|回复: 5

[已解决]魔法方法,简单定制

[复制链接]
发表于 2020-8-28 19:48:56 | 显示全部楼层 |阅读模式

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

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

x
哪位大神可以告诉我为啥总是提示在MyTime里面没有属性calc,以下是代码和运行结果
  1. import time as t

  2. class MyTimer():
  3.     #开始计时
  4.     def start(self):
  5.         self.start = t.localtime()
  6.         print("计时开始...")

  7.     #停止计时
  8.     def stop(self):
  9.         self.stop = t.localtime()
  10.         self._calc()
  11.         print("计时结束...")

  12.     #内部方法,计算运行时间
  13.         def _calc(self):
  14.             self.lasted = []
  15.             self.prompt = "总共运行了"
  16.             for index in range(6):
  17.                 self.lasted.append(self.stop[index] - self.start[index])
  18.                 self.prompt += str(self.lasted[index])

  19.             print(self.prompt)
复制代码




====================================== RESTART: C:\Users\john\Desktop\time.py =====================================
>>> t1 = MyTimer()
>>> t1.start()
计时开始...
>>> t1.stop()
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    t1.stop()
  File "C:\Users\john\Desktop\time.py", line 12, in stop
    self.calc()
AttributeError: 'MyTimer' object has no attribute 'calc'  
最佳答案
2020-8-28 21:14:04
南有暖树 发表于 2020-8-28 20:21
还是我太蠢了,谢谢大佬

问题解决了话,就设个最佳把求助帖关了吧,给你回复的几个,都很快就发现了你的代码的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-28 19:52:51 | 显示全部楼层
缩进问题,_calc()定义在stop()函数里面了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-28 19:53:41 | 显示全部楼层
  1. import time as t

  2. class MyTimer():
  3.     #开始计时
  4.     def start(self):
  5.         self.start = t.localtime()
  6.         print("计时开始...")

  7.     #停止计时
  8.     def stop(self):
  9.         self.stop = t.localtime()
  10.         self._calc()
  11.         print("计时结束...")

  12.     #内部方法,计算运行时间
  13.     def _calc(self):
  14.         self.lasted = []
  15.         self.prompt = "总共运行了"
  16.         for index in range(6):
  17.             self.lasted.append(self.stop[index] - self.start[index])
  18.             self.prompt += str(self.lasted[index])

  19.         print(self.prompt)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-28 19:55:26 | 显示全部楼层
  1. import time as t

  2. class MyTimer():
  3.     #开始计时
  4.     def start(self):
  5.         self.start = t.localtime()
  6.         print("计时开始...")

  7.     #停止计时
  8.     def stop(self):
  9.         self.stop = t.localtime()
  10.         self._calc()
  11.         print("计时结束...")

  12.     #内部方法,计算运行时间
  13.     def _calc(self):
  14.         self.lasted = []
  15.         self.prompt = "总共运行了"
  16.         for index in range(6):
  17.             self.lasted.append(self.stop[index] - self.start[index])
  18.             self.prompt += str(self.lasted[index])

  19.         print(self.prompt)

  20. t1 = MyTimer()
  21. t1.start()
  22. t1.stop()
复制代码

好像是你没对齐,把def _calc这个对齐了,就能运行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-8-28 20:21:45 | 显示全部楼层
疾风怪盗 发表于 2020-8-28 19:55
好像是你没对齐,把def _calc这个对齐了,就能运行

还是我太蠢了,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-28 21:14:04 | 显示全部楼层    本楼为最佳答案   
南有暖树 发表于 2020-8-28 20:21
还是我太蠢了,谢谢大佬

问题解决了话,就设个最佳把求助帖关了吧,给你回复的几个,都很快就发现了你的代码的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-26 08:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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