RuntimeError: thread.__init__() not called (Python)

Today I got a really dumb error from Python.

RuntimeError: thread.__init__() not called

But luckily it’s really easy to fix!

Below is the code before (with the error):

class SomeThread(Thread): def __init__(self, myVar): self.sMyVar = str(myVar) def run(self): self.sMyVar = "bla.." + self.sMyVar
Code language: Python (python)

And now for the code that resolved the problem:

class SomeThread(Thread): def __init__(self, myVar): Thread.__init__(self) self.sMyVar = str(myVar) def run(self): self.sMyVar = "bla.." + self.sMyVar
Code language: Python (python)

If you have really bad eye-sight and can’t spot the difference between the 2 code block above 😛
What we do is add the following code in the beginning of the construct – or __init__ in Python:

Thread.__init__(self)
Code language: Python (python)

Also be aware that I originally did:

from threading import Thread
Code language: Python (python)

Which means I don’t have to instantiate it going forward using `threading.Thread`, instead I can do `Thread` alone.

Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments