Hey guys and gals, I'm just starting Python, and I was wondering if someone could translate this code into english ?

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

I got this code from the "Learn code the Hard Way" series, but I do not understand exactly what is going on...
Thanks in advance :D

Recommended Answers

All 7 Replies

argv is a list containing the arguments passed to the python interpreter when called from a command line.
For example, let's look at this script:

# test.py
from sys import argv

print(argv[0])
print(argv[1])
print(argv[2])
print(argv[3])

command line call:

test.py david 8 jenny

output:

>>> test.py
david
8
jenny

I would recommend the free E-book 'Think Python', if you find your current book too ambiguous.
good luck.

from sys import argv Says: From the module named sys (which, if it were in a file would be in a file named 'sys.py') import the thing named 'argv' into the current name context. That makes it possible to refer to 'argv' without having to mention the module it came from. If you had instead said import sys then you would have all the things in sys available, but you would have to access them scoped by their module name. For instance script, first, second, third = sys.argv script, first, second, third = argv Says: unpack into the four named variables, the values contained in the variable named 'argv'. Python allows you do do simultaneous assignments so v_1,v_2 = "one",2 for instance works as if you said both v_1="one" and v_2=2 This is one of the cool syntactical features of Pyrhon. The quoted line assumes that argv holds exactly 4 values which can be unpacked into the four named variables. In real code, that assumption would be a very bad idea. print whatever You are using a 2.x version of Python which has an operator named print. Versions 3.x and above have a function named print. The difference is that operator print prints everything that follows it on the line: print "Hello", "world" whereas function print requires the things to be printed be passed as parameters in a function call: print("Hello","world") There is no urgent reason to choose Python 3.x over Python 2.x. Whichever one you have easy access to is fine.

Ok, so if I were to comment the code, then would this be good comprehensive commenting?

# Imports specific feature of "sys" named argv
from sys import argv

# Assigns a list of variables to argv to be taken from the consol.
script, first, second, third = argv

# Prints "text", then the string that was entered into the consol.
print "The script is called:", script
print "Your first variable is:", first
print "your second variable is:", second
print "Your third variable is:", third

First, let me recommend to you that you use the (CODE) button. Either click it and then paste your code between the tags, or highlight your code and click it. This is prettier

# Get command line arguments from the sys module 
from sys import argv
# ...

than yours, don't you you agree?

Second, for future reference. Note the "mark thread as solved" link near the bottom of the page. Only you, the original poster, can see that link. Using it after the thread is solved helps keep DaniWeb functioning smoothly.

Finally: Your comments are not quite right. For one thing, no real programmer would over-comment so much, but ignore that: You are indicating your understanding of the code, not "real comment"ing. The issue I still have is that you are skipping over how things are put into sys.argv When I invoke a python script from the command line: python myscript.py some subsequent words the python interpreter puts the script name into sys.argv[0] and any subsequent arguments into the rest of the sys.argv list. So from the command line above, you would end up with sys.argv == ['myscript.py', 'some', 'subsequent', 'words'] If you don't put exactly three arguments on the console command line when invoking your script, you will get a failure. Your comment almost seems to imply that the program interacts with the user to get the values. Not true.

I'm sorry, I do not understand this "The issue I still have is that you are skipping over how things are put into sys.argv When I invoke a python script from the command line: python myscript.py some subsequent words the python interpreter puts the script name into sys.argv[0] and any subsequent arguments into the rest of the sys.argv list. So from the command line above, you would end up with sys.argv == If you don't put exactly three arguments on the console command line when invoking your script, you will get a failure. Your comment almost seems to imply that the program interacts with the user to get the values. Not true."

Can you explain it in a high level way that anybody could understand (analogy) ? I'm sorry I'm so thick.

Hmm. I thought I was being clear.

If you chant myscript.py with no further arguments, then sys.argv has just one list element: "myscript.py". If you chant myscript.py argumentOne then sys.argv has two list elements: "myscript.py" and "argumentOne" ... and so forth for any number of command line arguments. Your script requires that you invoke it with exactly three arguments. If you were to invoke it with, say, two arguments or four arguments, it would fail. I'm not really sure what part of what I said was a problem. Does this help?

Yes, thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.