为什么我收到 AttributeError: 对象没有属性? [关闭]

2023-12-27

我有一个类 MyThread。其中,我有一个方法示例。我正在尝试从同一对象上下文中运行它。请看一下代码:

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()

看起来很简单不是吗。但是当我运行它时我收到此错误

AttributeError: 'myThread' object has no attribute 'sample'现在我有了这个方法,就在那里。那么出了什么问题呢?请帮忙

编辑:这是堆栈跟踪

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 类代码。但我可以向你保证它工作得很好


你的缩进是错误的,并且你混合了制表符和空格。运行脚本python -tt核实。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我收到 AttributeError: 对象没有属性? [关闭] 的相关文章

随机推荐