Python Threading - Creation Of A Subclass?
Solution 1:
The docs say you need to define a new subclass of the Thread class.
and
I was able to write my own script without creating this subclass and it works so I am not sure why this is stated as a requirement.
The Python docs say no such thing, and can't guess which docs you're talking about. Here are Python docs:
There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a subclass. No other methods (except for the constructor) should be overridden in a subclass. In other words, only override the init() and run() methods of this class.
You're using the first method specified there (passing a callable to the Thread()
constructor). That's fine. Subclasses become more valuable when the callable needs access to state variables and you don't want to clutter your program with globals for that purpose, and especially when using multiple threads that each need their own state variables. Then state variables can usually be implemented as instance variables on your own subclass of threading.Thread
. If you don't need that (yet), don't worry about it (yet).
Post a Comment for "Python Threading - Creation Of A Subclass?"