On one hand we have interactive use of the Python interpreter: we
type something and the interpreter responds immediately. We can do simple
things, but when our statements get too long, this interaction can become
a niusance. We introduced this first, in the section called “Command-Line Interaction”.
On the other hand, we have scripted use of the interpreter: we
present a file as a finished program to execute. While handy for getting
useful results, this isn't the easiest way to get a program to work in the
first place. We described this in the section called “Script Mode”.
In between the interactive mode and scripted mode, we have a third
operating mode, that we might call hacking mode.
The idea is to write most of our script and then exercise portions of our
script interactively. In this mode, we'll develop script files, but we'll
exercise them in an interactive environment. This is handy for developing
and debugging function definitions.
The basic procedure is as follows.
-
In our favorite editor, write a script with our function
definitions. We often leave this editor window open.
IDLE, for example, leaves this window open
for us to look at.
-
Open a Python shell. IDLE, for
example, always does this for us.
-
In the Python Shell,
import
the script file.
In IDLE, this is effectively what happens
when we run the module with
F5
.
-
In the Python Shell, test the function interactively. If it
works, we're done.
-
If the functions in our module didn't work, we return to our
editor window, make any changes and save the file.
-
In the Python Shell, clear out the old definition by restarting
the shell. In IDLE, we can force this with
F6
. This happens automatically
when we run the module using
F5
.
-
Go back to step 3, to import and test our definitions.
The interactive test results are often saved and put into the
docstring for the file with our function definitions. We usually copy the
contents of the Python Shell window and paste it into our module's or
function's docstring. This record of the testing can be validated using
the doctest
module.
Example. Here's the sample function we're developing. If you look
carefully, you might see a serious problem. If you don't see the
problem, don't worry, we'll find it by doing some debugging.
In IDLE, we created the following
file.
Example 9.2. function1.py Initial Version
# Some Function Or Other
def odd( number ):
"""odd(number) -> boolean
Returns True if the given number is odd.
"""
return number % 2 == "1"
We have two windows open: function1.py and
Python Shell.
Here's our interactive testing session. In our
function1.py window, we hit F5 to run the module.
Note the line that shows that the Python interpreter was restarted;
forgetting any previous definitions. Then we exercised our function with
two examples.
Python 2.5.1 (r251:54863, Oct 5 2007, 21:08:09)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************
IDLE 1.1.4
>>> ================================ RESTART ================================
>>>
>>> odd(2)
False
>>> odd(3)
False
Clearly, it doesn't work, since 3 is odd. When we look at the
original function, we can see the problem.
The expression number % 2 == "1"
should be number
% 2 == 1
. We need to fix function1.py
. Once
the file is fixed, we need to remove the old stuff from Python, re-import
our function and rerun our test. IDLE does this
for us when we hit
F5
to rerun the module. It shows this
with the prominent restart message.
If you are not using IDLE, you will need
to restart Python to clear out the old definitions. Python optimizes
import operations; if it's seen the module once, it doesn't import it a
second time. To remove this memory of which modules have been imported,
you will need to restart Python.