Building Your Own Modules 291
This module imports the turtle module and defines a function
called cspiral() for drawing colorful spirals of different shapes,
sizes, and locations. Let’s look at differences between this mod-
ule and the other programs we’ve written. First, at u, we have a
special comment called a docstring. A docstring is a way of add-
ing documentation to files that we intend to reuse or share with
others; in Python, modules should have docstrings to help future
users understand what the module does. The docstring will always
be the first statement in a module or function, and each docstring
starts and ends with triple double quotes (""", three double quotes
in a row with no spaces in between). After the docstring, we import
the turtle module—yes, we can import modules into our modules!
At v, we define a function called cspiral() that accepts up to
four arguments—sides, size, x, and y—for the number of sides in
the spiral, the size of the spiral, and the (x, y) location of the spiral
starting from the center of the turtle screen. A docstring for the
cspiral() function begins at w; this multiline docstring provides
more specific information about the function. The first line of the
docstring begins with triple double quotes and describes the func-
tion overall. Next we leave a blank line, followed by a list of the
arguments accepted by the function. With this documentation, a
future user can easily read which arguments are accepted by the
function and what each one means. The rest of the function is the
code to draw a colorful spiral, similar to code from Chapters 2, 4,
and 7.
Using the colorspiral Module
Once we’ve completed colorspiral.py and saved it, we can use it
as a module by importing it into another program. Create a new
file in IDLE and save it as MultiSpiral.py in the same folder as
colorspiral.py.
MultiSpiral.py
import colorspiral
colorspiral.cspiral(5,50)
colorspiral.cspiral(4,50,100,100)
This three-line program imports the colorspiral module we
created and uses the module’s cspiral() function to draw two
spirals on the screen, as shown in Figure C-1.