python - 使用命令参数调用函数

标签 python

我有以下代码。

import os, fnmatch, sys
print ("HERE")
def test(arg):
    print ("arg" + arg)

从命令行运行脚本时,如何调用函数 test

最佳答案

这个 python 习语也被广泛使用:

if __name__ == "__main__":
    # do stuff
    ...

当脚本从控制台运行时,这个 if 子句中的所有内容都会运行。 这样你就可以将驱动程序代码放在模块文件等中。

如果你用 test("blah blah") 替换上面的 ... 你会得到你想要的结果:)

关于python - 使用命令参数调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15691384/

相关文章:

python - 为什么在输出中打印 'None'?

python - 停止 Tkinter 菜单自动运行命令

python - 如何对每一组pandas groupby应用不同的功能?

python - 子集和问题

python - 对时间戳数组使用互相关有意义吗?

Python/Theano : Is it possible to construct truly recursive theano functions?

python - 不使用 .values() 等查找字典中值的平均值

python - 通过python连接mysql数据库

python - 给我一些很棒的 Web2py 提示和技巧,让你更有效率

python - python中map()函数的多个迭代器?