求解为什么我的str函数不能打印出return的值
Traceback (most recent call last):File "C:\Users\945\Desktop\MyTimer.py", line 3, in <module>
class MyTimer():
File "C:\Users\945\Desktop\MyTimer.py", line 7, in MyTimer
__repr__==__str__
NameError: name '__repr__' is not defined
>>>
================== RESTART: C:\Users\945\Desktop\MyTimer.py ==================
>>> t1 = MyTimer
>>> t1
<class '__main__.MyTimer'>
>>> print(t1)
<class '__main__.MyTimer'>
>>> print(t1)
<class '__main__.MyTimer'>
>>>
以上为报错
import time as t
class MyTimer():
def __str__(self):
return self.info
def __init__():
self.info = '请先调用start()开始计时'
self.begin = 0
self.end = 0
self.unit = ['年','月','天','小时','分','秒']
def start(self):
self.begin = t.localtime()
self.info = '请调用stop()停止计时'
print("计时开始")
def stop(self):
if not self.begin:
print("请先调用start()开始计时")
else:
self.end = t.localtime()
print("计时结束")
self._calc()
def _calc(self):
self.leasted = []
for index in range(6):
self.leasted.append(self.end - self.begin)
self.info = "总共运行了" + str(self.leasted) + self.unit
print(self.info)
这个是我写的代码 是我没有在__init__的括号里加self
页:
[1]