I am reading “Learn Python the Hard Way” and was confused by the “script” part of the second line.
from sys import argv
script, filename = argv
From what I understand, the second line says: script and filename comprise argv.
I tried running my code without the “script” part and it worked just fine. I’m not sure what the purpose of it is.
Generally, the first argument to a command-line executable is the script name, and the rest are the expected arguments.
Here,
argvis a list that is expected to contain two values: the script name and an argument. Using Python’s unpacking notation, you can writeas
while also throwing errors if there are an unexpected number of arguments (like one or three). This can be a good idea, depending on one’s code, because it also ensures that there are no unexpected arguments.
However, the following code will not result in
filenameactually containing the filename:This is because
filenameis now the argument list. To illustrate: