Avatar ·

Stop Thread when main script thread finishes

📁 Advanced Python Topics, example

For example, I have this code:

def loop:       while True:             print(""text"")             sleep(10)thread = threading.Thread(target=loop)

How can I make the thread stop when the program finishes?

Avatar ·

In principle, a declared class parameter with a value can already be used as a static property

>>> class MyClass:...     param = ""value""...>>> MyClass.param'value'>>>

This same value will also be accessible for an instance of the class. The only risk with an instance is that the property value might change during execution. In this case, you should ensure that this parameter is not modified and/or that the property is accessed exclusively through the class name, not through an instance.

You can use a static method - declared using the annotation

Log in to leave an answer

Blogs