Why am I getting AttributeError: Object has no attribute

25.2K    Asked by GeorgeBudd in Python , Asked on Apr 17, 2021

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

Answered by George Budd

If you are getting an object that has no attribute error then the reason behind it is because your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.



Your Answer

Interviews

Parent Categories