什么是命令行参数以及它们的用途是什么?

What are command line arguements and what are they use for?

我在阅读编码书籍时遇到了这个 "command line arguments" 事情。谁能解释一下它是什么,它有什么用,我什么时候需要它?

您始终可以从控制台启动 python 代码,如下所示:

python myCode.py

现在有时你想告诉你的程序一些进一步的信息,然后你会输入如下内容:

python myCode.py name=George

在您的代码中,您可以读取这些数据并使用它们。

这是一种无需更改代码即可为用户、管理员等更改程序行为的非常简单的方法。

Python supports the creation of programs that can be run on the command line, completely with command-line arguments.

示例:

import sys
for x in sys.argv:
     print("Argument: ", x)

运行:

python demo.py Hey Bye

输出:

Argument:  demo.py
Argument:  Hey
Argument:  Bye