|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import time as t
- class MyTimer():
- def __init__(self):
- self.prompt='未开始计时'
- self.lasted=0.0
-
- self.begin=0.0
- self.end=0.0
- self.default_time=t.perf_counter
- def __str__(self):
- return self.prompt
- __repr__=__str__
- def start(self):
- self.begin=self.default_time()
- self.prompt='请先调用stop停止计时'
- print("计时开始")
- def stop(self):
- if not self.begin:
- print("请先调用start,开始计时")
- else:
- self.end=self.default_time()[color=Red]#这个地方的self.default_time(),()代表的什么意思呢,自己写的时候没有括号,编译出错,不太理解,可否指教[/color]
-
- self._calc()
- print("计时结束")
- def _calc(self):
- self.lasted=self.end-self.begin
- self.prompt='总共运行了%0.2f'%self.lasted
- self.begin=0
- self.end=0
- def __add__(self,other):
- result=self.lasted+other.lasted
- prompt='总共耗时:%0.2f'%result
- return prompt
- def set_timer(self,timer):
- if timer=='perf_counter':
- self.default_time=t.perf_counter
- elif timer=='process_time':
- self.default_time=t.process_time
- else:
- print('输入有误,请输入perf_counter或process_time')
复制代码
因为t.perf_counter()是内置函数,你本应这样写self.default_time=t.perf_counter(),self.default_time就不用加(),而你只是把内置函数的名称赋值给self.default_time,所以调用self.default_time要加括号.
|
|