blob: a51fb83657dd4f897ff3a8f9b5dafcdaa4051b63 [file] [log] [blame]
Guido van Rossum37953781992-04-06 14:04:04 +00001\documentstyle[twoside,11pt,myformat]{report}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002
3\title{\bf
Guido van Rossum6fc178f1991-08-16 09:13:42 +00004 Python Tutorial
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00005}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00006
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00007\author{
8 Guido van Rossum \\
9 Dept. CST, CWI, Kruislaan 413 \\
10 1098 SJ Amsterdam, The Netherlands \\
11 E-mail: {\tt guido@cwi.nl}
12}
13
14\begin{document}
15
16\pagenumbering{roman}
17
18\maketitle
19
20\begin{abstract}
21
22\noindent
Guido van Rossum4410c751991-06-04 20:22:18 +000023Python is a simple, yet powerful programming language that bridges the
Guido van Rossum6fc178f1991-08-16 09:13:42 +000024gap between C and shell programming, and is thus ideally suited for
25``throw-away programming''
26and rapid prototyping. Its syntax is put
27together from constructs borrowed from a variety of other languages;
28most prominent are influences from ABC, C, Modula-3 and Icon.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000029
Guido van Rossum4410c751991-06-04 20:22:18 +000030The Python interpreter is easily extended with new functions and data
Guido van Rossum6fc178f1991-08-16 09:13:42 +000031types implemented in C. Python is also suitable as an extension
32language for highly customizable C applications such as editors or
33window managers.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000034
Guido van Rossum4410c751991-06-04 20:22:18 +000035Python is available for various operating systems, amongst which
Guido van Rossum6fc178f1991-08-16 09:13:42 +000036several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
37and MS-DOS.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000038
Guido van Rossum6fc178f1991-08-16 09:13:42 +000039This tutorial introduces the reader informally to the basic concepts
40and features of the Python language and system. It helps to have a
41Python interpreter handy for hands-on experience, but as the examples
42are self-contained, the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000043
Guido van Rossum481ae681991-11-25 17:28:03 +000044For a description of standard objects and modules, see the {\em Python
45Library Reference} document. The {\em Python Reference Manual} gives
46a more formal definition of the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000047
48\end{abstract}
49
50\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000051{
52\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000053\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000054}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000055
56\pagebreak
57
58\pagenumbering{arabic}
59
Guido van Rossum5e0759d1992-08-07 16:06:24 +000060
Guido van Rossum6fc178f1991-08-16 09:13:42 +000061\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000062
Guido van Rossum6fc178f1991-08-16 09:13:42 +000063If you ever wrote a large shell script, you probably know this
64feeling: you'd love to add yet another feature, but it's already so
65slow, and so big, and so complicated; or the feature involves a system
66call or other funcion that is only accessible from C \ldots Usually
67the problem at hand isn't serious enough to warrant rewriting the
68script in C; perhaps because the problem requires variable-length
69strings or other data types (like sorted lists of file names) that are
70easy in the shell but lots of work to implement in C; or perhaps just
71because you're not sufficiently familiar with C.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000072
Guido van Rossum6fc178f1991-08-16 09:13:42 +000073In such cases, Python may be just the language for you. Python is
74simple to use, but it is a real programming language, offering much
75more structure and support for large programs than the shell has. On
76the other hand, it also offers much more error checking than C, and,
77being a {\em very-high-level language}, it has high-level data types
78built in, such as flexible arrays and dictionaries that would cost you
79days to implement efficiently in C. Because of its more general data
80types Python is applicable to a much larger problem domain than {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +000081Awk} or even {\em Perl}, yet many things are at least as easy in
82Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000083
Guido van Rossum6fc178f1991-08-16 09:13:42 +000084Python allows you to split up your program in modules that can be
85reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +000086standard modules that you can use as the basis of your programs --- or
87as examples to start learning to program in Python. There are also
88built-in modules that provide things like file I/O, system calls,
89sockets, and even a generic interface to window systems (STDWIN).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000090
Guido van Rossuma8d754e1992-01-07 16:44:35 +000091Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000092during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +000093necessary. The interpreter can be used interactively, which makes it
94easy to experiment with features of the language, to write throw-away
95programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000096It is also a handy desk calculator.
97
Guido van Rossum6fc178f1991-08-16 09:13:42 +000098Python allows writing very compact and readable programs. Programs
99written in Python are typically much shorter than equivalent C
100programs, for several reasons:
101\begin{itemize}
102\item
103the high-level data types allow you to express complex operations in a
104single statement;
105\item
106statement grouping is done by indentation instead of begin/end
107brackets;
108\item
109no variable or argument declarations are necessary.
110\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000111
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000112Python is {\em extensible}: if you know how to program in C it is easy
113to add a new built-in
114function or
115module to the interpreter, either to
116perform critical operations at maximum speed, or to link Python
117programs to libraries that may only be available in binary form (such
118as a vendor-specific graphics library). Once you are really hooked,
119you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000120and use it as an extension or command language for that application.
121
122By the way, the language is named after the BBC show ``Monty
123Python's Flying Circus'' and has nothing to do with nasty reptiles...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000124
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000125\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000126
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000127Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000128in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000129using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000130
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000131In the next chapter, the mechanics of using the interpreter are
132explained. This is rather mundane information, but essential for
133trying out the examples shown later.
134
Guido van Rossum4410c751991-06-04 20:22:18 +0000135The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000136language and system though examples, beginning with simple
137expressions, statements and data types, through functions and modules,
138and finally touching upon advanced concepts like exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000139
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000140When you're through with the turtorial (or just getting bored), you
141should read the Library Reference, which gives complete (though terse)
142reference material about built-in and standard types, functions and
143modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000144
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000145
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000146\chapter{Using the Python Interpreter}
147
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000148\section{Invoking the Interpreter}
149
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000150The Python interpreter is usually installed as {\tt /usr/local/bin/python}
151on those machines where it is available; putting {\tt /usr/local/bin} in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000152your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000153typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000154
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000155\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000156python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000157\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000158%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000159to the shell. Since the choice of the directory where the interpreter
160lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000161your local Python guru or system administrator. (E.g., {\tt
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000162/usr/local/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000163
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000164The interpreter operates somewhat like the {\UNIX} shell: when called
165with standard input connected to a tty device, it reads and executes
166commands interactively; when called with a file name argument or with
167a file as standard input, it reads and executes a {\em script} from
168that file.
169
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000170A third way of starting the interpreter is
171``{\tt python -c command [arg] ...}'', which
172executes the statement(s) in {\tt command}, analogous to the shell's
173{\tt -c} option. Since Python statements often contain spaces or other
174characters that are special to the shell, it is best to quote {\tt
175command} in its entirety with double quotes.
176
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000177Note that there is a difference between ``{\tt python file}'' and
178``{\tt python $<$file}''. In the latter case, input requests from the
Guido van Rossum573805a1992-03-06 10:56:03 +0000179program, such as calls to {\tt input()} and {\tt raw_input()}, are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000180satisfied from {\em file}. Since this file has already been read
181until the end by the parser before the program starts executing, the
182program will encounter EOF immediately. In the former case (which is
183usually what you want) they are satisfied from whatever file or device
184is connected to standard input of the Python interpreter.
185
Guido van Rossumb2c65561993-05-12 08:53:36 +0000186When a script file is used, it is sometimes useful to be able to run
187the script and enter interactive mode afterwards. This can be done by
188passing {\tt -i} before the script. (This does not work if the script
189is read from standard input, for the same reason as explained in the
190previous paragraph.)
191
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000192\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000193
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000194When known to the interpreter, the script name and additional
195arguments thereafter are passed to the script in the variable {\tt
196sys.argv}, which is a list of strings. Its length is at least one;
197when no script and no arguments are given, {\tt sys.argv[0]} is an
198empty string. When the script name is given as {\tt '-'} (meaning
199standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
200command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
201found after {\tt -c command} are not consumed by the Python
202interpreter's option processing but left in {\tt sys.argv} for the
203command to handle.
204
205\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000206
Guido van Rossumdd010801991-06-07 14:31:11 +0000207When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000208{\em interactive\ mode}. In this mode it prompts for the next command
209with the {\em primary\ prompt}, usually three greater-than signs ({\tt
210>>>}); for continuation lines it prompts with the {\em secondary\
211prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
212at the primary prompt causes the interpreter to exit with a zero exit
213status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000214
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000215The interpreter prints a welcome message stating its version number
216and a copyright notice before printing the first prompt, e.g.:
217
218\bcode\begin{verbatim}
219python
Guido van Rossumb2c65561993-05-12 08:53:36 +0000220Python 0.9.9 (Apr 2 1993).
221Copyright 1990, 1991, 1992, 1993 Stichting Mathematisch Centrum, Amsterdam
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000222>>>
223\end{verbatim}\ecode
224
225\section{The Interpreter and its Environment}
226
227\subsection{Error Handling}
228
229When an error occurs, the interpreter prints an error
230message and a stack trace. In interactive mode, it then returns to
231the primary prompt; when input came from a file, it exits with a
232nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000233the stack trace. (Exceptions handled by an {\tt except} clause in a
234{\tt try} statement are not errors in this context.) Some errors are
235unconditionally fatal and cause an exit with a nonzero exit; this
236applies to internal inconsistencies and some cases of running out of
237memory. All error messages are written to the standard error stream;
238normal output from the executed commands is written to standard
239output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000240
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000241Typing the interrupt character (usually Control-C or DEL) to the
242primary or secondary prompt cancels the input and returns to the
243primary prompt.%
244\footnote{
245 A problem with the GNU Readline package may prevent this.
246}
247Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000248KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000249statement.
250
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000251\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000252
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000253When a module named {\tt foo} is imported, the interpreter searches
254for a file named {\tt foo.py} in the list of directories specified by
255the environment variable {\tt PYTHONPATH}. It has the same syntax as
256the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000257directory names. When {\tt PYTHONPATH} is not set, or when the file
258is not found there, the search continues in an installation-dependent
259default path, usually {\tt .:/usr/local/lib/python}.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000260
261Actually, modules are searched in the list of directories given by the
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000262variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} and
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000263the installation-dependent default. This allows Python programs that
264know what they're doing to modify or replace the module search path.
265See the section on Standard Modules later.
266
267\subsection{``Compiled'' Python files}
268
269As an important speed-up of the start-up time for short programs that
270use a lot of standard modules, if a file called {\tt foo.pyc} exists
271in the directory where {\tt foo.py} is found, this is assumed to
272contain an already-``compiled'' version of the module {\tt foo}. The
273modification time of the version of {\tt foo.py} used to create {\tt
274foo.pyc} is recorded in {\tt foo.pyc}, and the file is ignored if
275these don't match.
276
277Whenever {\tt foo.py} is successfully compiled, an attempt is made to
278write the compiled version to {\tt foo.pyc}. It is not an error if
279this attempt fails; if for any reason the file is not written
280completely, the resulting {\tt foo.pyc} file will be recognized as
281invalid and thus ignored later.
282
283\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000284
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000285On BSD'ish {\UNIX} systems, Python scripts can be made directly
286executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000287
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000288\bcode\begin{verbatim}
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000289#! /usr/local/bin/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000290\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000291%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000292(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000293script and giving the file an executable mode. The {\tt \#!} must be
294the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000295
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000296\subsection{The Interactive Startup File}
297
298When you use Python interactively, it is frequently handy to have some
299standard commands executed every time the interpreter is started. You
300can do this by setting an environment variable named {\tt
301PYTHONSTARTUP} to the name of a file containing your start-up
302commands. This is similar to the {\tt /profile} feature of the UNIX
303shells.
304
305This file is only read in interactive sessions, not when Python reads
306commands from a script, and not when {\tt /dev/tty} is given as the
307explicit source of commands (which otherwise behaves like an
308interactive session). It is executed in the same name space where
309interactive commands are executed, so that objects that it defines or
310imports can be used without qualification in the interactive session.
Guido van Rossum7b3c8a11992-09-08 09:20:13 +0000311You can also change the prompts {\tt sys.ps1} and {\tt sys.ps2} in
312this file.
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000313
314If you want to read an additional start-up file from the current
315directory, you can program this in the global start-up file, e.g.
316\verb\execfile('.pythonrc')\. If you want to use the startup file
317in a script, you must write this explicitly in the script, e.g.
318\verb\import os;\ \verb\execfile(os.environ['PYTHONSTARTUP'])\.
319
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000320\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000321
Guido van Rossum4410c751991-06-04 20:22:18 +0000322Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000323input line and history substitution, similar to facilities found in
324the Korn shell and the GNU Bash shell. This is implemented using the
325{\em GNU\ Readline} library, which supports Emacs-style and vi-style
326editing. This library has its own documentation which I won't
327duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000328
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000329Perhaps the quickest check to see whether command line editing is
330supported is typing Control-P to the first Python prompt you get. If
331it beeps, you have command line editing. If nothing appears to
332happen, or if \verb/^P/ is echoed, you can skip the rest of this
333section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000334
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000335\subsection{Line Editing}
336
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000337If supported, input line editing is active whenever the interpreter
338prints a primary or secondary prompt. The current line can be edited
339using the conventional Emacs control characters. The most important
340of these are: C-A (Control-A) moves the cursor to the beginning of the
341line, C-E to the end, C-B moves it one position to the left, C-F to
342the right. Backspace erases the character to the left of the cursor,
343C-D the character to its right. C-K kills (erases) the rest of the
344line to the right of the cursor, C-Y yanks back the last killed
345string. C-underscore undoes the last change you made; it can be
346repeated for cumulative effect.
347
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000348\subsection{History Substitution}
349
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000350History substitution works as follows. All non-empty input lines
351issued are saved in a history buffer, and when a new prompt is given
352you are positioned on a new line at the bottom of this buffer. C-P
353moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000354Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000355front of the prompt to mark a line as modified. Pressing the Return
356key passes the current line to the interpreter. C-R starts an
357incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000358
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000359\subsection{Key Bindings}
360
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000361The key bindings and some other parameters of the Readline library can
362be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000363{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000364
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000365\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000366key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000367\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000368%
369or
370
371\bcode\begin{verbatim}
372"string": function-name
373\end{verbatim}\ecode
374%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000375and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000376
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000377\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000378set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000379\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000380%
381For example:
382
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000383\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000384# I prefer vi-style editing:
385set editing-mode vi
386# Edit using a single line:
387set horizontal-scroll-mode On
388# Rebind some keys:
389Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000390"\C-u": universal-argument
391"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000392\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000393%
Guido van Rossum4410c751991-06-04 20:22:18 +0000394Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000395instead of Readline's default filename completion function. If you
396insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000397
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000398\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000399TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000400\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000401%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000402in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000403indented continuation lines...)
404
405\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000406
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000407This facility is an enormous step forward compared to previous
408versions of the interpreter; however, some wishes are left: It would
409be nice if the proper indentation were suggested on continuation lines
410(the parser knows if an indent token is required next). The
411completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000412command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000413would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000414
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000415
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000416\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000417
418In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000419presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
420the example, you must type everything after the prompt, when the
421prompt appears; lines that do not begin with a prompt are output from
422the interpreter.%
423\footnote{
424 I'd prefer to use different fonts to distinguish input
425 from output, but the amount of LaTeX hacking that would require
426 is currently beyond my ability.
427}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000428Note that a secondary prompt on a line by itself in an example means
429you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000430
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000431\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000432
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000433Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000434for the primary prompt, {\tt >>>}. (It shouldn't take long.)
435
436\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000437
438The interpreter acts as a simple calculator: you can type an
439expression at it and it will write the value. Expression syntax is
440straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000441work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000442can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000443
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000444\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000445>>> # This is a comment
446>>> 2+2
4474
448>>>
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000449>>> (50-5*6)/4
4505
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000451>>> # Division truncates towards zero:
452>>> 7/3
4532
454>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000455\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000456%
457Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000458variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000459
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000460\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000461>>> width = 20
462>>> height = 5*9
463>>> width * height
464900
465>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000466\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000467%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000468A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000469
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000470\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000471>>> # Zero x, y and z
472>>> x = y = z = 0
473>>>
474\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000475%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000476There is full support for floating point; operators with mixed type
477operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000478
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000479\bcode\begin{verbatim}
480>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004813.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000482>>> 7.0 / 2
4833.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000484>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000485\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000486
487\subsection{Strings}
488
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000489Besides numbers, Python can also manipulate strings, enclosed in
490single quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000491
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000492\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000493>>> 'foo bar'
494'foo bar'
495>>> 'doesn\'t'
496'doesn\'t'
497>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000498\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000499%
500Strings are written the same way as they are typed for input: inside
501quotes and with quotes and other funny characters escaped by
502backslashes, to show the precise value. (The {\tt print} statement,
503described later, can be used to write strings without quotes or
504escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000505
506Strings can be concatenated (glued together) with the {\tt +}
507operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000508
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000509\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000510>>> word = 'Help' + 'A'
511>>> word
512'HelpA'
513>>> '<' + word*5 + '>'
514'<HelpAHelpAHelpAHelpAHelpA>'
515>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000516\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000517%
518Strings can be subscripted (indexed); like in C, the first character of
519a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000520
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000521There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000522size one. Like in Icon, substrings can be specified with the {\em
523slice} notation: two indices separated by a colon.
524
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000525\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000526>>> word[4]
527'A'
528>>> word[0:2]
529'He'
530>>> word[2:4]
531'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000532>>>
533\end{verbatim}\ecode
534%
535Slice indices have useful defaults; an omitted first index defaults to
536zero, an omitted second index defaults to the size of the string being
537sliced.
538
539\bcode\begin{verbatim}
540>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000541'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000542>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000543'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000544>>>
545\end{verbatim}\ecode
546%
547Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
548equals \verb\s\.
549
550\bcode\begin{verbatim}
551>>> word[:2] + word[2:]
552'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000553>>> word[:3] + word[3:]
554'HelpA'
555>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000556\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000557%
558Degenerate slice indices are handled gracefully: an index that is too
559large is replaced by the string size, an upper bound smaller than the
560lower bound returns an empty string.
561
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000562\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000563>>> word[1:100]
564'elpA'
565>>> word[10:]
566''
567>>> word[2:1]
568''
569>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000570\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000571%
572Indices may be negative numbers, to start counting from the right.
573For example:
574
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000575\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000576>>> word[-1] # The last character
577'A'
578>>> word[-2] # The last-but-one character
579'p'
580>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000581'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000582>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000583'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000584>>>
585\end{verbatim}\ecode
586%
587But note that -0 is really the same as 0, so it does not count from
588the right!
589
590\bcode\begin{verbatim}
591>>> word[-0] # (since -0 equals 0)
592'H'
593>>>
594\end{verbatim}\ecode
595%
596Out-of-range negative slice indices are truncated, but don't try this
597for single-element (non-slice) indices:
598
599\bcode\begin{verbatim}
600>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000601'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000602>>> word[-10] # error
603Unhandled exception: IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000604>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000605\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000606%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000607The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000608pointing {\em between} characters, with the left edge of the first
609character numbered 0. Then the right edge of the last character of a
610string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000611
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000612\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000613 +---+---+---+---+---+
614 | H | e | l | p | A |
615 +---+---+---+---+---+
616 0 1 2 3 4 5
617-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000618\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000619%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000620The first row of numbers gives the position of the indices 0...5 in
621the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000622The slice from \verb\i\ to \verb\j\ consists of all characters between
623the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000624
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000625For nonnegative indices, the length of a slice is the difference of
626the indices, if both are within bounds, e.g., the length of
627\verb\word[1:3]\ is 2.
628
629The built-in function {\tt len()} returns the length of a string:
630
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000631\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000632>>> s = 'supercalifragilisticexpialidocious'
633>>> len(s)
63434
635>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000636\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000637
638\subsection{Lists}
639
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000640Python knows a number of {\em compound} data types, used to group
641together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000642can be written as a list of comma-separated values (items) between
643square brackets. List items need not all have the same type.
644
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000645\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000646>>> a = ['foo', 'bar', 100, 1234]
647>>> a
648['foo', 'bar', 100, 1234]
649>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000650\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000651%
652Like string indices, list indices start at 0, and lists can be sliced,
653concatenated and so on:
654
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000655\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000656>>> a[0]
657'foo'
658>>> a[3]
6591234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000660>>> a[-2]
661100
662>>> a[1:-1]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000663['bar', 100]
664>>> a[:2] + ['bletch', 2*2]
665['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000666>>> 3*a[:3] + ['Boe!']
667['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000668>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000669\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000670%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000671Unlike strings, which are {\em immutable}, it is possible to change
672individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000673
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000674\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000675>>> a
676['foo', 'bar', 100, 1234]
677>>> a[2] = a[2] + 23
678>>> a
679['foo', 'bar', 123, 1234]
680>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000681\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000682%
683Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000684of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000685
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000686\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000687>>> # Replace some items:
688>>> a[0:2] = [1, 12]
689>>> a
690[1, 12, 123, 1234]
691>>> # Remove some:
692>>> a[0:2] = []
693>>> a
694[123, 1234]
695>>> # Insert some:
696>>> a[1:1] = ['bletch', 'xyzzy']
697>>> a
698[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000699>>> a[:0] = a # Insert (a copy of) itself at the beginning
700>>> a
701[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000702>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000703\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000704%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000705The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000706
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000707\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000708>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00007098
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000710>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000711\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000712%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000713It is possible to nest lists (create lists containing other lists),
714for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000715
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000716\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000717>>> q = [2, 3]
718>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000719>>> len(p)
7203
721>>> p[1]
722[2, 3]
723>>> p[1][0]
7242
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000725>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000726>>> p
727[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000728>>> q
729[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000730>>>
731\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000732%
733Note that in the last example, {\tt p[1]} and {\tt q} really refer to
734the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000735
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000736\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000737
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000738Of course, we can use Python for more complicated tasks than adding
739two and two together. For instance, we can write an initial
740subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000741
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000742\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000743>>> # Fibonacci series:
744>>> # the sum of two elements defines the next
745>>> a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000746>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000747... print b
748... a, b = b, a+b
749...
7501
7511
7522
7533
7545
7558
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000756>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000757\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000758%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000759This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000760
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000761\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000762
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000763\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000764The first line contains a {\em multiple assignment}: the variables
765{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
766last line this is used again, demonstrating that the expressions on
767the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000768assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000769
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000770\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000771The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000772100}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000773true; zero is false. The condition may also be a string or list value,
774in fact any sequence; anything with a non-zero length is true, empty
775sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000776comparison. The standard comparison operators are written the same as
777in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000778
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000779\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000780The {\em body} of the loop is {\em indented}: indentation is Python's
781way of grouping statements. Python does not (yet!) provide an
782intelligent input line editing facility, so you have to type a tab or
783space(s) for each indented line. In practice you will prepare more
784complicated input for Python with a text editor; most text editors have
785an auto-indent facility. When a compound statement is entered
786interactively, it must be followed by a blank line to indicate
787completion (since the parser cannot guess when you have typed the last
788line).
789
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000790\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000791The {\tt print} statement writes the value of the expression(s) it is
792given. It differs from just writing the expression you want to write
793(as we did earlier in the calculator examples) in the way it handles
794multiple expressions and strings. Strings are written without quotes,
795and a space is inserted between items, so you can format things nicely,
796like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000797
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000798\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000799>>> i = 256*256
800>>> print 'The value of i is', i
801The value of i is 65536
802>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000803\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000804%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000805A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000806
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000807\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000808>>> a, b = 0, 1
809>>> while b < 1000:
810... print b,
811... a, b = b, a+b
812...
8131 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
814>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000815\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000816%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000817Note that the interpreter inserts a newline before it prints the next
818prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000819
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000820\end{itemize}
821
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000822
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000823\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000824
Guido van Rossum4410c751991-06-04 20:22:18 +0000825Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000826usual control flow statements known from other languages, with some
827twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000828
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000829\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000830
831Perhaps the most well-known statement type is the {\tt if} statement.
832For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000833
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000834\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000835>>> if x < 0:
836... x = 0
837... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000838... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000839... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000840... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000841... print 'Single'
842... else:
843... print 'More'
844...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000845\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000846%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000847There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000848optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
849useful to avoid excessive indentation. An {\tt if...elif...elif...}
850sequence is a substitute for the {\em switch} or {\em case} statements
851found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000852
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000853\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000854
Guido van Rossum4410c751991-06-04 20:22:18 +0000855The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000856used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000857arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000858completely free in the iteration test and step (as C), Python's {\tt
859for} statement iterates over the items of any sequence (e.g., a list
860or a string), in the order that they appear in the sequence. For
861example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000862
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000863\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000864>>> # Measure some strings:
865>>> a = ['cat', 'window', 'defenestrate']
866>>> for x in a:
867... print x, len(x)
868...
869cat 3
870window 6
871defenestrate 12
872>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000873\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000874%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000875It is not safe to modify the sequence being iterated over in the loop
876(this can only happen for mutable sequence types, i.e., lists). If
877you need to modify the list you are iterating over, e.g., duplicate
878selected items, you must iterate over a copy. The slice notation
879makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000880
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000881\bcode\begin{verbatim}
882>>> for x in a[:]: # make a slice copy of the entire list
883... if len(x) > 6: a.insert(0, x)
884...
885>>> a
886['defenestrate', 'cat', 'window', 'defenestrate']
887>>>
888\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000889
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000890\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000891
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000892If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000893function {\tt range()} comes in handy. It generates lists containing
894arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000895
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000896\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000897>>> range(10)
898[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
899>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000900\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000901%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000902The given end point is never part of the generated list; {\tt range(10)}
903generates a list of 10 values, exactly the legal indices for items of a
904sequence of length 10. It is possible to let the range start at another
905number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000906
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000907\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000908>>> range(5, 10)
909[5, 6, 7, 8, 9]
910>>> range(0, 10, 3)
911[0, 3, 6, 9]
912>>> range(-10, -100, -30)
913[-10, -40, -70]
914>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000915\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000916%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000917To iterate over the indices of a sequence, combine {\tt range()} and
918{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000919
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000920\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000921>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000922>>> for i in range(len(a)):
923... print i, a[i]
924...
9250 Mary
9261 had
9272 a
9283 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009294 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000930>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000931\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000932
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000933\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000934
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000935The {\tt break} statement, like in C, breaks out of the smallest
936enclosing {\tt for} or {\tt while} loop.
937
938The {\tt continue} statement, also borrowed from C, continues with the
939next iteration of the loop.
940
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000941Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000942loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000943the condition becomes false (with {\tt while}), but not when the loop is
944terminated by a {\tt break} statement. This is exemplified by the
945following loop, which searches for a list item of value 0:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000946
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000947\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000948>>> for n in range(2, 10):
949... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000950... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000951... print n, 'equals', x, '*', n/x
952... break
953... else:
954... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000955...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009562 is a prime number
9573 is a prime number
9584 equals 2 * 2
9595 is a prime number
9606 equals 2 * 3
9617 is a prime number
9628 equals 2 * 4
9639 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000964>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000965\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000966
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000967\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000968
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000969The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000970It can be used when a statement is required syntactically but the
971program requires no action.
972For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000973
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000974\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000975>>> while 1:
976... pass # Busy-wait for keyboard interrupt
977...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000978\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000979
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000980\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000981
982We can create a function that writes the Fibonacci series to an
983arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000984
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000985\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000986>>> def fib(n): # write Fibonacci series up to n
987... a, b = 0, 1
988... while b <= n:
989... print b,
990... a, b = b, a+b
991...
992>>> # Now call the function we just defined:
993>>> fib(2000)
9941 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
995>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000996\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000997%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000998The keyword {\tt def} introduces a function {\em definition}. It must
999be followed by the function name and the parenthesized list of formal
1000parameters. The statements that form the body of the function starts at
1001the next line, indented by a tab stop.
1002
1003The {\em execution} of a function introduces a new symbol table used
1004for the local variables of the function. More precisely, all variable
1005assignments in a function store the value in the local symbol table;
1006whereas
1007 variable references first look in the local symbol table, then
1008in the global symbol table, and then in the table of built-in names.
1009Thus,
1010global variables cannot be directly assigned to from within a
1011function, although they may be referenced.
1012
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001013The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001014the local symbol table of the called function when it is called; thus,
1015arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001016\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001017 Actually, {\em call by object reference} would be a better
1018 description, since if a mutable object is passed, the caller
1019 will see any changes the callee makes to it (e.g., items
1020 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001021}
1022When a function calls another function, a new local symbol table is
1023created for that call.
1024
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001025A function definition introduces the function name in the
1026current
1027symbol table. The value
1028of the function name
1029has a type that is recognized by the interpreter as a user-defined
1030function. This value can be assigned to another name which can then
1031also be used as a function. This serves as a general renaming
1032mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001033
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001034\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001035>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001036<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001037>>> f = fib
1038>>> f(100)
10391 1 2 3 5 8 13 21 34 55 89
1040>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001041\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001042%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001043You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001044Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001045value. In fact, technically speaking, procedures do return a value,
1046albeit a rather boring one. This value is called {\tt None} (it's a
1047built-in name). Writing the value {\tt None} is normally suppressed by
1048the interpreter if it would be the only value written. You can see it
1049if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001050
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001051\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001052>>> print fib(0)
1053None
1054>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001055\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001056%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001057It is simple to write a function that returns a list of the numbers of
1058the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001059
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001060\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001061>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001062... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001063... a, b = 0, 1
1064... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001065... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001066... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001067... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001068...
1069>>> f100 = fib2(100) # call it
1070>>> f100 # write the result
1071[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1072>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001073\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001074%
Guido van Rossum4410c751991-06-04 20:22:18 +00001075This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001076
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001077\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001078
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001079\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001080The {\tt return} statement returns with a value from a function. {\tt
1081return} without an expression argument is used to return from the middle
1082of a procedure (falling off the end also returns from a proceduce), in
1083which case the {\tt None} value is returned.
1084
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001085\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001086The statement {\tt result.append(b)} calls a {\em method} of the list
1087object {\tt result}. A method is a function that `belongs' to an
1088object and is named {\tt obj.methodname}, where {\tt obj} is some
1089object (this may be an expression), and {\tt methodname} is the name
1090of a method that is defined by the object's type. Different types
1091define different methods. Methods of different types may have the
1092same name without causing ambiguity. (It is possible to define your
1093own object types and methods, using {\em classes}. This is an
1094advanced feature that is not discussed in this tutorial.)
1095The method {\tt append} shown in the example, is defined for
1096list objects; it adds a new element at the end of the list. In this
1097example
1098it is equivalent to {\tt result = result + [b]}, but more efficient.
1099
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001100\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001101
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001102
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001103\chapter{Odds and Ends}
1104
1105This chapter describes some things you've learned about already in
1106more detail, and adds some new things as well.
1107
1108\section{More on Lists}
1109
1110The list data type has some more methods. Here are all of the methods
1111of lists objects:
1112
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001113\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001114
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001115\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001116Insert an item at a given position. The first argument is the index of
1117the element before which to insert, so {\tt a.insert(0, x)} inserts at
1118the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1119{\tt a.append(x)}.
1120
1121\item[{\tt append(x)}]
1122Equivalent to {\tt a.insert(len(a), x)}.
1123
1124\item[{\tt index(x)}]
1125Return the index in the list of the first item whose value is {\tt x}.
1126It is an error if there is no such item.
1127
1128\item[{\tt remove(x)}]
1129Remove the first item from the list whose value is {\tt x}.
1130It is an error if there is no such item.
1131
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001132\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001133Sort the items of the list, in place.
1134
1135\item[{\tt reverse()}]
1136Reverse the elements of the list, in place.
1137
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001138\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001139
1140An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001141
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001142\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001143>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001144>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001145>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001146>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001147[66.6, 333, -1, 333, 1, 1234.5, 333]
1148>>> a.index(333)
11491
1150>>> a.remove(333)
1151>>> a
1152[66.6, -1, 333, 1, 1234.5, 333]
1153>>> a.reverse()
1154>>> a
1155[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001156>>> a.sort()
1157>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001158[-1, 1, 66.6, 333, 333, 1234.5]
1159>>>
1160\end{verbatim}\ecode
1161
1162\section{The {\tt del} statement}
1163
1164There is a way to remove an item from a list given its index instead
1165of its value: the {\tt del} statement. This can also be used to
1166remove slices from a list (which we did earlier by assignment of an
1167empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001168
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001169\bcode\begin{verbatim}
1170>>> a
1171[-1, 1, 66.6, 333, 333, 1234.5]
1172>>> del a[0]
1173>>> a
1174[1, 66.6, 333, 333, 1234.5]
1175>>> del a[2:4]
1176>>> a
1177[1, 66.6, 1234.5]
1178>>>
1179\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001180%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001181{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001182
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001183\bcode\begin{verbatim}
1184>>> del a
1185>>>
1186\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001187%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001188Referencing the name {\tt a} hereafter is an error (at least until
1189another value is assigned to it). We'll find other uses for {\tt del}
1190later.
1191
1192\section{Tuples and Sequences}
1193
1194We saw that lists and strings have many common properties, e.g.,
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001195indexinging and slicing operations. They are two examples of {\em
1196sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001197sequence data types may be added. There is also another standard
1198sequence data type: the {\em tuple}.
1199
1200A tuple consists of a number of values separated by commas, for
1201instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001202
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001203\bcode\begin{verbatim}
1204>>> t = 12345, 54321, 'hello!'
1205>>> t[0]
120612345
1207>>> t
1208(12345, 54321, 'hello!')
1209>>> # Tuples may be nested:
1210>>> u = t, (1, 2, 3, 4, 5)
1211>>> u
1212((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1213>>>
1214\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001215%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001216As you see, on output tuples are alway enclosed in parentheses, so
1217that nested tuples are interpreted correctly; they may be input with
1218or without surrounding parentheses, although often parentheses are
1219necessary anyway (if the tuple is part of a larger expression).
1220
1221Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1222from a database, etc. Tuples, like strings, are immutable: it is not
1223possible to assign to the individual items of a tuple (you can
1224simulate much of the same effect with slicing and concatenation,
1225though).
1226
1227A special problem is the construction of tuples containing 0 or 1
1228items: the syntax has some extra quirks to accomodate these. Empty
1229tuples are constructed by an empty pair of parentheses; a tuple with
1230one item is constructed by following a value with a comma
1231(it is not sufficient to enclose a single value in parentheses).
1232Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001233
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001234\bcode\begin{verbatim}
1235>>> empty = ()
1236>>> singleton = 'hello', # <-- note trailing comma
1237>>> len(empty)
12380
1239>>> len(singleton)
12401
1241>>> singleton
1242('hello',)
1243>>>
1244\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001245%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001246The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1247tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1248are packed together in a tuple. The reverse operation is also
1249possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001250
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001251\bcode\begin{verbatim}
1252>>> x, y, z = t
1253>>>
1254\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001255%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001256This is called, appropriately enough, {\em tuple unpacking}. Tuple
1257unpacking requires that the list of variables on the left has the same
1258number of elements as the length of the tuple. Note that multiple
1259assignment is really just a combination of tuple packing and tuple
1260unpacking!
1261
1262Occasionally, the corresponding operation on lists is useful: {\em list
1263unpacking}. This is supported by enclosing the list of variables in
1264square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001265
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001266\bcode\begin{verbatim}
1267>>> a = ['foo', 'bar', 100, 1234]
1268>>> [a1, a2, a3, a4] = a
1269>>>
1270\end{verbatim}\ecode
1271
1272\section{Dictionaries}
1273
1274Another useful data type built into Python is the {\em dictionary}.
1275Dictionaries are sometimes found in other languages as ``associative
1276memories'' or ``associative arrays''. Unlike sequences, which are
1277indexed by a range of numbers, dictionaries are indexed by {\em keys},
1278which are strings. It is best to think of a dictionary as an unordered set of
1279{\em key:value} pairs, with the requirement that the keys are unique
1280(within one dictionary).
1281A pair of braces creates an empty dictionary: \verb/{}/.
1282Placing a comma-separated list of key:value pairs within the
1283braces adds initial key:value pairs to the dictionary; this is also the
1284way dictionaries are written on output.
1285
1286The main operations on a dictionary are storing a value with some key
1287and extracting the value given the key. It is also possible to delete
1288a key:value pair
1289with {\tt del}.
1290If you store using a key that is already in use, the old value
1291associated with that key is forgotten. It is an error to extract a
1292value using a non-existant key.
1293
1294The {\tt keys()} method of a dictionary object returns a list of all the
1295keys used in the dictionary, in random order (if you want it sorted,
1296just apply the {\tt sort()} method to the list of keys). To check
1297whether a single key is in the dictionary, use the \verb/has_key()/
1298method of the dictionary.
1299
1300Here is a small example using a dictionary:
1301
1302\bcode\begin{verbatim}
1303>>> tel = {'jack': 4098, 'sape': 4139}
1304>>> tel['guido'] = 4127
1305>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001306{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001307>>> tel['jack']
13084098
1309>>> del tel['sape']
1310>>> tel['irv'] = 4127
1311>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001312{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001313>>> tel.keys()
1314['guido', 'irv', 'jack']
1315>>> tel.has_key('guido')
13161
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001317>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001318\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001319
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001320\section{More on Conditions}
1321
1322The conditions used in {\tt while} and {\tt if} statements above can
1323contain other operators besides comparisons.
1324
1325The comparison operators {\tt in} and {\tt not in} check whether a value
1326occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1327is not} compare whether two objects are really the same object; this
1328only matters for mutable objects like lists. All comparison operators
1329have the same priority, which is lower than that of all numerical
1330operators.
1331
1332Comparisons can be chained: e.g., {\tt a < b = c} tests whether {\tt a}
1333is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1334
1335Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1336or}, and the outcome of a comparison (or of any other Boolean
1337expression) may be negated with {\tt not}. These all have lower
1338priorities than comparison operators again; between them, {\tt not} has
1339the highest priority, and {\tt or} the lowest, so that
1340{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1341course, parentheses can be used to express the desired composition.
1342
1343The Boolean operators {\tt and} and {\tt or} are so-called {\em
1344shortcut} operators: their arguments are evaluated from left to right,
1345and evaluation stops as soon as the outcome is determined. E.g., if
1346{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1347does not evaluate the expression C. In general, the return value of a
1348shortcut operator, when used as a general value and not as a Boolean, is
1349the last evaluated argument.
1350
1351It is possible to assign the result of a comparison or other Boolean
1352expression to a variable, but you must enclose the entire Boolean
1353expression in parentheses. This is necessary because otherwise an
1354assignment like \verb/a = b = c/ would be ambiguous: does it assign the
1355value of {\tt c} to {\tt a} and {\tt b}, or does it compare {\tt b} to
1356{\tt c} and assign the outcome (0 or 1) to {\tt a}? As it is, the first
1357meaning is what you get, and to get the latter you have to write
1358\verb/a = (b = c)/. (In Python, unlike C, assignment cannot occur
1359inside expressions.)
1360
1361\section{Comparing Sequences and Other Types}
1362
1363Sequence objects may be compared to other objects with the same
1364sequence type. The comparison uses {\em lexicographical} ordering:
1365first the first two items are compared, and if they differ this
1366determines the outcome of the comparison; if they are equal, the next
1367two items are compared, and so on, until either sequence is exhausted.
1368If two items to be compared are themselves sequences of the same type,
1369the lexiographical comparison is carried out recursively. If all
1370items of two sequences compare equal, the sequences are considered
1371equal. If one sequence is an initial subsequence of the other, the
1372shorted sequence is the smaller one. Lexicographical ordering for
1373strings uses the ASCII ordering for individual characters. Some
1374examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001375
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001376\bcode\begin{verbatim}
1377(1, 2, 3) < (1, 2, 4)
1378[1, 2, 3] < [1, 2, 4]
1379'ABC' < 'C' < 'Pascal' < 'Python'
1380(1, 2, 3, 4) < (1, 2, 4)
1381(1, 2) < (1, 2, -1)
1382(1, 2, 3) = (1.0, 2.0, 3.0)
1383(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1384\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001385%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001386Note that comparing objects of different types is legal. The outcome
1387is deterministic but arbitrary: the types are ordered by their name.
1388Thus, a list is always smaller than a string, a string is always
1389smaller than a tuple, etc. Mixed numeric types are compared according
1390to their numeric value, so 0 equals 0.0, etc.%
1391\footnote{
1392 The rules for comparing objects of different types should
1393 not be relied upon; they may change in a future version of
1394 the language.
1395}
1396
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001397
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001398\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001399
Guido van Rossum4410c751991-06-04 20:22:18 +00001400If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001401definitions you have made (functions and variables) are lost.
1402Therefore, if you want to write a somewhat longer program, you are
1403better off using a text editor to prepare the input for the interpreter
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001404and run it with that file as input instead. This is known as creating a
1405{\em script}. As your program gets longer, you may want to split it
1406into several files for easier maintenance. You may also want to use a
1407handy function that you've written in several programs without copying
1408its definition into each program.
1409
Guido van Rossum4410c751991-06-04 20:22:18 +00001410To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001411them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001412Such a file is called a {\em module}; definitions from a module can be
1413{\em imported} into other modules or into the {\em main} module (the
1414collection of variables that you have access to in a script
1415executed at the top level
1416and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001417
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001418A module is a file containing Python definitions and statements. The
1419file name is the module name with the suffix {\tt .py} appended. For
1420instance, use your favorite text editor to create a file called {\tt
1421fibo.py} in the current directory with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001422
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001423\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001424# Fibonacci numbers module
1425
1426def fib(n): # write Fibonacci series up to n
1427 a, b = 0, 1
1428 while b <= n:
1429 print b,
1430 a, b = b, a+b
1431
1432def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001433 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001434 a, b = 0, 1
1435 while b <= n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001436 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001437 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001438 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001439\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001440%
Guido van Rossum4410c751991-06-04 20:22:18 +00001441Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001442following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001443
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001444\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001445>>> import fibo
1446>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001447\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001448%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001449This does not enter the names of the functions defined in
1450{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001451directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001452{\tt fibo}
1453there.
1454Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001455
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001456\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001457>>> fibo.fib(1000)
14581 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1459>>> fibo.fib2(100)
1460[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1461>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001462\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001463%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001464If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001465
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001466\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001467>>> fib = fibo.fib
1468>>> fib(500)
14691 1 2 3 5 8 13 21 34 55 89 144 233 377
1470>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001471\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001472
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001473\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001474
1475A module can contain executable statements as well as function
1476definitions.
1477These statements are intended to initialize the module.
1478They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001479{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001480time the module is imported somewhere.%
1481\footnote{
1482 In fact function definitions are also `statements' that are
1483 `executed'; the execution enters the function name in the
1484 module's global symbol table.
1485}
1486
1487Each module has its own private symbol table, which is used as the
1488global symbol table by all functions defined in the module.
1489Thus, the author of a module can use global variables in the module
1490without worrying about accidental clashes with a user's global
1491variables.
1492On the other hand, if you know what you are doing you can touch a
1493module's global variables with the same notation used to refer to its
1494functions,
1495{\tt modname.itemname}.
1496
1497Modules can import other modules.
1498It is customary but not required to place all
1499{\tt import}
1500statements at the beginning of a module (or script, for that matter).
1501The imported module names are placed in the importing module's global
1502symbol table.
1503
1504There is a variant of the
1505{\tt import}
1506statement that imports names from a module directly into the importing
1507module's symbol table.
1508For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001509
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001510\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001511>>> from fibo import fib, fib2
1512>>> fib(500)
15131 1 2 3 5 8 13 21 34 55 89 144 233 377
1514>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001515\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001516%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001517This does not introduce the module name from which the imports are taken
1518in the local symbol table (so in the example, {\tt fibo} is not
1519defined).
1520
1521There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001522
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001523\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001524>>> from fibo import *
1525>>> fib(500)
15261 1 2 3 5 8 13 21 34 55 89 144 233 377
1527>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001528\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001529%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001530This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001531({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001532
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001533\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001534
Guido van Rossum4410c751991-06-04 20:22:18 +00001535Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001536document (Python Library Reference). Some modules are built into the
1537interpreter; these provide access to operations that are not part of the
1538core of the language but are nevertheless built in, either for
1539efficiency or to provide access to operating system primitives such as
1540system calls. The set of such modules is a configuration option; e.g.,
1541the {\tt amoeba} module is only provided on systems that somehow support
1542Amoeba primitives. One particular module deserves some attention: {\tt
1543sys}, which is built into every Python interpreter. The variables {\tt
1544sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1545secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001546
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001547\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001548>>> import sys
1549>>> sys.ps1
1550'>>> '
1551>>> sys.ps2
1552'... '
1553>>> sys.ps1 = 'C> '
1554C> print 'Yuck!'
1555Yuck!
1556C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001557\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001558%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001559These two variables are only defined if the interpreter is in
1560interactive mode.
1561
1562The variable
1563{\tt sys.path}
1564is a list of strings that determine the interpreter's search path for
1565modules.
1566It is initialized to a default path taken from the environment variable
1567{\tt PYTHONPATH},
1568or from a built-in default if
1569{\tt PYTHONPATH}
1570is not set.
1571You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001572
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001573\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001574>>> import sys
1575>>> sys.path.append('/ufs/guido/lib/python')
1576>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001577\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001578
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001579\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001580
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001581The built-in function {\tt dir} is used to find out which names a module
1582defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001583
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001584\bcode\begin{verbatim}
1585>>> import fibo, sys
1586>>> dir(fibo)
1587['fib', 'fib2']
1588>>> dir(sys)
1589['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout']
1590>>>
1591\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001592%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001593Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001594
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001595\bcode\begin{verbatim}
1596>>> a = [1, 2, 3, 4, 5]
1597>>> import fibo, sys
1598>>> fib = fibo.fib
1599>>> dir()
1600['a', 'fib', 'fibo', 'sys']
1601>>>
1602\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001603%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001604Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001605
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001606{\tt dir()} does not list the names of built-in functions and variables.
1607If you want a list of those, they are defined in the standard module
1608{\tt builtin}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001609
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001610\bcode\begin{verbatim}
1611>>> import builtin
1612>>> dir(builtin)
1613['EOFError', 'KeyboardInterrupt', 'MemoryError', 'NameError', 'None', 'Runti
1614meError', 'SystemError', 'TypeError', 'abs', 'chr', 'dir', 'divmod', 'eval',
1615 'exec', 'float', 'input', 'int', 'len', 'long', 'max', 'min', 'open', 'ord'
1616, 'pow', 'range', 'raw_input', 'reload', 'type']
1617>>>
1618\end{verbatim}\ecode
1619
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001620
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001621\chapter{Output Formatting}
1622
1623So far we've encountered two ways of writing values: {\em expression
1624statements} and the {\tt print} statement. (A third way is using the
1625{\tt write} method of file objects; the standard output file can be
1626referenced as {\tt sys.stdout}. See the Library Reference for more
1627information on this.)
1628
1629Often you'll want more control over the formatting of your output than
1630simply printing space-separated values. The key to nice formatting in
1631Python is to do all the string handling yourself; using string slicing
1632and concatenation operations you can create any lay-out you can imagine.
1633The standard module {\tt string} contains some useful operations for
1634padding strings to a given column width; these will be discussed shortly.
1635
1636One question remains, of course: how do you convert values to strings?
1637Luckily, Python has a way to convert any value to a string: just write
1638the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001639
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001640\bcode\begin{verbatim}
1641>>> x = 10 * 3.14
1642>>> y = 200*200
1643>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1644>>> print s
1645The value of x is 31.4, and y is 40000...
1646>>> # Reverse quotes work on other types besides numbers:
1647>>> p = [x, y]
1648>>> ps = `p`
1649>>> ps
1650'[31.4, 40000]'
1651>>> # Converting a string adds string quotes and backslashes:
1652>>> hello = 'hello, world\n'
1653>>> hellos = `hello`
1654>>> print hellos
1655'hello, world\012'
1656>>> # The argument of reverse quotes may be a tuple:
1657>>> `x, y, ('foo', 'bar')`
1658'(31.4, 40000, (\'foo\', \'bar\'))'
1659>>>
1660\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001661%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001662Here is how you write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001663
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001664\bcode\begin{verbatim}
1665>>> import string
1666>>> for x in range(1, 11):
1667... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1668... # Note trailing comma on previous line
1669... print string.rjust(`x*x*x`, 4)
1670...
1671 1 1 1
1672 2 4 8
1673 3 9 27
1674 4 16 64
1675 5 25 125
1676 6 36 216
1677 7 49 343
1678 8 64 512
1679 9 81 729
168010 100 1000
1681>>>
1682\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001683%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001684(Note that one space between each column was added by the way {\tt print}
1685works: it always adds spaces between its arguments.)
1686
1687This example demonstrates the function {\tt string.rjust()}, which
1688right-justifies a string in a field of a given width by padding it with
1689spaces on the left. There are similar functions {\tt string.ljust()}
1690and {\tt string.center()}. These functions do not write anything, they
1691just return a new string. If the input string is too long, they don't
1692truncate it, but return it unchanged; this will mess up your column
1693lay-out but that's usually better than the alternative, which would be
1694lying about a value. (If you really want truncation you can always add
1695a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1696
1697There is another function, {\tt string.zfill}, which pads a numeric
1698string on the left with zeros. It understands about plus and minus
1699signs:%
1700\footnote{
1701 Better facilities for formatting floating point numbers are
1702 lacking at this moment.
1703}
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001704
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001705\bcode\begin{verbatim}
1706>>> string.zfill('12', 5)
1707'00012'
1708>>> string.zfill('-3.14', 7)
1709'-003.14'
1710>>> string.zfill('3.14159265359', 5)
1711'3.14159265359'
1712>>>
1713\end{verbatim}\ecode
1714
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001715
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001716\chapter{Errors and Exceptions}
1717
1718Until now error messages haven't been more than mentioned, but if you
1719have tried out the examples you have probably seen some. There are
1720(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1721and {\em exceptions}.
1722
1723\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001724
1725Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001726kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001727
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001728\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001729>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001730Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001731while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001732 ^
1733Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001734>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001735\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001736%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001737The parser repeats the offending line and displays a little `arrow'
1738pointing at the earliest point in the line where the error was detected.
1739The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001740{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001741the arrow: in the example, the error is detected at the keyword
1742{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001743File name and line number are printed so you know where to look in case
1744the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001745
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001746\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001747
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001748Even if a statement or expression is syntactically correct, it may
1749cause an error when an attempt is made to execute it.
1750Errors detected during execution are called {\em exceptions} and are
1751not unconditionally fatal: you will soon learn how to handle them in
1752Python programs. Most exceptions are not handled by programs,
1753however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001754
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001755\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001756>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001757Stack backtrace (innermost last):
1758 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001759ZeroDivisionError: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001760>>> 4 + foo*3
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001761Stack backtrace (innermost last):
1762 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001763NameError: foo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001764>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001765Stack backtrace (innermost last):
1766 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001767TypeError: illegal argument type for built-in operation
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001768>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001769\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001770%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001771The last line of the error message indicates what happened.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001772Exceptions come in different types, and the type is printed as part of
1773the message: the types in the example are
Guido van Rossumb2c65561993-05-12 08:53:36 +00001774{\tt ZeroDivisionError},
1775{\tt NameError}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001776and
Guido van Rossumb2c65561993-05-12 08:53:36 +00001777{\tt TypeError}.
1778The string printed as the exception type is the name of the built-in
1779name for the exception that occurred. This is true for all built-in
1780exceptions, but need not be true for user-defined exceptions (although
1781it is a useful convention).
1782Standard exception names are built-in identifiers (not reserved
1783keywords).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001784
Guido van Rossumb2c65561993-05-12 08:53:36 +00001785The rest of the line is a detail whose interpretation depends on the
1786exception type; its meaning is dependent on the exception type.
1787
1788The preceding part of the error message shows the context where the
1789exception happened, in the form of a stack backtrace.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001790In general it contains a stack backtrace listing source lines; however,
1791it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001792
Guido van Rossumb2c65561993-05-12 08:53:36 +00001793The Python library reference manual lists the built-in exceptions and
1794their meanings.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001795
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001796\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001797
1798It is possible to write programs that handle selected exceptions.
1799Look at the following example, which prints a table of inverses of
1800some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001801
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001802\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001803>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001804>>> for x in numbers:
1805... print x,
1806... try:
1807... print 1.0 / x
Guido van Rossumb2c65561993-05-12 08:53:36 +00001808... except ZeroDivisionError:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001809... print '*** has no inverse ***'
1810...
18110.3333 3.00030003
18122.5 0.4
18130 *** has no inverse ***
181410 0.1
1815>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001816\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001817%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001818The {\tt try} statement works as follows.
1819\begin{itemize}
1820\item
1821First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001822{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001823(the statement(s) between the {\tt try} and {\tt except} keywords) is
1824executed.
1825\item
1826If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001827{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001828is skipped and execution of the {\tt try} statement is finished.
1829\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001830If an exception occurs during execution of the try clause,
1831the rest of the clause is skipped. Then if
1832its type matches the exception named after the {\tt except} keyword,
1833the rest of the try clause is skipped, the except clause is executed,
1834and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001835\item
1836If an exception occurs which does not match the exception named in the
1837except clause, it is passed on to outer try statements; if no handler is
1838found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001839{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001840and execution stops with a message as shown above.
1841\end{itemize}
1842A {\tt try} statement may have more than one except clause, to specify
1843handlers for different exceptions.
1844At most one handler will be executed.
1845Handlers only handle exceptions that occur in the corresponding try
1846clause, not in other handlers of the same {\tt try} statement.
1847An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001848e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001849
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001850\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001851... except (RuntimeError, TypeError, NameError):
1852... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001853\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001854%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001855The last except clause may omit the exception name(s), to serve as a
1856wildcard.
Guido van Rossumb2c65561993-05-12 08:53:36 +00001857Use this with extreme caution, since it is easy to mask a real
1858programming error in this way!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001859
1860When an exception occurs, it may have an associated value, also known as
1861the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001862{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001863The presence and type of the argument depend on the exception type.
1864For exception types which have an argument, the except clause may
1865specify a variable after the exception name (or list) to receive the
1866argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001867
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001868\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001869>>> try:
1870... foo()
1871... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001872... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001873...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001874name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001875>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001876\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001877%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001878If an exception has an argument, it is printed as the last part
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001879(`detail') of the message for unhandled exceptions.
1880
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001881Exception handlers don't just handle exceptions if they occur
1882immediately in the try clause, but also if they occur inside functions
1883that are called (even indirectly) in the try clause.
1884For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001885
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001886\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001887>>> def this_fails():
1888... x = 1/0
1889...
1890>>> try:
1891... this_fails()
Guido van Rossumb2c65561993-05-12 08:53:36 +00001892... except ZeroDivisionError, detail:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001893... print 'Handling run-time error:', detail
1894...
Guido van Rossumb2c65561993-05-12 08:53:36 +00001895Handling run-time error: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001896>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001897\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001898
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001899\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001900
1901The {\tt raise} statement allows the programmer to force a specified
1902exception to occur.
1903For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001904
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001905\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001906>>> raise NameError, 'HiThere'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001907Stack backtrace (innermost last):
1908 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001909NameError: HiThere
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001910>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001911\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001912%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001913The first argument to {\tt raise} names the exception to be raised.
1914The optional second argument specifies the exception's argument.
1915
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001916\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001917
1918Programs may name their own exceptions by assigning a string to a
1919variable.
1920For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001921
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001922\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001923>>> my_exc = 'my_exc'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001924>>> try:
1925... raise my_exc, 2*2
1926... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001927... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001928...
1929My exception occured, value: 4
1930>>> raise my_exc, 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001931Stack backtrace (innermost last):
1932 File "<stdin>", line 7
Guido van Rossumb2c65561993-05-12 08:53:36 +00001933my_exc: 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001934>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001935\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001936%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001937Many standard modules use this to report errors that may occur in
1938functions they define.
1939
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001940\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001941
1942The {\tt try} statement has another optional clause which is intended to
1943define clean-up actions that must be executed under all circumstances.
1944For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001945
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001946\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001947>>> try:
1948... raise KeyboardInterrupt
1949... finally:
1950... print 'Goodbye, world!'
1951...
1952Goodbye, world!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001953Stack backtrace (innermost last):
1954 File "<stdin>", line 2
Guido van Rossumb2c65561993-05-12 08:53:36 +00001955KeyboardInterrupt
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001956>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001957\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001958%
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001959A {\tt finally} clause is executed whether or not an exception has
1960occurred in the {\tt try} clause. When an exception has occurred, it
1961is re-raised after the {\tt finally} clauses is executed. The
1962{\tt finally} clause is also executed ``on the way out'' when the
1963{\tt try} statement is left via a {\tt break} or {\tt return}
1964statement.
1965
1966A {\tt try} statement must either have one or more {\tt except}
1967clauses or one {\tt finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001968
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001969
1970\chapter{Classes}
1971
1972Python's class mechanism adds classes to the language with a minimum
1973of new syntax and semantics. It is a mixture of the class mechanisms
1974found in C++ and Modula-3. As is true for modules, classes in Python
1975do not put an absolute barrier between definition and user, but rather
1976rely on the politeness of the user not to ``break into the
1977definition.'' The most important features of classes are retained
1978with full power, however: the class inheritance mechanism allows
1979multiple base classes, a derived class can override any methods of its
1980base class(es), a method can call the method of a base class with the
1981same name. Objects can contain an arbitrary amount of private data.
1982
1983In C++ terminology, all class members (including the data members) are
1984{\em public}, and all member functions are {\em virtual}. There are
1985no special constructors or desctructors. As in Modula-3, there are no
1986shorthands for referencing the object's members from its methods: the
1987method function is declared with an explicit first argument
1988representing the object, which is provided implicitly by the call. As
1989in Smalltalk, classes themselves are objects, albeit in the wider
1990sense of the word: in Python, all data types are objects. This
1991provides semantics for importing and renaming. But, just like in C++
1992or Modula-3, built-in types cannot be used as base classes for
1993extension by the user. Also, like in Modula-3 but unlike in C++, the
1994built-in operators with special syntax (arithmetic operators,
1995subscriptong etc.) cannot be redefined for class members.
1996
1997
1998\section{A word about terminology}
1999
2000Lacking universally accepted terminology to talk about classes, I'll
2001make occasional use of Smalltalk and C++ terms. (I'd use Modula-3
2002terms, since its object-oriented semantics are closer to those of
2003Python than C++, but I expect that few readers have heard of it...)
2004
2005I also have to warn you that there's a terminological pitfall for
2006object-oriented readers: the word ``object'' in Python does not
2007necessarily mean a class instance. Like C++ and Modula-3, and unlike
2008Smalltalk, not all types in Python are classes: the basic built-in
2009types like integers and lists aren't, and even somewhat more exotic
2010types like files aren't. However, {\em all} Python types share a little
2011bit of common semantics that is best described by using the word
2012object.
2013
2014Objects have individuality, and multiple names (in multiple scopes)
2015can be bound to the same object. This is known as aliasing in other
2016languages. This is usually not appreciated on a first glance at
2017Python, and can be safely ignored when dealing with immutable basic
2018types (numbers, strings, tuples). However, aliasing has an
2019(intended!) effect on the semantics of Python code involving mutable
2020objects such as lists, dictionaries, and most types representing
2021entities outside the program (files, windows, etc.). This is usually
2022used to the benefit of the program, since aliases behave like pointers
2023in some respects. For example, passing an object is cheap since only
2024a pointer is passed by the implementation; and if a function modifies
2025an object passed as an argument, the caller will see the change --- this
2026obviates the need for two different argument passing mechanisms as in
2027Pascal.
2028
2029
2030\section{Python scopes and name spaces}
2031
2032Before introducing classes, I first have to tell you something about
2033Python's scope rules. Class definitions play some neat tricks with
2034name spaces, and you need to know how scopes and name spaces work to
2035fully understand what's going on. Incidentally, knowledge about this
2036subject is useful for any advanced Python programmer.
2037
2038Let's begin with some definitions.
2039
2040A {\em name space} is a mapping from names to objects. Most name
2041spaces are currently implemented as Python dictionaries, but that's
2042normally not noticeable in any way (except for performance), and it
2043may change in the future. Examples of name spaces are: the set of
2044built-in names (functions such as \verb\abs()\, and built-in exception
2045names); the global names in a module; and the local names in a
2046function invocation. In a sense the set of attributes of an object
2047also form a name space. The important things to know about name
2048spaces is that there is absolutely no relation between names in
2049different name spaces; for instance, two different modules may both
2050define a function ``maximize'' without confusion --- users of the
2051modules must prefix it with the module name.
2052
2053By the way, I use the word {\em attribute} for any name following a
2054dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2055an attribute of the object \verb\z\. Strictly speaking, references to
2056names in modules are attribute references: in the expression
2057\verb\modname.funcname\, \verb\modname\ is a module object and
2058\verb\funcname\ is an attribute of it. In this case there happens to
2059be a straightforward mapping between the module's attributes and the
2060global names defined in the module: they share the same name space!%
2061\footnote{
2062 Except for one thing. Module objects have a secret read-only
2063 attribute called {\tt __dict__} which returns the dictionary
2064 used to implement the module's name space; the name
2065 {\tt __dict__} is an attribute but not a global name.
2066 Obviously, using this violates the abstraction of name space
2067 implementation, and should be restricted to things like
2068 post-mortem debuggers...
2069}
2070
2071Attributes may be read-only or writable. In the latter case,
2072assignment to attributes is possible. Module attributes are writable:
2073you can write \verb\modname.the_answer = 42\. Writable attributes may
2074also be deleted with the del statement, e.g.
2075\verb\del modname.the_answer\.
2076
2077Name spaces are created at different moments and have different
2078lifetimes. The name space containing the built-in names is created
2079when the Python interpreter starts up, and is never deleted. The
2080global name space for a module is created when the module definition
2081is read in; normally, module name spaces also last until the
2082interpreter quits. The statements executed by the top-level
2083invocation of the interpreter, either read from a script file or
2084interactively, are considered part of a module called \verb\__main__\,
2085so they have their own global name space. (The built-in names
2086actually also live in a module; this is called \verb\builtin\,
2087although it should really have been called \verb\__builtin__\.)
2088
2089The local name space for a function is created when the function is
2090called, and deleted when the function returns or raises an exception
2091that is not handled within the function. (Actually, forgetting would
2092be a better way to describe what actually happens.) Of course,
2093recursive invocations each have their own local name space.
2094
2095A {\em scope} is a textual region of a Python program where a name space
2096is directly accessible. ``Directly accessible'' here means that an
2097unqualified reference to a name attempts to find the name in the name
2098space.
2099
2100Although scopes are determined statically, they are used dynamically.
2101At any time during execution, exactly three nested scopes are in use
2102(i.e., exactly three name spaces are directly accessible): the
2103innermost scope, which is searched first, contains the local names,
2104the middle scope, searched next, contains the current module's global
2105names, and the outermost scope (searched last) is the name space
2106containing built-in names.
2107
2108Usually, the local scope references the local names of the (textually)
2109current function. Outside functions, the the local scope references
2110the same name space as the global scope: the module's name space.
2111Class definitions place yet another name space in the local scope.
2112
2113It is important to realize that scopes are determined textually: the
2114global scope of a function defined in a module is that module's name
2115space, no matter from where or by what alias the function is called.
2116On the other hand, the actual search for names is done dynamically, at
2117run time --- however, the the language definition is evolving towards
2118static name resolution, at ``compile'' time, so don't rely on dynamic
2119name resolution! (In fact, local variables are already determined
2120statically.)
2121
2122A special quirk of Python is that assignments always go into the
2123innermost scope. Assignments do not copy data --- they just
2124bind names to objects. The same is true for deletions: the statement
2125\verb\del x\ removes the binding of x from the name space referenced by the
2126local scope. In fact, all operations that introduce new names use the
2127local scope: in particular, import statements and function definitions
2128bind the module or function name in the local scope. (The
2129\verb\global\ statement can be used to indicate that particular
2130variables live in the global scope.)
2131
2132
2133\section{A first look at classes}
2134
2135Classes introduce a little bit of new syntax, three new object types,
2136and some new semantics.
2137
2138
2139\subsection{Class definition syntax}
2140
2141The simplest form of class definition looks like this:
2142
2143\begin{verbatim}
2144 class ClassName:
2145 <statement-1>
2146 .
2147 .
2148 .
2149 <statement-N>
2150\end{verbatim}
2151
2152Class definitions, like function definitions (\verb\def\ statements)
2153must be executed before they have any effect. (You could conceivably
2154place a class definition in a branch of an \verb\if\ statement, or
2155inside a function.)
2156
2157In practice, the statements inside a class definition will usually be
2158function definitions, but other statements are allowed, and sometimes
2159useful --- we'll come back to this later. The function definitions
2160inside a class normally have a peculiar form of argument list,
2161dictated by the calling conventions for methods --- again, this is
2162explained later.
2163
2164When a class definition is entered, a new name space is created, and
2165used as the local scope --- thus, all assignments to local variables
2166go into this new name space. In particular, function definitions bind
2167the name of the new function here.
2168
2169When a class definition is left normally (via the end), a {\em class
2170object} is created. This is basically a wrapper around the contents
2171of the name space created by the class definition; we'll learn more
2172about class objects in the next section. The original local scope
2173(the one in effect just before the class definitions was entered) is
2174reinstated, and the class object is bound here to class name given in
2175the class definition header (ClassName in the example).
2176
2177
2178\subsection{Class objects}
2179
2180Class objects support two kinds of operations: attribute references
2181and instantiation.
2182
2183{\em Attribute references} use the standard syntax used for all
2184attribute references in Python: \verb\obj.name\. Valid attribute
2185names are all the names that were in the class's name space when the
2186class object was created. So, if the class definition looked like
2187this:
2188
2189\begin{verbatim}
2190 class MyClass:
2191 i = 12345
2192 def f(x):
2193 return 'hello world'
2194\end{verbatim}
2195
2196then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
2197references, returning an integer and a function object, respectively.
2198Class attributes can also be assigned to, so you can change the
2199value of \verb\MyClass.i\ by assignment.
2200
2201Class {\em instantiation} uses function notation. Just pretend that
2202the class object is a parameterless function that returns a new
2203instance of the class. For example, (assuming the above class):
2204
2205\begin{verbatim}
2206 x = MyClass()
2207\end{verbatim}
2208
2209creates a new {\em instance} of the class and assigns this object to
2210the local variable \verb\x\.
2211
2212
2213\subsection{Instance objects}
2214
2215Now what can we do with instance objects? The only operations
2216understood by instance objects are attribute references. There are
2217two kinds of valid attribute names.
2218
2219The first I'll call {\em data attributes}. These correspond to
2220``instance variables'' in Smalltalk, and to ``data members'' in C++.
2221Data attributes need not be declared; like local variables, they
2222spring into existence when they are first assigned to. For example,
2223if \verb\x\ in the instance of \verb\MyClass\ created above, the
2224following piece of code will print the value 16, without leaving a
2225trace:
2226
2227\begin{verbatim}
2228 x.counter = 1
2229 while x.counter < 10:
2230 x.counter = x.counter * 2
2231 print x.counter
2232 del x.counter
2233\end{verbatim}
2234
2235The second kind of attribute references understood by instance objects
2236are {\em methods}. A method is a function that ``belongs to'' an
2237object. (In Python, the term method is not unique to class instances:
2238other object types can have methods as well, e.g., list objects have
2239methods called append, insert, remove, sort, and so on. However,
2240below, we'll use the term method exclusively to mean methods of class
2241instance objects, unless explicitly stated otherwise.)
2242
2243Valid method names of an instance object depend on its class. By
2244definition, all attributes of a class that are (user-defined) function
2245objects define corresponding methods of its instances. So in our
2246example, \verb\x.f\ is a valid method reference, since
2247\verb\MyClass.f\ is a function, but \verb\x.i\ is not, since
2248\verb\MyClass.i\ is not. But \verb\x.f\ is not the
2249same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a
2250function object.
2251
2252
2253\subsection{Method objects}
2254
2255Usually, a method is called immediately, e.g.:
2256
2257\begin{verbatim}
2258 x.f()
2259\end{verbatim}
2260
2261In our example, this will return the string \verb\'hello world'\.
2262However, it is not necessary to call a method right away: \verb\x.f\
2263is a method object, and can be stored away and called at a later
2264moment, for example:
2265
2266\begin{verbatim}
2267 xf = x.f
2268 while 1:
2269 print xf()
2270\end{verbatim}
2271
2272will continue to print \verb\hello world\ until the end of time.
2273
2274What exactly happens when a method is called? You may have noticed
2275that \verb\x.f()\ was called without an argument above, even though
2276the function definition for \verb\f\ specified an argument. What
2277happened to the argument? Surely Python raises an exception when a
2278function that requires an argument is called without any --- even if
2279the argument isn't actually used...
2280
2281Actually, you may have guessed the answer: the special thing about
2282methods is that the object is passed as the first argument of the
2283function. In our example, the call \verb\x.f()\ is exactly equivalent
2284to \verb\MyClass.f(x)\. In general, calling a method with a list of
2285{\em n} arguments is equivalent to calling the corresponding function
2286with an argument list that is created by inserting the method's object
2287before the first argument.
2288
2289If you still don't understand how methods work, a look at the
2290implementation can perhaps clarify matters. When an instance
2291attribute is referenced that isn't a data attribute, its class is
2292searched. If the name denotes a valid class attribute that is a
2293function object, a method object is created by packing (pointers to)
2294the instance object and the function object just found together in an
2295abstract object: this is the method object. When the method object is
2296called with an argument list, it is unpacked again, a new argument
2297list is constructed from the instance object and the original argument
2298list, and the function object is called with this new argument list.
2299
2300
2301\section{Random remarks}
2302
2303
2304[These should perhaps be placed more carefully...]
2305
2306
2307Data attributes override method attributes with the same name; to
2308avoid accidental name conflicts, which may cause hard-to-find bugs in
2309large programs, it is wise to use some kind of convention that
2310minimizes the chance of conflicts, e.g., capitalize method names,
2311prefix data attribute names with a small unique string (perhaps just
2312an undescore), or use verbs for methods and nouns for data attributes.
2313
2314
2315Data attributes may be referenced by methods as well as by ordinary
2316users (``clients'') of an object. In other words, classes are not
2317usable to implement pure abstract data types. In fact, nothing in
2318Python makes it possible to enforce data hiding --- it is all based
2319upon convention. (On the other hand, the Python implementation,
2320written in C, can completely hide implementation details and control
2321access to an object if necessary; this can be used by extensions to
2322Python written in C.)
2323
2324
2325Clients should use data attributes with care --- clients may mess up
2326invariants maintained by the methods by stamping on their data
2327attributes. Note that clients may add data attributes of their own to
2328an instance object without affecting the validity of the methods, as
2329long as name conflicts are avoided --- again, a naming convention can
2330save a lot of headaches here.
2331
2332
2333There is no shorthand for referencing data attributes (or other
2334methods!) from within methods. I find that this actually increases
2335the readability of methods: there is no chance of confusing local
2336variables and instance variables when glancing through a method.
2337
2338
2339Conventionally, the first argument of methods is often called
2340\verb\self\. This is nothing more than a convention: the name
2341\verb\self\ has absolutely no special meaning to Python. (Note,
2342however, that by not following the convention your code may be less
2343readable by other Python programmers, and it is also conceivable that
2344a {\em class browser} program be written which relies upon such a
2345convention.)
2346
2347
2348Any function object that is a class attribute defines a method for
2349instances of that class. It is not necessary that the function
2350definition is textually enclosed in the class definition: assigning a
2351function object to a local variable in the class is also ok. For
2352example:
2353
2354\begin{verbatim}
2355 # Function defined outside the class
2356 def f1(self, x, y):
2357 return min(x, x+y)
2358
2359 class C:
2360 f = f1
2361 def g(self):
2362 return 'hello world'
2363 h = g
2364\end{verbatim}
2365
2366Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
2367\verb\C\ that refer to function objects, and consequently they are all
2368methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
2369to \verb\g\. Note that this practice usually only serves to confuse
2370the reader of a program.
2371
2372
2373Methods may call other methods by using method attributes of the
2374\verb\self\ argument, e.g.:
2375
2376\begin{verbatim}
2377 class Bag:
2378 def empty(self):
2379 self.data = []
2380 def add(self, x):
2381 self.data.append(x)
2382 def addtwice(self, x):
Guido van Rossum084b0b21992-08-14 09:19:56 +00002383 self.add(x)
2384 self.add(x)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002385\end{verbatim}
2386
2387
2388The instantiation operation (``calling'' a class object) creates an
2389empty object. Many classes like to create objects in a known initial
2390state. There is no special syntax to enforce this, but a convention
2391works almost as well: add a method named \verb\init\ to the class,
2392which initializes the instance (by assigning to some important data
2393attributes) and returns the instance itself. For example, class
2394\verb\Bag\ above could have the following method:
2395
2396\begin{verbatim}
2397 def init(self):
2398 self.empty()
2399 return self
2400\end{verbatim}
2401
2402The client can then create and initialize an instance in one
2403statement, as follows:
2404
2405\begin{verbatim}
2406 x = Bag().init()
2407\end{verbatim}
2408
2409Of course, the \verb\init\ method may have arguments for greater
2410flexibility.
2411
2412Warning: a common mistake is to forget the \verb\return self\ at the
2413end of an init method!
2414
2415
2416Methods may reference global names in the same way as ordinary
2417functions. The global scope associated with a method is the module
2418containing the class definition. (The class itself is never used as a
2419global scope!) While one rarely encounters a good reason for using
2420global data in a method, there are many legitimate uses of the global
2421scope: for one thing, functions and modules imported into the global
2422scope can be used by methods, as well as functions and classes defined
2423in it. Usually, the class containing the method is itself defined in
2424this global scope, and in the next section we'll find some good
2425reasons why a method would want to reference its own class!
2426
2427
2428\section{Inheritance}
2429
2430Of course, a language feature would not be worthy of the name ``class''
2431without supporting inheritance. The syntax for a derived class
2432definition looks as follows:
2433
2434\begin{verbatim}
2435 class DerivedClassName(BaseClassName):
2436 <statement-1>
2437 .
2438 .
2439 .
2440 <statement-N>
2441\end{verbatim}
2442
2443The name \verb\BaseClassName\ must be defined in a scope containing
2444the derived class definition. Instead of a base class name, an
2445expression is also allowed. This is useful when the base class is
2446defined in another module, e.g.,
2447
2448\begin{verbatim}
2449 class DerivedClassName(modname.BaseClassName):
2450\end{verbatim}
2451
2452Execution of a derived class definition proceeds the same as for a
2453base class. When the class object is constructed, the base class is
2454remembered. This is used for resolving attribute references: if a
2455requested attribute is not found in the class, it is searched in the
2456base class. This rule is applied recursively if the base class itself
2457is derived from some other class.
2458
2459There's nothing special about instantiation of derived classes:
2460\verb\DerivedClassName()\ creates a new instance of the class. Method
2461references are resolved as follows: the corresponding class attribute
2462is searched, descending down the chain of base classes if necessary,
2463and the method reference is valid if this yields a function object.
2464
2465Derived classes may override methods of their base classes. Because
2466methods have no special privileges when calling other methods of the
2467same object, a method of a base class that calls another method
2468defined in the same base class, may in fact end up calling a method of
2469a derived class that overrides it. (For C++ programmers: all methods
2470in Python are ``virtual functions''.)
2471
2472An overriding method in a derived class may in fact want to extend
2473rather than simply replace the base class method of the same name.
2474There is a simple way to call the base class method directly: just
2475call \verb\BaseClassName.methodname(self, arguments)\. This is
2476occasionally useful to clients as well. (Note that this only works if
2477the base class is defined or imported directly in the global scope.)
2478
2479
2480\subsection{Multiple inheritance}
2481
2482Poython supports a limited form of multiple inheritance as well. A
2483class definition with multiple base classes looks as follows:
2484
2485\begin{verbatim}
2486 class DerivedClassName(Base1, Base2, Base3):
2487 <statement-1>
2488 .
2489 .
2490 .
2491 <statement-N>
2492\end{verbatim}
2493
2494The only rule necessary to explain the semantics is the resolution
2495rule used for class attribute references. This is depth-first,
2496left-to-right. Thus, if an attribute is not found in
2497\verb\DerivedClassName\, it is searched in \verb\Base1\, then
2498(recursively) in the base classes of \verb\Base1\, and only if it is
2499not found there, it is searched in \verb\Base2\, and so on.
2500
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002501(To some people breadth first---searching \verb\Base2\ and
2502\verb\Base3\ before the base classes of \verb\Base1\---looks more
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002503natural. However, this would require you to know whether a particular
2504attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
2505one of its base classes before you can figure out the consequences of
2506a name conflict with an attribute of \verb\Base2\. The depth-first
2507rule makes no differences between direct and inherited attributes of
2508\verb\Base1\.)
2509
2510It is clear that indiscriminate use of multiple inheritance is a
2511maintenance nightmare, given the reliance in Python on conventions to
2512avoid accidental name conflicts. A well-known problem with multiple
2513inheritance is a class derived from two classes that happen to have a
2514common base class. While it is easy enough to figure out what happens
2515in this case (the instance will have a single copy of ``instance
2516variables'' or data attributes used by the common base class), it is
2517not clear that these semantics are in any way useful.
2518
2519
2520\section{Odds and ends}
2521
2522Sometimes it is useful to have a data type similar to the Pascal
2523``record'' or C ``struct'', bundling together a couple of named data
2524items. An empty class definition will do nicely, e.g.:
2525
2526\begin{verbatim}
2527 class Employee:
2528 pass
2529
2530 john = Employee() # Create an empty employee record
2531
2532 # Fill the fields of the record
2533 john.name = 'John Doe'
2534 john.dept = 'computer lab'
2535 john.salary = 1000
2536\end{verbatim}
2537
2538
2539A piece of Python code that expects a particular abstract data type
2540can often be passed a class that emulates the methods of that data
2541type instead. For instance, if you have a function that formats some
2542data from a file object, you can define a class with methods
2543\verb\read()\ and \verb\readline()\ that gets the data from a string
2544buffer instead, and pass it as an argument. (Unfortunately, this
2545technique has its limitations: a class can't define operations that
2546are accessed by special syntax such as sequence subscripting or
2547arithmetic operators, and assigning such a ``pseudo-file'' to
2548\verb\sys.stdin\ will not cause the interpreter to read further input
2549from it.)
2550
2551
2552Instance method objects have attributes, too: \verb\m.im_self\ is the
2553object of which the method is an instance, and \verb\m.im_func\ is the
2554function object corresponding to the method.
2555
2556
2557XXX Mention bw compat hacks.
2558
2559
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002560\end{document}