问题描述
我有一个类 MyThread.我有一个方法示例.我试图从同一个对象上下文中运行它.请看代码:
I have a class MyThread. In that I have a method sample. I am trying to run it from within the same object context. Please have a look at the code:
class myThread (threading.Thread):
def __init__(self, threadID, name, counter, redisOpsObj):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
self.redisOpsObj = redisOpsObj
def stop(self):
self.kill_received = True
def sample(self):
print "Hello"
def run(self):
time.sleep(0.1)
print "\n Starting " + self.name
self.sample()
看起来很简单是不是.但是当我运行它时,我收到此错误
Looks very simple ain't it. But when I run it I get this error
AttributeError: 'myThread' object has no attribute 'sample'
现在我有了那个方法,就在那里.那么怎么了?请帮忙
AttributeError: 'myThread' object has no attribute 'sample'
Now I have that method, right there. So what's wrong? Please help
这是堆栈跟踪
Starting Thread-0
Starting Thread-1
Exception in thread Thread-0:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'
我是这样称呼的
arThreads = []
maxThreads = 2;
for i in range( maxThreads ):
redisOpsObj = redisOps()
arThreads.append( myThread(i, "Thread-"+str(i), 10, redisOpsObj) )
抱歉,我不能发布 redisOps 类代码.但我可以向你保证它工作得很好
Sorry I can't post redisOps class code. But I can assure you that it works just fine
推荐答案
您的缩进是错误的,并且混用了制表符和空格.使用 python -tt
运行脚本进行验证.
Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt
to verify.
这篇关于为什么我收到 AttributeError: Object has no attribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!