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.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
Lance Shi
  • 1,127
  • 3
  • 13
  • 28

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.

logi-kal
  • 7,107
  • 6
  • 31
  • 43