python - Python线程参数数量错误
问题描述
很简单的代码:
import threading
def test(word):
print(word)
threading.Thread(target = test, args = "hello").start()
我收到以下错误:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\phil\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner
self.run()
File "C:\Users\phil\AppData\Local\Programs\Python\Python39\lib\threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
TypeError: test() takes 1 positional argument but 4 were given
给定的 4 个参数是单词“hello”中的字母数。这是我不明白的,因为“你好”这个词只是一个论点。如果我用“A”替换“hello”,它会起作用,因为“A”只是一个字母。
解决方案
import threading
def test(word):
print(word)
threading.Thread(target=test, args=("hello",)).start()