New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
threading.Thread documentation can be improved #55282
Comments
The documentation for the threading.Thread constructor says: "target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called." This could be improved by explicitly stating that target is called in a static context. As written, it takes a bit of thought (and experimentation) to be sure of that. |
I have no idea what "a static context" means, so it wouldn't make it any clearer to me. Can you explain further what your confusion is? |
What I meant was whether target should be declared as @staticmethod or not. |
I still don't understand. I haven't used threading much, but I don't believe I've ever used a static method with it. |
Roy, it's not clear what you're after. What is it that you think is special about the way the target is called? |
Here's the code I ended up writing: class Foo():
def __init__(self):
self.thread = Thread(target=Foo.runner, args=[self])
self.thread.start()
@staticmethod
def runner(self):
# blah, blah, blah It was not immediately clear from the documentation if my runner() method should be declared static or not. In retrospect, it must be (since this can be used with target functions which are not class methods at all), but it took a bit of thought to get from what the documentation said (i.e. 'callable object to be invoked by the run() method') to that conclusion. It seems to me the documentation could be a bit more explicit that your target does not get called as a method of some object. Changing the text to read, "static function or other callable object" would remove any such confusion. It could be that some of my confusion is due to my previously working with a C++ threading package where the thread runner functions *were* class methods. Still, it seems like the potential for other people to be similarly confused exists and a little tweaking of the documentation text would help. |
The doc doesn't mention static methods at all, and my uses and others that I've seen have never used static methods. |
You don't have to use any staticmethod here (actually, staticmethod is an anti-pattern in Python). Just write: class Foo():
def __init__(self):
self.thread = Thread(target=self.runner)
self.thread.start()
def runner(self):
# blah, blah, blah |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: