Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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

1 Answer

0 votes
by (106k points)

You are getting this attribute error because your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...