定义类的问题
这是小甲鱼的代码:mport time as t #首先我们需要time的模块,就先导入
class MyTimer():
#开始计时
def start(self):
self.star = t.localtime()
print('计时开始...')
运行后出现:
self.star = t.localtime()
AttributeError: 'MyTimer' object has no attribute 'localtime'
这里为什么说 MyTimer 类没有这个 localtime 属性?
谢谢! 本帖最后由 jackz007 于 2022-11-17 23:12 编辑
import time as t
class MyTimer():
#开始计时
def start(self):
self . star = t . localtime()
print('计时开始...')
x = MyTimer()
x . start() jackz007 发表于 2022-11-17 23:06
t = Mytimer()
t . start()
试了 就显示 : AttributeError: 'MyTimer' object has no attribute 'localtime' 本帖最后由 jackz007 于 2022-11-17 23:14 编辑
潺陵大地 发表于 2022-11-17 23:08
t = Mytimer()
t . start()
试了 就显示 : AttributeError: 'MyTimer' object has no attribute 'loca ...
代码更新了,现在再试
>>> import time as t
>>> class MyTimer():
#开始计时
def start(self):
self . star = t . localtime()
print('计时开始...')
>>> x = MyTimer()
>>> x . start()
计时开始...
>>> 你是不是在实例化对象的时候将 变量名重新赋值给了变量t,这个变量名覆盖了“import time as t”,所以才会出现上面的问题 潺陵大地 发表于 2022-11-17 23:08
t = Mytimer()
t . start()
试了 就显示 : AttributeError: 'MyTimer' object has no attribute 'loca ...
实例化对象的时候将 变量名重新赋值给了变量t,这个变量名覆盖了“import time as t”,所以才会出现上面的问题。将“t = Mytimer() ” 改成“x = Mytimer()”就行了 lxping 发表于 2022-11-17 23:13
你是不是在实例化对象的时候将 变量名重新赋值给了变量t,这个变量名覆盖了“import time as t”,所以才会 ...
搞明白 了,以为外面的不影响 类里面,实例的话 不能 一样。谢谢 jackz007 发表于 2022-11-17 23:10
代码更新了,现在再试
懂了 谢谢你
页:
[1]