Python为导入的函数提供参数数量

标签 python function arguments

我正在尝试为导入的函数提供参数。
我有 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/

相关文章:

python - Django导入导出: Documentation cannot be understood

javascript - Django View 中的通用身份验证

数组中的 JavaScript 函数

reactjs - 在函数上使用 React FC

c++ - 如何为模板嵌套类类型的函数声明参数?

python - 导入带参数的 python 脚本

python - 为什么pystan安装这么慢?

python - 在遇到换行符或逗号时拆分/删除一组行

python - 将函数从一个 python 文件导入到另一个

function - PowerShell 中 $args 的混淆评估