【问题标题】:Python threading number of arguments errorPython线程参数数量错误
【发布时间】:2021-06-27 14:29:18
【问题描述】:

很简单的代码:

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”只有一个字母。

【问题讨论】:

  • 嗯,很奇怪,我以为"hello" 里面有5 个字母。没关系。您的意思是:args = ("hello",)
  • 这行得通,谢谢!确实“hello”这个词有5个字母,我用“test”写了代码,它有4个字母,但是用“hello”代替了它,以免混淆你,因为“test”也是函数的名称。

标签: python multithreading python-multithreading


【解决方案1】:
import threading

def test(word):
    print(word)

threading.Thread(target=test, args=("hello",)).start()

【讨论】:

    猜你喜欢
    • 2011-11-15
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    相关资源
    最近更新 更多