I’ve been trekking along in Learn Python The Hard Way book. It’s been going pretty well. I try to dedicate at least 15 minutes a day (but ideally an hour) to learning. I just try to get some practice in on the regular.
Anyways, I’m beginning to get confused. I’m on lesson 17 (there are 52 lessons in total) and I am having issues understanding what is going on.
To be honest, I guess my confusion began back in lesson 15 or 16. Perhaps even earlier. I really wish I had a video to consult right now. I have the paperback edition which came with a DVD .. But I do not have a cd-rom on my computer. Sigh.
I’m going to breakdown some unfamiliar terms/concepts in this lesson.
Here is the Lesson 17 Code
from sys import argv from os.path import exists script, from_file, to_file = argv print "Copying from %s to %s" % (from_file, to_file) # we could do these two on one line too, how? in_file = open(from_file) indata = in_file.read() print "The input file is %d bytes long" % len(indata) print "Does the output file exist? %r" % exists(to_file) print "Ready, hit RETURN to continue, CTRL-C to abort." raw_input() out_file = open(to_file, 'w') out_file.write(indata) print "Alright, all done." out_file.close() in_file.close()
(The code for the lesson can be found here, as well as the lesson 17 in its entirety.)
I want to try to sound cool and like I know what’s going on … But I have no freaking clue. Yeah sure I can type the code into my interpreter and run the file. But that doesn’t mean I “get” it.
I started off typing out everything I didn’t understand till I noticed it was everything. So I’m going to cover only the first line of code as well as specific some specific functions / syntax used as an attempt to gain some clarity.
“from sys import argv”
In the first line, this code appears – “from sys import argv”. It’s been appearing repeatedly and somehow I’m still thinking, “huh?”
I like the explanations here. There are a lot. And the people could be wrong. But I am going to take the benefit of the doubt. I like this description best about what “from sys import argv” means:
“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.”
This leads me to wonder two things: what exactly is a module (I know the book went over this before…) and what does import mean.
As stated by Python documentation, a module is “a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module.”
“A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.”
So basically all the .py scripts we’ve been creating in this book and running in the shell are modules? (I use the question mark because I am not sure if that statement is right … if you know the answer email me at firstname.lastname@example.org.)
As Zed Shaw says in this lesson, “Using import is a way to get tons of free code other better (well, usually) programmers have written so you do not have to write it.”
As taken from Stackoverflow:
“they allow you to use functions defined elsewhere (either in a standard module, or your own).”
%s is a string formatting syntax. It’s a placeholder.
In this instance:
name = raw_input(“who are you?”)
print “hello %s” % (name,)
The “who are you” would go after “hello” when printed. It’s used to simplify and make life easier. (Read more here.)
There is also %r placeholder which is used for debugging. The %r is a “raw representation.” (Shaw talks about this in his book.)
Opening a file. Read more here. It’s a built in func
Again, as documented by Python, this is a built-in Python function that returns “the length (the number of items) of an object.”
For this lesson, when you run the script in the shell it says it is 21 bytes long.
Yikes. I am going to stop there. My head is swirling.
At the very end of the lesson, where he answers common questions, this was said,
Is it normal to feel like this exercise was really hard?
Yes, it is totally normal. Programming may not “click” for you until maybe even Exercise 36, or it might not until you finish the book and then make something with Python. Everyone is different, so just keep going and keep reviewing exercises that you had trouble with until it clicks. Be patient.
This makes me feel a lot better.