左边的东西"="
是 变量 ,它们从右边的变量获取 值 。
鉴于:
script, first, second, third = argv
argv
是一个字符串 列表 ,在这种情况下,包含4个项目。这些字符串被“解压”并分配给左侧的四个变量=
。
argv
它的值是从命令行调用Python程序时的值,如下所示:
test.py this is sure cool
在这种情况下argv
将包含 ['test.py', 'this', 'is', 'sure', 'cool']
。命令后的这些字符串称为“ ”(请参见本教程)和脚本名称,所有参数都存储在中argv
。这是一种在启动脚本时向脚本发送信息的方法。
在这种情况下,变量将获得以下值:
script is set to "this.py" # the string is in argv[0]
first to "is" # argv[1]
second to "sure" # argv[2]
和
third to "cool" # argv[3]
所以:
script, first, second, third = argv
实际上等于:
script = argv[0]
first = argv[1]
second = argv[2]
third = argv[3]
只有Python可以让您一口气完成这项任务。
请注意,您可以使用适当的索引值以任何顺序提取命令行参数。
该机制用于将信息传达给Python脚本。您可以想象运行一个需要输入文件和输出文件的程序。您可以在命令行中提供它们,而不是在脚本中对其进行硬编码。例如,
computeData.py input.txt result.txt