3

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:

Here is the code from exercise 13:

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

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?

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."

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.

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.

Warren P
  • 65,725
  • 40
  • 181
  • 316
ecs
  • 33
  • 3
  • Relevant docs: http://docs.python.org/release/1.5.1p1/tut/tuples.html Python syntax feature "tuple unpacking" is applicable to both lists and tuples, and I've commonly heard the syntax called "tuple unpacking". In the case above, it's actually list-unpacking but in the sense of the question about python syntax, I think that tagging things with tuple-unpacking is helpful. You can think of there being a "quiet cast from list to tuple" as it's unpacked, and that sometimes helps with understanding the operation. – Warren P Aug 25 '12 at 18:13

3 Answers3

3

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 gets its value is when a Python program is invoked from the command line, like this:

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.

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.

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.,

 computeData.py input.txt result.txt
Markus Unterwaditzer
  • 7,992
  • 32
  • 60
Levon
  • 138,105
  • 33
  • 200
  • 191
  • > These strings are "unpacked" and assigned to the four variables on the left of the = So the technical term is value? Upon reading a few chapters back in Learning Python the Hard way, I saw Zed call those variables "values." Does that sound right, or would I be better off calling them variables, arguments or maybe list values? – ecs Aug 25 '12 at 03:27
  • 1
    These are the terms I would use: On the left of the `"="` you have 4 *variables*. On the right you have a *list* of strings. These strings are assigned to each of the 4 variables respectively. I.e., each of the variables gets its *value* (a string) from an element of `argv`. Does that help? – Levon Aug 25 '12 at 03:32
  • 1
    @eric.christopher.scott they're certainly not values - values in this context is what's on the *right* of the `=` (in this case, because of unpacking, you might say thats the contents of `argv` rather than `argv` itself). They're just variables - its just a bit fancy that you can assign to multiple variables at once, item-wise. Other than that, its the same operation as `script = argv[0]`, so the terminology should be the same (script is a variable, argv[0] is a value). – lvc Aug 25 '12 at 03:35
  • @lvc Thanks, that answers my question entirely and succinctly. Levon, you added depth to my understanding. – ecs Aug 25 '12 at 03:41
  • @Ivc Nicely said I have to agree with OP – Levon Aug 25 '12 at 03:49
  • In Python we use the term names instead of what other languages might term variables. This does create a distinction handy when comparing dynamic vs static language activities "under the hood" so to speak. – Paddy3118 Aug 25 '12 at 06:22
1

Sometimes it's easier to just type some code into the interactive python prompt, and see how these things work.

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:

  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.

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".

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • > "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" Ha ha! I like it, it's got a ring to it. Upon further review of Learning Python the Hard Way, I'm beginning to think the technical term (if there is one) is **value**. – ecs Aug 25 '12 at 03:25
  • +1 for sometimes its easier to just play with the interactive interpreter. But to nitpick - `argv` is a list (not an array), and `v1, v2, v3` is more like a tuple than a list (although its not really a tuple either, since `a = (a1, a2, a3); a = sys.argv` doesn't do unpacking). – lvc Aug 25 '12 at 03:28
  • Correct. The native type name is list. And I think that the syntax "a,b,c = x" is known as "tuple-unpacking" even if X is a list. I may not be being precise enough but to my mind "tuple and list are sub-types that allow itemwise access" and thus I refer to them both (imprecisely) as being like arrays. I hope I've edited to make the sense clearer. – Warren P Aug 25 '12 at 18:06
0

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.

ashastral
  • 2,818
  • 1
  • 21
  • 32