Page 79 -
P. 79
sharing your code
Import a module to use it
Now that your module is built, packaged as a distribution, and installed, let’s
see what’s involved in using it. To use a module, simply import it into your
programs or import it into the IDLE shell:
Use the Python
keyword “import”… …and provide the name
import nester of your module.
Note: you don’t need to
include the “.py” extension
when naming your module.
The import statement tells Python to include the nester.py module in
your program. From that point on, you can use the module’s functions as if
they were entered directly into your program, right? Well…that’s what you
might expect. Let’s check out the validity of your assumption.
Write a small program that imports your newly created module, defines a small list called “cast,”
and then uses the function provided by your module to display the contents of the list on screen.
Use the following list data (all strings): Palin, Cleese, Idle, Jones, Gilliam, and Chapman.
Open your program in IDLE’s edit window, and then press F5 to execute your code. Describe
what happens in the space below:
you are here 4 43