类对象属性问题
<div>import time as t</div><div>class Time():def _init_(self):
self.unit = ['年','月','日','时','分','秒']
self.prompt='未开始计时'
self.lasted=[]
self.begin=0
self.end=0
def _str_(self):
return self.prompt
_repr_ = _str_</div><div> def _add_(self,other):
prompt='总共运行了'
result=[]
for index in range(6):
result.append(self.lasted+other.lasted)
if result:
prompt+=(str(result)+self.unit)
return prompt
#开始计时
def start(self):
self.begin=t.localtime()
self.prompt='请先调用stop()停止计时'
print('计时开始')
#停止计时
def stop(self):
if not self.begin:
print('请先调用start()开始计时')
else:
self.end=t.localtime()
self._calc()
print('计时结束')
#内部方法,计算时间
def _calc(self):
self.lasted=[]
self.prompt="总共运行了"
for index in range(6):
self.lasted.append(self.end-self.begin)
if self.lasted:
self.prompt +=(str(self.lasted)+self.unit)
#为下一轮计时初始化
self.begin=0
self.end=0
</div>结果:
=======================
>>> q=Time()
>>> q.start()
计时开始
>>> q.stop()
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
q.stop()
File "E:\python\程序\44_计时器.py", line 39, in stop
self._calc()
File "E:\python\程序\44_计时器.py", line 49, in _calc
self.prompt +=(str(self.lasted)+self.unit)
AttributeError: 'Time' object has no attribute 'unit'
>>>
在最开始不是有初始化self.unit = ['年','月','日','时','分','秒']吗?
为什么他还会报错说“Time”对象没有“unit”属性 ?
def __init__()
双下划线 本帖最后由 xiaosi4081 于 2020-8-4 20:32 编辑
import time as t
class Time:
def __init__(self):
self.unit = ['年','月','日','时','分','秒']
self.prompt='未开始计时'
self.lasted=[]
self.begin=0
self.end=0
def __str__(self):
return self.prompt
__repr__ = __str__
def __add__(self,other):
prompt='总共运行了'
result=[]
for index in range(6):
result.append(self.lasted+other.lasted)
if result:
prompt+=(str(result)+self.unit)
return prompt
#开始计时
def start(self):
self.begin=t.localtime()
self.prompt='请先调用stop()停止计时'
print('计时开始')
#停止计时
def stop(self):
if not self.begin:
print('请先调用start()开始计时')
else:
self.end=t.localtime()
self._calc()
print('计时结束')
#内部方法,计算时间
def _calc(self):
self.lasted=[]
self.prompt="总共运行了"
for index in range(6):
self.lasted.append(self.end-self.begin)
if self.lasted:
self.prompt +=(str(self.lasted)+self.unit)
#为下一轮计时初始化
self.begin=0
self.end=0
1.魔方方法都是双下划线
2.Time后面不用括号 初始化函数__init__
重载运算符函数__add__
还有属性描述函数__repr__
以上这三个函数都是魔法函数,两侧都是双下划线。
注意:类的很多魔法函数都是双下划线
import time as t
class Time():
def __init__(self):
self.unit = ['年','月','日','时','分','秒']
self.prompt='未开始计时'
self.lasted=[]
self.begin=0
self.end=0
def _str_(self):
return self.prompt
__repr__ = _str_
def __add__(self,other):
prompt='总共运行了'
result=[]
for index in range(6):
result.append(self.lasted+other.lasted)
if result:
prompt+=(str(result)+self.unit)
return prompt
#开始计时
def start(self):
self.begin=t.localtime()
self.prompt='请先调用stop()停止计时'
print('计时开始')
#停止计时
def stop(self):
if not self.begin:
print('请先调用start()开始计时')
else:
self.end=t.localtime()
self._calc()
print('计时结束')
#内部方法,计算时间
def _calc(self):
self.lasted=[]
self.prompt="总共运行了"
for index in range(6):
self.lasted.append(self.end-self.begin)
if self.lasted:
self.prompt +=(str(self.lasted)+self.unit)
#为下一轮计时初始化
self.begin=0
self.end=0
另外,为什么你发的代码中总会带有<div>标记呢?
页:
[1]