Zed Shaw's "Learn Python the Hard Way" frequently asks you to "write out in English" what each and every line of a script does. I am struggling to do that with some stuff associated with the function (command?) argv because I don't know what to name certain parts of the code. Heck, I don't even know what to call argv--a function? A command? Variable? I know it's a module. But back on track:
Zed Shaw的“以艰难的方式学习Python”经常要求你“用英语写出”脚本的每一行所做的事情。我正在努力做一些与函数(命令?)argv相关的东西,因为我不知道如何命名代码的某些部分。哎呀,我甚至都不知道怎么称呼argv - 一个功能?一个命令?变量?我知道这是一个模块。但回到正轨:
Here is the code from exercise 13:
以下是练习13中的代码:
from sys import argvscript, first, second, third = argvprint "The script is called:", scriptprint "Your first variable is:", firstprint "Your second variable is:", secondprint "Your third variable is:", third
Zed states "The 'argv' is the "argument variable." My question is, what is the name of the things to the left of the equals sign on line three?
Zed说:“'argv'是”参数变量。“我的问题是,第三行等号左边的东西是什么名字?
Colloquially speaking, my urge is to call the words "script," "first" etc. variables themselves--but that doesn't seem right, since according to Zed argv is "the argument variable."
通俗地说,我的冲动是将“脚本”,“第一”等字称为变量本身 - 但这似乎不正确,因为根据Zed argv是“论证变量”。
I did not think calling them "arguments" is correct either; I've only read "argument" in terms of command line arguments.
我不认为称他们为“论据”也是正确的;我只在命令行参数方面读过“参数”。
"Parameters" seemed likely, since it was mentioned in the title of the exercise, but doing web searches with combinations of "equals sign," "=," "python," "argv," "definition" and so on wasn't very enlightening. Searching for things is very difficult when you don't know what they're called.
“参数”似乎很可能,因为它在练习的标题中被提及,但使用“等号”,“=”,“python”,“argv”,“定义”等组合进行网络搜索不是非常有启发性。当你不知道他们叫什么时,搜索东西是非常困难的。
I am very clear on what's happening in this script, I'm just not clear on the name of a part of it. I am very sure I'm going to slap my forehead when this is answered.
我非常清楚这个脚本中发生了什么,我只是不清楚其中一部分的名称。我很确定当这个问题得到解答时我会打我的额头。
3 个解决方案
#1
The things to the left of the "="
are variables that get their value from the variable on the right.
“=”左边的内容是从右边的变量中获取值的变量。
Given:
script, first, second, third = argv
argv
is a list of strings which in this case contains 4 items. These strings are "unpacked" and assigned to the four variables on the left of the =
.
argv是一个字符串列表,在这种情况下包含4个项目。这些字符串被“解压缩”并分配给=左侧的四个变量。
argv
gets its value is when a Python program is invoked from the command line, like this:
argv获取它的值是从命令行调用Python程序时,如下所示:
test.py this is sure cool
in this case argv
will contain ['test.py', 'this', 'is', 'sure', 'cool']
. These strings after the command are called "command line arguments" (see this tutorial) and the name of the script, and any arguments are stored in argv
. This is a way to send information to the script when you start it.
在这种情况下,argv将包含['test.py','this','is','sure','cool']。命令后面的这些字符串称为“命令行参数”(参见本教程)和脚本的名称,任何参数都存储在argv中。这是一种在启动时向脚本发送信息的方法。
In this case the variables get the following values:
在这种情况下,变量获得以下值:
script is set to "this.py" # the string is in argv[0] first to "is" # argv[1] second to "sure" # argv[2]
and
third to "cool" # argv[3]
So:
script, first, second, third = argv
is really equivalent to:
真的相当于:
script = argv[0] first = argv[1] second = argv[2] third = argv[3]
It's only that Python lets you do this assignment in one nice swoop.
只有Python允许你在一个很好的猛扑中完成这个任务。
Note that you can pull out your command line arguments in any order using the appropriate index value.
请注意,您可以使用适当的索引值以任何顺序提取命令行参数。
This mechanism is used to communicate information the to the Python script. You can imagine running a program that expects an input file and and output file. Instead of hardcoding them in your script, you could provide them on the command line. E.g.,
此机制用于将信息传递给Python脚本。您可以想象运行一个需要输入文件和输出文件的程序。您可以在命令行上提供它们,而不是在脚本中对它们进行硬编码。例如。,
computeData.py input.txt result.txt
#2
Sometimes it's easier to just type some code into the interactive python prompt, and see how these things work.
有时,只需在交互式python提示符中输入一些代码就更容易了,看看这些东西是如何工作的。
While sys.argv is a list that is defined for you by Python itself, it's not that different from any list or tuple (the mutable and non-mutable array-like types of Python) you define yourself. So try defining one yourself and play with it. After you've declared a variable named argv = ['123','456','789']
that is a list type, try assigning it to another name:
虽然sys.argv是一个由Python本身为你定义的列表,但它与你自己定义的任何列表或元组(可变和不可变的数组类型的Python)没有什么不同。所以尝试自己定义一个并玩它。在声明了名为argv = ['123','456','789']的变量之后,请尝试将其分配给另一个名称:
anothername = argv
Note that nothing special happens. now notice what happens if you instead try to assign to three different variables:
请注意,没有什么特别的事现在注意如果您尝试分配三个不同的变量会发生什么:
v1,v2,v3 = argv
The first (technically, "zeroeth") element in argv is stored in v1, the second element of argv is stored in v2, and so on.
argv中的第一个(技术上,“零”)元素存储在v1中,argv的第二个元素存储在v2中,依此类推。
I believe I would call v1,v2,v3 a "list of variables that are going to hold stuff that used to be elements in the list argv, but which I wish to unpack and store in their own place".
我相信我会将v1,v2,v3称为“将包含以前在列表argv中的元素的变量列表,但我希望将其解压缩并存储在自己的位置”。
#3
To answer your first question, argv
is an attribute of the sys
module. As for your second question, Python's docs do not specify a name for the right-hand side of assignment expressions, but script
, first
, etc. can be called variables in this context.
要回答第一个问题,argv是sys模块的一个属性。至于你的第二个问题,Python的文档没有为赋值表达式的右边指定一个名称,但是脚本,第一个等在这个上下文中可以被称为变量。
#1
The things to the left of the "="
are variables that get their value from the variable on the right.
“=”左边的内容是从右边的变量中获取值的变量。
Given:
script, first, second, third = argv
argv
is a list of strings which in this case contains 4 items. These strings are "unpacked" and assigned to the four variables on the left of the =
.
argv是一个字符串列表,在这种情况下包含4个项目。这些字符串被“解压缩”并分配给=左侧的四个变量。
argv
gets its value is when a Python program is invoked from the command line, like this:
argv获取它的值是从命令行调用Python程序时,如下所示:
test.py this is sure cool
in this case argv
will contain ['test.py', 'this', 'is', 'sure', 'cool']
. These strings after the command are called "command line arguments" (see this tutorial) and the name of the script, and any arguments are stored in argv
. This is a way to send information to the script when you start it.
在这种情况下,argv将包含['test.py','this','is','sure','cool']。命令后面的这些字符串称为“命令行参数”(参见本教程)和脚本的名称,任何参数都存储在argv中。这是一种在启动时向脚本发送信息的方法。
In this case the variables get the following values:
在这种情况下,变量获得以下值:
script is set to "this.py" # the string is in argv[0] first to "is" # argv[1] second to "sure" # argv[2]
and
third to "cool" # argv[3]
So:
script, first, second, third = argv
is really equivalent to:
真的相当于:
script = argv[0] first = argv[1] second = argv[2] third = argv[3]
It's only that Python lets you do this assignment in one nice swoop.
只有Python允许你在一个很好的猛扑中完成这个任务。
Note that you can pull out your command line arguments in any order using the appropriate index value.
请注意,您可以使用适当的索引值以任何顺序提取命令行参数。
This mechanism is used to communicate information the to the Python script. You can imagine running a program that expects an input file and and output file. Instead of hardcoding them in your script, you could provide them on the command line. E.g.,
此机制用于将信息传递给Python脚本。您可以想象运行一个需要输入文件和输出文件的程序。您可以在命令行上提供它们,而不是在脚本中对它们进行硬编码。例如。,
computeData.py input.txt result.txt
#2
Sometimes it's easier to just type some code into the interactive python prompt, and see how these things work.
有时,只需在交互式python提示符中输入一些代码就更容易了,看看这些东西是如何工作的。
While sys.argv is a list that is defined for you by Python itself, it's not that different from any list or tuple (the mutable and non-mutable array-like types of Python) you define yourself. So try defining one yourself and play with it. After you've declared a variable named argv = ['123','456','789']
that is a list type, try assigning it to another name:
虽然sys.argv是一个由Python本身为你定义的列表,但它与你自己定义的任何列表或元组(可变和不可变的数组类型的Python)没有什么不同。所以尝试自己定义一个并玩它。在声明了名为argv = ['123','456','789']的变量之后,请尝试将其分配给另一个名称:
anothername = argv
Note that nothing special happens. now notice what happens if you instead try to assign to three different variables:
请注意,没有什么特别的事现在注意如果您尝试分配三个不同的变量会发生什么:
v1,v2,v3 = argv
The first (technically, "zeroeth") element in argv is stored in v1, the second element of argv is stored in v2, and so on.
argv中的第一个(技术上,“零”)元素存储在v1中,argv的第二个元素存储在v2中,依此类推。
I believe I would call v1,v2,v3 a "list of variables that are going to hold stuff that used to be elements in the list argv, but which I wish to unpack and store in their own place".
我相信我会将v1,v2,v3称为“将包含以前在列表argv中的元素的变量列表,但我希望将其解压缩并存储在自己的位置”。
#3
To answer your first question, argv
is an attribute of the sys
module. As for your second question, Python's docs do not specify a name for the right-hand side of assignment expressions, but script
, first
, etc. can be called variables in this context.
要回答第一个问题,argv是sys模块的一个属性。至于你的第二个问题,Python的文档没有为赋值表达式的右边指定一个名称,但是脚本,第一个等在这个上下文中可以被称为变量。