提高对Python参数变量的理解

提高对Python参数变量的理解,python,python-3.x,Python,Python 3.x,对于以下名为ex13.py的Python脚本: from sys import argv script, first, second, third = argv print("The script is called:", script) print("Your first variable is:", first) print("Your second variable is:", second) print("Your third variable is:", third) 我对以下代

对于以下名为ex13.py的Python脚本:

from sys import argv

script, first, second, third = argv

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)
我对以下代码行有一个问题:

 script, first, second, third = argv
我们的意思是按此顺序将
argv
分配给左边的四个变量

然后,在我的终端中使用以下脚本:

python ex13.py first 2nd 3rd
我知道我们正在使用终端作为输入方法将变量传递给脚本。然而让我困惑的是这一点

当我编写基本Python脚本时,我调用它们的方式是:

python ex3.py

我说的这个
pythonex3.py
没有传递一个命令行参数,而
pythonex13.pyfirst-second-3rd
传递了几个命令行参数,对吗

从命令行工作时,argv返回一个命令行参数列表,其中第一个参数(在零位
argv[0]
我们得到了在
pyhton
名称之后使用的python文件名)

从第1位开始,这些值与接收参数的顺序相关。请注意,如果使用可选参数(
python myscript.py-p 127.0.0.1
),这些参数也会计入argv。因此,您将得到
argv[1]=-p

我说这个
pythonex3.py
没有传递一个 命令行参数和
python ex13.py第一个第二个第三个
正在传递 几个


不,您不正确,
python ex3.py
正在传递1个参数,
argv[0]=ex3.py

您是正确的。请记住,
argv[0]
将包含脚本的名称,而不管您传递了多少个参数it@Nullman他是不正确的——正如您所提到的,
pythonex3.py
正在传递参数“ex3.py”。