.run() method executes any target function belonging to a given thread object that is now active. It normally executes in the background after the
.start() method is invoked.
thread_object.start() # .run() executing in the background
.run() method commonly takes the form of the
target function passed to the
thread_object, if any.
.run() method can be overridden with different instructions and statements in a separate subclass, like in the example below:
import threadingclass CustomThread(threading.Thread):def run(self):print("This is my custom run!")custom_thread = CustomThread()custom_thread.start()
.start() method is called against the
custom_thread, the overriden
.run() method is executed and the following is printed:
This is my custom run!
Choosing whether or not to override the
.run() method is a matter of preference. It exists to assist Java developers with learning to use threads in Python. Passing a
target function into the
.Thread() constructor and then invoking the
.start() will achieve the same thing: