[Tutor] Where is sys.py?
Steven D'Aprano
steve at pearwood.info
Mon Aug 22 20:55:02 CEST 2011
Lisi wrote:
> By "envelope" I meant container, because I was afraid of misusing the correct
> technical terms. I am still very confused by the terminology. Having been
> told that modules, functions and methods are just different names for the
> same thing, that commands are really also the same thing and having had them
> called yet a fifth thing in order "not to confuse you" (ouch!!) I am in a
> right muddle.
Ayyeee!!!! You have my sympathy!
Okay, let's start with the basics. Here's a small piece of simple Python
code, to count down from ten:
for i in range(10, 0, -1):
print(i)
print("Lift off!")
If you type that in the interactive interpreter, and hit Enter, Python
will execute that code and print 10 9 8 ... 2 1 Lift off!
Suppose you want to do a count-down again. In the interpreter, you would
have to type out the whole thing again. This is annoying and frustrating
-- imagine if you wanted to run it a thousand times. So instead, we can
make a re-usable, named, piece of code: a function.
def countdown():
for i in range(10, 0, -1):
print(i)
print("Lift off!")
If you type that in, once, it defines a reusable piece of code called
"countdown". Now you can run that code as many times as you like, by typing:
countdown()
and then hitting Enter. Much simpler, with much less chance of error.
Note that the parentheses are required. Without the round brackets, the
code inside the function won't be run.
(Methods are very similar to functions. At the most basic level, we can
pretend that a method is just a function that comes stuck to something
else. Don't worry about methods for now.)
But the problem here is that the countdown function is not permanent. If
you exit the interpreter, and start again, it will be lost for good. The
solution is to save the function in a .py file -- a module.
So in your text editor, type
def countdown():
for i in range(10, 0, -1):
print(i)
print("Lift off!")
in the file, save the file as "example.py". This becomes a module. A
module can contain any Python code you like: one line or a thousand, one
function or a hundred functions, or anything else.
Then, later, you can restart the Python interpreter, give the command:
from example import countdown
and use countdown as if you had just typed it in by hand.
Sometimes people will use "function" and "command" interchangeably. I
don't like this. Commands (also known as statements) are always built-in
to Python and operate as *instructions* to the interpreter. There are
only a few:
import ...
from ... import ...
def
class
del
print (Python 2 only, in Python 3 it becomes a function)
are the main ones. (There may be a few more I have missed.) Such
commands generally don't use parentheses, except for grouping items.
Functions always require parentheses. They may be built-in, such as len,
or you can write your own, using the def command.
Functions also always return a result, so you can do this:
x = some_function(a, b, c)
Commands don't. You can't say:
x = del a, b, c
because del doesn't return anything. Since it doesn't return anything,
you can't save the result in x.
(Aside: you can create functions that mimic commands by returning the
special value None. That's used for functions which are intended to not
return a value, but since Python requires functions to return something,
they return the special value None to mean "ignore this, it's nothing".
But they're still functions, and they still return something: the
special value None.)
So, summary:
* Commands are built-in Python instructions;
* Sometimes also known as "statements".
* They take actions, but don't return a result.
* "Command" is not an official Python term, so some people may
disagree with me!
* Functions may be built-in, or defined by the user.
* Functions always return something.
* Functions can mimic commands by returning None. I call these
"procedures", copying the language Pascal.
* Methods are related to functions.
* A module is a container that holds functions and any other values
you like.
* Modules are NOT functions, although they can hold functions.
* Modules usually are loaded from .py files on your disk.
* When you import the module, Python runs the code inside the file,
builds a module, and makes the module appear in the interpreter.
* Some special modules are built-in to the Python compiler, and
don't exist in a .py file.
* Other modules come from DLLs or .so files, and are written in C.
> So now, can anyone suggest an example of a module to look at? (Preferably
> with some subroutines inside).It is clearly NOT just another name for
> function or method. I imagine that there are some on the Internet?
The Python standard library consists of 200 or so Python files, all of
which can be opened in a text editor and read or printed for study.
(Be careful not to make modifications to the files, since you risk
breaking them and preventing Python from working correctly.)
Are you using Windows or Linux or Mac?
On my Linux system, I can look inside:
/usr/lib/python2.5/
and see the Python modules.
--
Steven
More information about the Tutor
mailing list