In some of the examples given so far there is a statement at the beginning that looks like “import name.” The implication is that there are some functions that are needed by the program that are provided elsewhere, possibly by the Python system itself or perhaps by some other software developer. The idea of writing functions that can be re-used in a straightforward way is very important to the software development process. It means that no programmer is really alone; that code is available for doing things like generating random numbers or interfacing with the operating system or the Internet, and that it does not to be created each time. In addition, there is an assumption that a module works correctly. When a programmer builds a collection of code for their own use, it needs to be tested as thoroughly as possible, and from that time on it can be used in a package with confidence. If a program has errors in it, then look in the code for that program first and not in the modules. This makes debugging code faster.
What is a module? It is simply a function or collection of functions that reside in a file whose name ends in .py. Technically, all of the code developed so far qualifies as modules. Consider as an example the function from the previous section that finds the maximum value in a list. Save the functions max() and maxr() in a file named max.py. Now create a new Python program named usemax.py and place it in the same directory as max.py. If the two files are in the same directory then they can “see” each other in some sense.
Here is some code to place in the file usemax.py:
import max
d = [12,32,76,45,9,26,84,25,61, 66, 1,2]
print “”MAX is””, max.max(d),”” MAXR is””, max.maxr(d))
if max.maxr(d) != max.max(d):
print “”*** NOT EQUAL ***””)
This program is just a test of the two functions to make certain that they return the same value for the same list, the variable d. Note two things:
- The statement import max occurs at the beginning of the program, meaning that the code inside this file is available to this program. Python looks inside this file for the function and variable names.
- When the function max() or maxr() is called, the function name is preceded by the module name (max) and a period. This syntax informs the Python system that the name maxr() (for example) is found in the module max and not elsewhere.
The first time that the module is loaded into the Python program, the code in the module is executed. This allows any variable initializations to be performed. Henceforth, that code is not executed again, and functions within the module can be called knowing that the initializations have been performed.
The module could reside in the same directory as the program that uses it, but does not have to. The Python system recognizes a set of directories and paths and modules can be placed in some of those locations as well, making it easier for other programs on the same computer to take advantage of them. On the computer used to create the examples in this book, the directory C:\Python34\Lib can be used to store modules, and they will be recognized by import statements.
Finally, if the syntax max.maxr(list) seems a bit cumbersome, then it is possible to import specific names from the module into the program. Consider the following rewrite of usemax.py:
from max import max, maxr
d = [12,32,76,45,9,26,84,25,61, 66, 1,2]
print (“MAX is ”, max(d), ” MAXR is ”, maxr(d))
if maxr(d) != max(d):
print (”*** NOT EQUAL ****”)
The statement from max import max, maxr instructs Python to recognize the names max and maxr as belonging to the module named max (i.e., as residing in the file named max.py). In that case, the function can be called by simply referencing its name.
There appears to be a name conflict with the package named max and the function named max, but in fact, there is no problem. It is not uncommon to find this sort of naming relationship (example: random.random()). The module name max refers to a file name, max.py. The function name max refers to a function within that file.
Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.