【发布时间】:2017-10-30 23:07:36
【问题描述】:
在 python 2.7 中使用类时遇到一些错误
我的班级定义是:
class Timer(object):
def __init__(self):
self.msg = ""
def start(self,msg):
self.msg = msg
self.start = time.time()
def stop(self):
t = time.time() - self.start
return self.msg, " => ", t, 'seconds'
在执行以下代码时。
timer = Timer()
timer.start("Function 1")
Some code
timer.stop()
timer.start('Function 2')
some code
timer.stop()
我收到以下错误:
Function 1 => 0.01 seconds
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object is not callable
对于第一次调用,它按预期工作,但对于第二次调用,它给出了错误。我无法找出错误的原因。
【问题讨论】:
-
t = time.time() - self.start看起来不对。self.start是一个函数。 -
是的,谢谢
-
self.start = time.time()也是错误的 -
@sv_jan5 也是,这个问题非常有效,+1,我喜欢找到问题哈哈
-
@sv_jan5 确实!我实际上再次编辑了我的答案,所以它现在终于正确和完整了
标签: python python-2.7 class oop