3

Currently I'm not a python programmer, but I'm giving some maintenance to some python code, and I have what's more or less the following:

class DerivedThread(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)

  def initStuff():
    print "Hello 2"

  def run(self):
    print "Hello 1"
    self.initStuff()
    print "Hello 3"

initStuff does not call print in truth, just set some variables, I've added this method for organization, there was only __init__ and run before.

The problem is that, once execution reachs self.initStuff(), I see no messages following anymore, only "Hello 1", I thought it was some problem with calling derived methods with python, but I don't know what's happening.

What's happening?

2 Answers 2

4

The problem is in definition of your initStuff method. It requires at least one argument (usually called 'self') in it's definition (you've defined no arguments). Let's see what happens then:

class MyClass(object):

    def initStuff(self):
        pass

c = MyClass()
c.initStuff()

TypeError: initStuff() takes no arguments (1 given)

So obviously your code fails silently in thread you've defined.

The way to fix the problem is to define initStuff method following Python rules.

 ...
 def initStuff(self):  #  Here 'self' argument is defined
      pass

This kind of method is called instancemethod as it's called on behalf of your class instance (and the first argument is an instance object itself that must be explicitly defined).

Read more about classes in documentation.

Sign up to request clarification or add additional context in comments.

Comments

1

Oh I got it, I just needed to pass self as argument to initStuff.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.