Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 03

Class Threads

Run a method of a Python class on a child thread by passing that method as the Thread target and supplying the method arguments as a tuple.

Advanced25 min
Class threads hero Module 19 Lesson 3 a method as the target

Run a method of a Python class on a child thread by passing that method as the Thread target and supplying the method arguments as a tuple.

The class approach

Example

python
import threading

class Board:
    def list_values(self, m):
        print(threading.current_thread().name, list(range(1, m + 1)))

board = Board()
worker = threading.Thread(target=board.list_values, args=(4,), name="count")
worker.start()
worker.join()

count prints [1, 2, 3, 4]. self is still the Board instance. args holds only m, because self is already bound on board.list_values. The steps match the function approach, with a method in place of a plain function.

Key takeaways

  • The second approach puts the child's logic in a method

  • target can be instance.method

  • self stays the instance; args holds the remaining parameters

  • Create the object before you create the Thread

  • start() still dispatches the child

  • Next: Join and Status — is_alive() and join()

  • Previous: Function Threads

Loading playground…

Check your understanding

Class Threads Quiz

In the class approach, where does the child's logic live?

1 / 10