有关python计时器的问题
小甲鱼的计时器:我的计时器:
import time as t
class Mytimer():
#定义普通方法:开始计时
def start(self):
self.start=t.localtime()
print('开始计时')
#定义普通方法:停止计时 并在结束的时候就得到过了多少时间
def stop(self):
self.stop=t.localtime()
self._calu()
print('停止计时')
#定义内部方法(__):计算时间差:时间的年月日时分秒都是相互独立的
#用序列来存储,用迭代来得到并计算
def _calu(self):
self.suboftime=[]#创建列表//得到的是 相差的年月日
self.prompt='总共运行了'
for index in range(6):#迭代计算得到年月日时分秒的差值 并用序列的方法append保存进序列
self.suboftime.append(self.stop-self.start) #star.stop 指的就是stop方法得到的时间的年/月/日/。。。
self.prompt += str(self.suboftime)
print(self.prompt)
A=Mytimer()
A.start()
A.stop()
结果: 怎么都还是0000
{:10_266:}有谁可以帮帮忙 本帖最后由 小思喵 于 2018-7-30 21:55 编辑
这是小甲鱼的 小甲鱼的代码是有时间差的 而我的却没有
import time as t
class Mytimer():
#打印结果 :魔法方法 __str__和__repr__
def __str__(self):
return self.prompt
__repr__ = __str__
#定义普通方法:开始计时
def start(self):
self.start=t.localtime()
print('开始计时')
#定义普通方法:停止计时 并在结束的时候就得到过了多少时间
def stop(self):
self.stop=t.localtime()
self._calu()
print('停止计时')
#定义内部方法(__):计算时间差:时间的年月日时分秒都是相互独立的
#用序列来存储,用迭代来得到并计算
def _calu(self):
self.suboftime=[]#创建列表//得到的是 相差的年月日
self.prompt='总共运行了'
for index in range(6):#迭代计算得到年月日时分秒的差值 并用序列的方法append保存进序列
self.suboftime.append(self.stop-self.start) #star.stop 指的就是stop方法得到的时间的年/月/日/。。。
self.prompt += str(self.suboftime)
A=Mytimer()
A.start()
A.stop()
你的start完了就stop了。程序运行很快的。你加上t.sleep(2) 甲鱼老师是实例运行后手敲的,有时间间隔 塔利班 发表于 2018-7-30 22:12
甲鱼老师是实例运行后手敲的,有时间间隔
我用你的方法改进了的确有效
不过我按照3楼甲鱼的代码巧
并没有 显示prompt的内容 这是为什么呢
而且我不明白 __repr__ = __str__有什么作用
__str__变成字符串用于打印
__repr__用于你直接敲实例名称做字符串显示 塔利班 发表于 2018-7-30 23:39
__str__变成字符串用于打印
__repr__用于你直接敲实例名称做字符串显示
那为什么我还是无法显示prompt的内容 小思喵 发表于 2018-7-30 23:46
那为什么我还是无法显示prompt的内容
你把小甲鱼代码打出来吧,一个图片我看着实在累 支持就是胜利 {:13_447:}
页:
[1]