[Python] Extending Python Thread class [duplicate]

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I was reading this article about extending Python Thread class, and as I understood I can define a subclass and override the run(self, [args]) method:

    class Subclass(Thread):
    def __init__(self):
    ...

    def run(self) -> None:
    ...


    Then I can execute the run() method by launching a subclass from the main thread:

    new_thread = Subclass()
    new_thread.start()


    But I don't understand why would I need to do it.

    I just could create a normal class and normal method with the same logic as in the run() method of a previous example:

    class NormalClass:
    def __init__(self):
    ...

    def normalMethod(self):
    # same logic as in run() method


    And then create an object of this class and launch method in the new thread:

    normal_class = NormalClass()
    thread = threading.Thread(normal_class.normal_method)
    thread.start()


    I don't understand the benefits of the overriding Python Thread class.

    Continue reading...

Compartilhe esta Página