我正在尝试为导入的函数提供参数。
我有 base.py
:
import sc1 #import sc1.py
from threading import Thread
Thread(target=sc1.main,args="John").start()
sc1.py
中的一个函数:
def main(name):
print "Hello ",name
说
TypeError: main() takes exactly 1 argument (4 given)
如果我只给一个 args="J"
那么它工作正常,
有人知道我能做什么吗?
最佳答案
你想传递一个参数元组:
Thread(target=sc1.main,args=("John",)).start()
在你的例子中,因为 str
是可迭代的,Thread
试图解压 "J""o", "h", "n"
作为参数而不是将整个事物作为一个原子单元传递。
关于Python为导入的函数提供参数数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39083767/