2

I am reading the multi-threading in python. When I come into code like below:

x = threading.Thread(target=thread_function, args=(1,))

I am a little bit confused why args is (1,) which takes a comma at the end. I have tested that if I remove the comma it won't work. But I kind of get confused what is the difference here.

horcrux
  • 6,493
  • 6
  • 27
  • 40
Lance Shi
  • 987
  • 3
  • 12
  • 27

1 Answers1

2

The reason why your code doesn't work without the comma is that the args parameter must contain a tuple, and the common way of creating a single-element tuple is exactly like (elem,).

If you remove the comma, the python interpreter will just ignore the parentheses and pass 1 as argument.

horcrux
  • 6,493
  • 6
  • 27
  • 40