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'
Now I have that method, right there. So what's wrong? Please help
Edit: This is the stack trace
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'
I am calling it like this
arThreads = [] maxThreads = 2; for i in range( maxThreads ): redisOpsObj = redisOps() arThreads.append( myThread(i, "Thread-"+str(i), 10, redisOpsObj) )
Sorry, I can't post the redisOps class code. But I can assure you that it works just fine
It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling.
The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.
In the example above, object b has the attribute disp , so the hasattr() function returns True. The list doesn't have an attribute size , so it returns False. If we want an attribute to return a default value, we can use the setattr() function. This function is used to create any missing attribute with the given value.
You are getting AttributeError: 'NoneType' object has no attribute 'something' because NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. It means that an assignment or function call up above failed or returned an unexpected result.
Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt
to verify.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With