为什么我得到 AttributeError: Object 没有属性?

我有一个类 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()

看起来很简单,不是吗? 但是当我运行它时,我得到了这个错误

现在我有了这个方法,就在这里。那么出了什么问题? 请帮助

编辑: 这是堆栈跟踪

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

612340 次浏览

Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.

These kind of bugs are common when Python multi-threading. What happens is that, on interpreter tear-down, the relevant module (myThread in this case) goes through a sort-of del myThread.

The call self.sample() is roughly equivalent to myThread.__dict__["sample"](self). But if we're during the interpreter's tear-down sequence, then its own dictionary of known types might've already had myThread deleted, and now it's basically a NoneType - and has no 'sample' attribute.

Python protects those members by internally changing the name to include the class name. You can access such attributes as object._className__attrName.

If you’re using python 3+ this may also occur if you’re using private variables that start with double underscore, e.g., self.__yourvariable. Just something to take note of for some of you who may run into this issue.

You can't access outside private fields of a class. private fields are starting with __ . for example -

class car:
def __init__(self):
self.__updatesoftware()


def drive(self):
print("driving")


def __updatesoftware(self):
print("updating software:")


obj = car()
obj.drive()
obj.__updatesoftware()  ## here it will throw an error because

__updatesoftware is an private method.

I got this error for multi-threading scenario (specifically when dealing with ZMQ). It turned out that socket was still being connected on one thread while another thread already started sending data. The events that occured due to another thread tried to access variables that weren't created yet. If your scenario involves multi-threading and if things work if you add bit of delay then you might have similar issue.

I have encountered the same error as well. I am sure my indentation did not have any problem. Only restarting the python sell solved the problem.

The same error occurred when I had another variable named mythread. That variable overwrote this and that's why I got error

This may also occur if your using slots in class and have not added this new attribute in slots yet.

class xyz(object):
"""
class description


"""


__slots__ = ['abc', 'ijk']


def __init__(self):
self.abc = 1
self.ijk = 2
self.pqr = 6 # This will throw error 'AttributeError: <name_of_class_object> object has no attribute 'pqr'