gpt4 book ai didi

python - 为什么我收到 AttributeError : Object has no attribute?

转载 作者:行者123 更新时间:2023-12-04 18:51:12 24 4
gpt4 key购买 nike

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic在这里,这个问题的解决方式不太可能帮助 future 的读者。


2年前关闭。


此帖9个月前编辑提交审核,未能重新打开帖:

原始关闭原因未解决






Improve this question




我有一堂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 运行脚本验证。

关于python - 为什么我收到 AttributeError : Object has no attribute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44183552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com