Skip to content

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.

See also  Find All Numbers Disappeared in an Array using Python
Tags:
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Hilton Fernandes
Hilton Fernandes
10 months ago

Good and simple explanation. Congrats !

1
0
Would love your thoughts, please comment.x
()
x