"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "docs/script_guide/flow_control.html" of archive TestMaker.zip:
<HTML>
<!--This file created 10/18/02 4:07 PM by Claris Home Page version 3.0 30 Day Trial-->
<HEAD>
<TITLE>TestMaker Script Guide - Flow Control</TITLE>
<META NAME=GENERATOR CONTENT="Claris Home Page 3.0 30 Day Trial">
<X-CLARIS-WINDOW TOP=46 BOTTOM=718 LEFT=30 RIGHT=816>
<X-CLARIS-TAGVIEW MODE=minimal>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<H1><FONT FACE="Arial"><TABLE BORDER=0 BGCOLOR="#0000CC" CELLSPACING=1 CELLPADDING=0 WIDTH="100%">
<TR>
<TD>
<P><FONT FACE="Arial"><TABLE BORDER=0 BGCOLOR="#FFFFCC" CELLSPACING=0 CELLPADDING=0 WIDTH="100%">
<TR>
<TD WIDTH=10>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=10 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD WIDTH=120>
<P><A HREF="http://www.pushtotest.com"><FONT FACE="Arial"><IMG SRC="../images/ptt_logo_120w.gif" WIDTH=120 HEIGHT=32 X-CLARIS-USEIMAGEWIDTH X-CLARIS-USEIMAGEHEIGHT BORDER=0 ALIGN=bottom></FONT></A></P>
</TD>
<TD WIDTH=30>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD>
<P><FONT FACE="Arial">TestMaker Script
Guide</FONT></P>
</TD>
<TD VALIGN=top ROWSPAN=3>
<P><A HREF="../index.html"><FONT SIZE="-1" FACE="Arial">Table
of Contents</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
<BR>
TestMaker Script Guide Chapters<BR>
</FONT><A HREF="script_index.html"><FONT SIZE="-1" FACE="Arial">Introduction</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
</FONT><A HREF="flow_control.html"><FONT SIZE="-1" FACE="Arial">Flow
Control</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
</FONT><A HREF="data_structures.html"><FONT SIZE="-1" FACE="Arial">Data
Structures</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
</FONT><A HREF="modules.html"><FONT SIZE="-1" FACE="Arial">Modules</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
</FONT><A HREF="input_output.html"><FONT SIZE="-1" FACE="Arial">Input
and Output</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
</FONT><A HREF="exceptions.html"><FONT SIZE="-1" FACE="Arial">Errors
and Exceptions</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
</FONT><A HREF="classes.html"><FONT SIZE="-1" FACE="Arial">Classes</FONT></A><FONT SIZE="-1" FACE="Arial"><BR>
</FONT><A HREF="floating_point.html"><FONT SIZE="-1" FACE="Arial">Floating
Point Numbers</FONT></A></P>
<P></P>
</TD>
</TR>
<TR>
<TD WIDTH=10>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=10 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD WIDTH=120>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=120 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD WIDTH=30>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD VALIGN=top>
<P><FONT SIZE="-1" FACE="Arial">Published: October
21, 2002<BR>
Applies to: TestMaker 3.0</FONT></P>
</TD>
</TR>
<TR>
<TD WIDTH=10>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=10 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD WIDTH=120>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=120 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD WIDTH=30>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD VALIGN=top>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
</TR>
</TABLE>
</FONT></P>
</TD>
</TR>
</TABLE>
<BR>
Flow Control</FONT></H1>
<P>Besides the <TT>while</TT> statement just introduced, Python knows
the usual control flow statements known from other languages, with
some twists.</P>
<P> </P>
<H3>
<HR>
<FONT FACE="Arial" COLOR="#000099"><TT>if</TT></FONT></H3>
<P><FONT FACE="Arial">Perhaps the most well-known statement type is
the <TT>if</TT> statement. For example:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> x = int(raw_input("Please enter an integer: "))
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">There can be zero or more <TT>elif</TT> parts,
and the <TT>else</TT> part is optional. The keyword `<TT>elif</TT>'
is short for `else if', and is useful to avoid excessive indentation.
An <TT>if</TT> ... <TT>elif</TT> ... <TT>elif</TT> ... sequence is a
substitute for the <TT>switch</TT> or <TT>case</TT> statements found
in other languages.</FONT></P>
<P><FONT FACE="Arial"> </FONT></P>
<H3><FONT FACE="Arial">
<HR>
</FONT><FONT FACE="Arial" COLOR="#000099"><TT>for</TT></FONT></H3>
<P><FONT FACE="Arial">The
<TT>for</TT><A NAME="l2h-4"></A> statement in Python differs a
bit from what you may be used to in C or Pascal. Rather than always
iterating over an arithmetic progression of numbers (like in Pascal),
or giving the user the ability to define both the iteration step and
halting condition (as C), Python's
<TT>for</TT><A NAME="l2h-5"></A> statement iterates over the
items of any sequence (a list or a string), in the order that they
appear in the sequence. For example (no pun intended):</FONT></P>
<P><FONT FACE="Arial"> </FONT></P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> # Measure some strings:
... a = ['cat', 'window', 'defenestrate']
>>> for x in a:
... print x, len(x)
...
cat 3
window 6
defenestrate 12
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">It is not safe to modify the sequence being
iterated over in the loop (this can only happen for mutable sequence
types, such as lists). If you need to modify the list you are
iterating over (for example, to duplicate selected items) you must
iterate over a copy. The slice notation makes this particularly
convenient:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> for x in a[:]: # make a slice copy of the entire list
... if len(x) > 6: a.insert(0, x)
...
>>> a
['defenestrate', 'cat', 'window', 'defenestrate']
</PRE></BLOCKQUOTE>
<P> </P>
<H3><FONT FACE="Arial">
<HR>
</FONT><FONT FACE="Arial" COLOR="#000099"><TT>range()</TT></FONT></H3>
<P><FONT FACE="Arial">If you do need to iterate over a sequence of
numbers, the built-in function <TT>range()</TT> comes in handy. It
generates lists containing arithmetic progressions:</FONT></P>
<P><FONT FACE="Arial"> </FONT> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">The given end point is never part of the
generated list; <CODE>range(10)</CODE> generates a list of 10 values,
exactly the legal indices for items of a sequence of length 10. It is
possible to let the range start at another number, or to specify a
different increment (even negative; sometimes this is called the
`step'):</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> range(5, 10)
[5, 6, 7, 8, 9]
>>> range(0, 10, 3)
[0, 3, 6, 9]
>>> range(-10, -100, -30)
[-10, -40, -70]
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">To iterate over the indices of a sequence,
combine <TT>range()</TT> and <TT>len()</TT> as follows:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print i, a[i]
...
0 Mary
1 had
2 a
3 little
4 lamb
</PRE></BLOCKQUOTE>
<P> </P>
<H3>
<HR>
<FONT FACE="Arial" COLOR="#000099"><TT>break</TT>, <TT>continue,</TT>
<TT>else</TT> loops</FONT></H3>
<P><FONT FACE="Arial">The <TT>break</TT> statement, like in C, breaks
out of the smallest enclosing <TT>for</TT> or <TT>while</TT>
loop.</FONT></P>
<P><FONT FACE="Arial">The <TT>continue</TT> statement, also borrowed
from C, continues with the next iteration of the loop.</FONT></P>
<P><FONT FACE="Arial">Loop statements may have an <CODE>else</CODE>
clause; it is executed when the loop terminates through exhaustion of
the list (with <TT>for</TT>) or when the condition becomes false
(with <TT>while</TT>), but not when the loop is terminated by a
<TT>break</TT> statement. This is exemplified by the following loop,
which searches for prime numbers:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
</PRE></BLOCKQUOTE>
<P> </P>
<H3>
<HR>
<FONT FACE="Arial" COLOR="#000099"><TT>pass</TT></FONT></H3>
<P><FONT FACE="Arial">The <TT>pass</TT> statement does nothing. It
can be used when a statement is required syntactically but the
program requires no action. For example:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> while 1:
... pass # Busy-wait for keyboard interrupt
...
</PRE></BLOCKQUOTE>
<P>
<HR>
<FONT FACE="Arial" COLOR="#000099"><TT>sys.exit(0)</TT></FONT></P>
<P><FONT FACE="Arial">The sys object holds many functions, including
the exit method to discontinue the currently running agent. By
calling sys.exit(0) you stop the currently running agent. The agent
returns the numeric value in the exit() parameter. To access
sys.exit(0) requires you import sys first.</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> import sys
>>> sys.exit(0)
...
</PRE></BLOCKQUOTE>
<P> </P>
<H2>
<HR>
<FONT FACE="Arial" COLOR="#000099">Defining Functions</FONT></H2>
<P><FONT FACE="Arial">We can create a function that writes the
Fibonacci series to an arbitrary boundary:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> def fib(n): # write Fibonacci series up to n
... """Print a Fibonacci series up to n."""
... a, b = 0, 1
... while b < n:
... print b,
... a, b = b, a+b
...
>>> # Now call the function we just defined:
... fib(2000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">The keyword <TT>def</TT> introduces a function
<I>definition</I>. It must be followed by the function name and the
parenthesized list of formal parameters. The statements that form the
body of the function start at the next line, and must be indented.
The first statement of the function body can optionally be a string
literal; this string literal is the function's
<A NAME="l2h-6"></A> documentation string, or
<I>docstring</I>.<A NAME="l2h-7"></A> </FONT></P>
<P><FONT FACE="Arial">There are tools which use docstrings to
automatically produce online or printed documentation, or to let the
user interactively browse through code; it's good practice to include
docstrings in code that you write, so try to make a habit of
it.</FONT></P>
<P><FONT FACE="Arial">The <I>execution</I> of a function introduces a
new symbol table used for the local variables of the function. More
precisely, all variable assignments in a function store the value in
the local symbol table; whereas variable references first look in the
local symbol table, then in the global symbol table, and then in the
table of built-in names. Thus, global variables cannot be directly
assigned a value within a function (unless named in a <TT>global</TT>
statement), although they may be referenced.</FONT></P>
<P><FONT FACE="Arial">The actual parameters (arguments) to a function
call are introduced in the local symbol table of the called function
when it is called; thus, arguments are passed using <I>call by
value</I> (where the <I>value</I> is always an object
<I>reference</I>, not the value of the object).</FONT><A HREF="#foot1349" name=tex2html2><FONT FACE="Arial"><SUP>4.1</SUP></FONT></A><FONT FACE="Arial">
When a function calls another function, a new local symbol table is
created for that call.</FONT></P>
<P><FONT FACE="Arial">A function definition introduces the function
name in the current symbol table. The value of the function name has
a type that is recognized by the interpreter as a user-defined
function. This value can be assigned to another name which can then
also be used as a function. This serves as a general renaming
mechanism:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> fib
<function object at 10042ed0>
>>> f = fib
>>> f(100)
1 1 2 3 5 8 13 21 34 55 89
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">You might object that <CODE>fib</CODE> is not a
function but a procedure. In Python, like in C, procedures are just
functions that don't return a value. In fact, technically speaking,
procedures do return a value, albeit a rather boring one. This value
is called <CODE>None</CODE> (it's a built-in name). Writing the value
<CODE>None</CODE> is normally suppressed by the interpreter if it
would be the only value written. You can see it if you really want
to:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> print fib(0)
None
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">It is simple to write a function that returns a
list of the numbers of the Fibonacci series, instead of printing
it:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> def fib2(n): # return Fibonacci series up to n
... """Return a list containing the Fibonacci series up to n."""
... result = []
... a, b = 0, 1
... while b < n:
... result.append(b) # see below
... a, b = b, a+b
... return result
...
>>> f100 = fib2(100) # call it
>>> f100 # write the result
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">This example, as usual, demonstrates some new
Python features:</FONT></P>
<UL>
<LI><FONT FACE="Arial">The <TT>return</TT> statement returns with
a value from a function. <TT>return</TT> without an expression
argument returns <CODE>None</CODE>. Falling off the end of a
procedure also returns <CODE>None</CODE>.<BR>
<BR>
</FONT></LI>
<LI><FONT FACE="Arial">The statement <CODE>result.append(b)</CODE>
calls a <I>method</I> of the list object <CODE>result</CODE>. A
method is a function that `belongs' to an object and is named
<CODE>obj.methodname</CODE>, where <CODE>obj</CODE> is some object
(this may be an expression), and <CODE>methodname</CODE> is the
name of a method that is defined by the object's type. Different
types define different methods. Methods of different types may
have the same name without causing ambiguity. (It is possible to
define your own object types and methods, using <I>classes</I>, as
discussed later in this tutorial.) The method <TT>append()</TT>
shown in the example, is defined for list objects; it adds a new
element at the end of the list. In this example it is equivalent
to "<TT>result = result + [b]</TT>", but more
efficient.</FONT>
<P> </P></LI>
</UL>
<H2>
<HR>
<FONT FACE="Arial" COLOR="#000099">More on Defining
Functions</FONT></H2>
<P><FONT FACE="Arial">It is also possible to define functions with a
variable number of arguments. There are three forms, which can be
combined.</FONT></P>
<P><FONT FACE="Arial"> </FONT></P>
<H3><FONT FACE="Arial" COLOR="#000099">Default Argument
Values</FONT></H3>
<P><FONT FACE="Arial">The most useful form is to specify a default
value for one or more arguments. This creates a function that can be
called with fewer arguments than it is defined</FONT></P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while 1:
ok = raw_input(prompt)
if ok in ('y', 'ye', 'yes'): return 1
if ok in ('n', 'no', 'nop', 'nope'): return 0
retries = retries - 1
if retries < 0: raise IOError, 'refusenik user'
print complaint
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">This function can be called either like this:
<CODE>ask_ok('Do you really want to quit?')</CODE> or like this:
<CODE>ask_ok('OK to overwrite the file?', 2)</CODE>.</FONT></P>
<P><FONT FACE="Arial">The default values are evaluated at the point
of function definition in the <I>defining</I> scope, so
that</FONT></P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>
i = 5
def f(arg=i):
print arg
i = 6
f()
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">will print <CODE>5</CODE>.</FONT></P>
<P><FONT FACE="Arial"><B>Important warning:</B> The default value is
evaluated only once. This makes a difference when the default is a
mutable object such as a list or dictionary. For example, the
following function accumulates the arguments passed to it on
subsequent calls:</FONT></P>
<P><FONT FACE="Arial"> </FONT></P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>def f(a, L=[]):
L.append(a)
return L
print f(1)
print f(2)
print f(3)
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">This will print</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>[1]
[1, 2]
[1, 2, 3]
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">If you don't want the default to be shared
between subsequent calls, you can write the function like this
instead:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
</PRE></BLOCKQUOTE>
<P> </P>
<H3>
<HR>
<FONT FACE="Arial" COLOR="#000099">Keyword Arguments</FONT></H3>
<P><FONT FACE="Arial">Functions can also be called using keyword
arguments of the form "<TT><VAR>keyword</VAR> =
<VAR>value</VAR></TT>". For instance, the following
function:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
print "-- This parrot wouldn't", action,
print "if you put", voltage, "Volts through it."
print "-- Lovely plumage, the", type
print "-- It's", state, "!"
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">could be called in any of the following
ways:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>parrot(1000)
parrot(action = 'VOOOOOM', voltage = 1000000)
parrot('a thousand', state = 'pushing up the daisies')
parrot('a million', 'bereft of life', 'jump')
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">but the following calls would all be
invalid:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>parrot() # required argument missing
parrot(voltage=5.0, 'dead') # non-keyword argument following keyword
parrot(110, voltage=220) # duplicate value for argument
parrot(actor='John Cleese') # unknown keyword
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">In general, an argument list must have any
positional arguments followed by any keyword arguments, where the
keywords must be chosen from the formal parameter names. It's not
important whether a formal parameter has a default value or not. No
argument may receive a value more than once -- formal parameter names
corresponding to positional arguments cannot be used as keywords in
the same calls. Here's an example that fails due to this
restriction:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> def function(a):
... pass
...
>>> function(0, a=0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: keyword parameter redefined
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">When a final formal parameter of the form
<CODE>**<VAR>name</VAR></CODE> is present, it receives a dictionary
containing all keyword arguments whose keyword doesn't correspond to
a formal parameter. This may be combined with a formal parameter of
the form <CODE>*<VAR>name</VAR></CODE> (described in the next
subsection) which receives a tuple containing the positional
arguments beyond the formal parameter list.
(<CODE>*<VAR>name</VAR></CODE> must occur before
<CODE>**<VAR>name</VAR></CODE>.) For example, if we define a function
like this:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>def cheeseshop(kind, *arguments, **keywords):
print "-- Do you have any", kind, '?'
print "-- I'm sorry, we're all out of", kind
for arg in arguments: print arg
print '-'*40
for kw in keywords.keys(): print kw, ':', keywords[kw]
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">It could be called like this:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>cheeseshop('Limburger', "It's very runny, sir.",
"It's really very, VERY runny, sir.",
client='John Cleese',
shopkeeper='Michael Palin',
sketch='Cheese Shop Sketch')
</PRE></BLOCKQUOTE>
<P><FONT FACE="Arial">and of course it would print:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
client : John Cleese
shopkeeper : Michael Palin
sketch : Cheese Shop Sketch
</PRE></BLOCKQUOTE>
<P> </P>
<H3><FONT FACE="Arial">
<HR>
</FONT><FONT FACE="Arial" COLOR="#000099">Arbitrary Argument
Lists</FONT></H3>
<P><FONT FACE="Arial">Finally, the least frequently used option is to
specify that a function can be called with an arbitrary number of
arguments. These arguments will be wrapped up in a tuple. Before the
variable number of arguments, zero or more normal arguments may
occur.</FONT></P>
<P><FONT FACE="Arial"> </FONT></P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>def fprintf(file, format, *args):
file.write(format % args)
</PRE></BLOCKQUOTE>
<P> </P>
<H3><FONT COLOR="#000099">
<HR>
</FONT><FONT FACE="Arial" COLOR="#000099">Lambda Forms</FONT></H3>
<P><FONT FACE="Arial">By popular demand, a few features commonly
found in functional programming languages and Lisp have been added to
Python. With the <TT>lambda</TT> keyword, small anonymous functions
can be created. Here's a function that returns the sum of its two
arguments: "<TT>lambda a, b: a+b</TT>". Lambda forms can be used
wherever function objects are required. They are syntactically
restricted to a single expression. Semantically, they are just
syntactic sugar for a normal function definition. Like nested
function definitions, lambda forms can reference variables from the
containing scope:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43
</PRE></BLOCKQUOTE>
<P> </P>
<H3>
<HR>
<FONT FACE="Arial" COLOR="#000099">Documentation Strings</FONT></H3>
<P><FONT FACE="Arial">There are emerging conventions about the
content and formatting of documentation strings.
<A NAME="l2h-8"></A> </FONT></P>
<P><FONT FACE="Arial">The first line should always be a short,
concise summary of the object's purpose. For brevity, it should not
explicitly state the object's name or type, since these are available
by other means (except if the name happens to be a verb describing a
function's operation). This line should begin with a capital letter
and end with a period.</FONT></P>
<P><FONT FACE="Arial">If there are more lines in the documentation
string, the second line should be blank, visually separating the
summary from the rest of the description. The following lines should
be one or more paragraphs describing the object's calling
conventions, its side effects, etc.</FONT></P>
<P><FONT FACE="Arial">The Python parser does not strip indentation
from multi-line string literals in Python, so tools that process
documentation have to strip indentation if desired. This is done
using the following convention. The first non-blank line <I>after</I>
the first line of the string determines the amount of indentation for
the entire documentation string. (We can't use the first line since
it is generally adjacent to the string's opening quotes so its
indentation is not apparent in the string literal.) Whitespace
``equivalent'' to this indentation is then stripped from the start of
all lines of the string. Lines that are indented less should not
occur, but if they occur all their leading whitespace should be
stripped. Equivalence of whitespace should be tested after expansion
of tabs (to 8 spaces, normally).</FONT></P>
<P><FONT FACE="Arial">Here is an example of a multi-line
docstring:</FONT></P>
<P> </P>
<BLOCKQUOTE class=verbatim><PRE class=verbatim>>>> def my_function():
... """Do nothing, but document it.
...
... No, really, it doesn't do anything.
... """
... pass
...
>>> print my_function.__doc__
Do nothing, but document it.
No, really, it doesn't do anything.
</PRE></BLOCKQUOTE>
<H2> </H2>
<H2><FONT FACE="Arial" COLOR="#0033FF">
<HR>
What's Next?</FONT></H2>
<P><FONT FACE="Arial">Choose one of the other chapters in the
Scripting Guide:</FONT></P>
<OL>
<LI><A HREF="#Data"><FONT FACE="Arial">DataTypes</FONT></A></LI>
<LI><A HREF="flow_control.html"><FONT FACE="Arial">Flow Control
and Functions</FONT></A></LI>
<LI><A HREF="data_structures.html"><FONT FACE="Arial">Data
Structures</FONT></A></LI>
<LI><A HREF="modules.html"><FONT FACE="Arial">Modules</FONT></A></LI>
<LI><A HREF="input_output.html"><FONT FACE="Arial">Input and
Output</FONT></A></LI>
<LI><A HREF="exceptions.html"><FONT FACE="Arial">Errors and
Exceptions</FONT></A></LI>
<LI><A HREF="classes.html"><FONT FACE="Arial">Classes</FONT></A></LI>
<LI><A HREF="floating_point.html"><FONT FACE="Arial">Floating
Point Numbers</FONT></A></LI>
</OL>
<PRE>
<FONT FACE="Arial"> </FONT></PRE>
<P><FONT FACE="Arial"><TABLE BORDER=0 BGCOLOR="#0000CC" CELLSPACING=1 CELLPADDING=0 WIDTH="100%">
<TR>
<TD>
<P><FONT FACE="Arial"><TABLE BORDER=0 BGCOLOR="#FFFFCC" CELLPADDING=0 WIDTH="100%">
<TR>
<TD WIDTH=10>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=10 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD WIDTH=120>
<P><A HREF="http://www.pushtotest.com"><FONT FACE="Arial"><IMG SRC="../images/ptt_logo_120w.gif" WIDTH=120 HEIGHT=32 X-CLARIS-USEIMAGEWIDTH X-CLARIS-USEIMAGEHEIGHT BORDER=0 ALIGN=bottom></FONT></A></P>
</TD>
<TD WIDTH=30>
<P><FONT FACE="Arial"><IMG SRC="../images/blank.gif" WIDTH=30 HEIGHT=1 X-CLARIS-USEIMAGEHEIGHT ALIGN=bottom></FONT></P>
</TD>
<TD>
<P><FONT SIZE="-1" FACE="Arial">Additional
documentation, product downloads and updates are at
</FONT><A HREF="http://www.pushtotest.com"><FONT SIZE="-1" FACE="Arial">www.PushToTest.com</FONT></A><FONT SIZE="-1" FACE="Arial">.
While the TestMaker software is distributed under
an open-source license, the documentation remains
(c) 2002 PushToTest. All rights
reserved.</FONT></P>
</TD>
</TR>
</TABLE>
</FONT></P>
</TD>
</TR>
</TABLE>
</FONT></P>
</BODY>
</HTML>