blob: 3aaeba9dd2894b95f50a99e034b1085f2357e466 [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
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001608{\tt __builtin__}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001609
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001610\bcode\begin{verbatim}
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001611>>> import __builtin__
1612>>> dir(__builtin__)
1613['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError', 'I
1614mportError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'MemoryError', '
1615NameError', 'None', 'OverflowError', 'RuntimeError', 'SyntaxError', 'SystemE
1616rror', 'SystemExit', 'TypeError', 'ValueError', 'ZeroDivisionError', 'abs',
1617'apply', 'chr', 'cmp', 'coerce', 'compile', 'dir', 'divmod', 'eval', 'execfi
1618le', 'float', 'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'le
1619n', 'long', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input',
1620 'reload', 'repr', 'round', 'setattr', 'str', 'type']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001621>>>
1622\end{verbatim}\ecode
1623
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001624
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001625\chapter{Output Formatting}
1626
1627So far we've encountered two ways of writing values: {\em expression
1628statements} and the {\tt print} statement. (A third way is using the
1629{\tt write} method of file objects; the standard output file can be
1630referenced as {\tt sys.stdout}. See the Library Reference for more
1631information on this.)
1632
1633Often you'll want more control over the formatting of your output than
1634simply printing space-separated values. The key to nice formatting in
1635Python is to do all the string handling yourself; using string slicing
1636and concatenation operations you can create any lay-out you can imagine.
1637The standard module {\tt string} contains some useful operations for
1638padding strings to a given column width; these will be discussed shortly.
1639
1640One question remains, of course: how do you convert values to strings?
1641Luckily, Python has a way to convert any value to a string: just write
1642the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001643
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001644\bcode\begin{verbatim}
1645>>> x = 10 * 3.14
1646>>> y = 200*200
1647>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1648>>> print s
1649The value of x is 31.4, and y is 40000...
1650>>> # Reverse quotes work on other types besides numbers:
1651>>> p = [x, y]
1652>>> ps = `p`
1653>>> ps
1654'[31.4, 40000]'
1655>>> # Converting a string adds string quotes and backslashes:
1656>>> hello = 'hello, world\n'
1657>>> hellos = `hello`
1658>>> print hellos
1659'hello, world\012'
1660>>> # The argument of reverse quotes may be a tuple:
1661>>> `x, y, ('foo', 'bar')`
1662'(31.4, 40000, (\'foo\', \'bar\'))'
1663>>>
1664\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001665%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001666Here is how you write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001667
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001668\bcode\begin{verbatim}
1669>>> import string
1670>>> for x in range(1, 11):
1671... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1672... # Note trailing comma on previous line
1673... print string.rjust(`x*x*x`, 4)
1674...
1675 1 1 1
1676 2 4 8
1677 3 9 27
1678 4 16 64
1679 5 25 125
1680 6 36 216
1681 7 49 343
1682 8 64 512
1683 9 81 729
168410 100 1000
1685>>>
1686\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001687%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001688(Note that one space between each column was added by the way {\tt print}
1689works: it always adds spaces between its arguments.)
1690
1691This example demonstrates the function {\tt string.rjust()}, which
1692right-justifies a string in a field of a given width by padding it with
1693spaces on the left. There are similar functions {\tt string.ljust()}
1694and {\tt string.center()}. These functions do not write anything, they
1695just return a new string. If the input string is too long, they don't
1696truncate it, but return it unchanged; this will mess up your column
1697lay-out but that's usually better than the alternative, which would be
1698lying about a value. (If you really want truncation you can always add
1699a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1700
1701There is another function, {\tt string.zfill}, which pads a numeric
1702string on the left with zeros. It understands about plus and minus
1703signs:%
1704\footnote{
1705 Better facilities for formatting floating point numbers are
1706 lacking at this moment.
1707}
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001708
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001709\bcode\begin{verbatim}
1710>>> string.zfill('12', 5)
1711'00012'
1712>>> string.zfill('-3.14', 7)
1713'-003.14'
1714>>> string.zfill('3.14159265359', 5)
1715'3.14159265359'
1716>>>
1717\end{verbatim}\ecode
1718
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001719
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001720\chapter{Errors and Exceptions}
1721
1722Until now error messages haven't been more than mentioned, but if you
1723have tried out the examples you have probably seen some. There are
1724(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1725and {\em exceptions}.
1726
1727\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001728
1729Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001730kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001731
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001732\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001733>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001734Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001735while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001736 ^
1737Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001738>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001739\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001740%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001741The parser repeats the offending line and displays a little `arrow'
1742pointing at the earliest point in the line where the error was detected.
1743The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001744{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001745the arrow: in the example, the error is detected at the keyword
1746{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001747File name and line number are printed so you know where to look in case
1748the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001749
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001750\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001751
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001752Even if a statement or expression is syntactically correct, it may
1753cause an error when an attempt is made to execute it.
1754Errors detected during execution are called {\em exceptions} and are
1755not unconditionally fatal: you will soon learn how to handle them in
1756Python programs. Most exceptions are not handled by programs,
1757however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001758
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001759\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001760>>> 10 * (1/0)
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 +00001763ZeroDivisionError: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001764>>> 4 + foo*3
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 +00001767NameError: foo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001768>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001769Stack backtrace (innermost last):
1770 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001771TypeError: illegal argument type for built-in operation
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001772>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001773\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001774%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001775The last line of the error message indicates what happened.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001776Exceptions come in different types, and the type is printed as part of
1777the message: the types in the example are
Guido van Rossumb2c65561993-05-12 08:53:36 +00001778{\tt ZeroDivisionError},
1779{\tt NameError}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001780and
Guido van Rossumb2c65561993-05-12 08:53:36 +00001781{\tt TypeError}.
1782The string printed as the exception type is the name of the built-in
1783name for the exception that occurred. This is true for all built-in
1784exceptions, but need not be true for user-defined exceptions (although
1785it is a useful convention).
1786Standard exception names are built-in identifiers (not reserved
1787keywords).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001788
Guido van Rossumb2c65561993-05-12 08:53:36 +00001789The rest of the line is a detail whose interpretation depends on the
1790exception type; its meaning is dependent on the exception type.
1791
1792The preceding part of the error message shows the context where the
1793exception happened, in the form of a stack backtrace.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001794In general it contains a stack backtrace listing source lines; however,
1795it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001796
Guido van Rossumb2c65561993-05-12 08:53:36 +00001797The Python library reference manual lists the built-in exceptions and
1798their meanings.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001799
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001800\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001801
1802It is possible to write programs that handle selected exceptions.
1803Look at the following example, which prints a table of inverses of
1804some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001805
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001806\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001807>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001808>>> for x in numbers:
1809... print x,
1810... try:
1811... print 1.0 / x
Guido van Rossumb2c65561993-05-12 08:53:36 +00001812... except ZeroDivisionError:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001813... print '*** has no inverse ***'
1814...
18150.3333 3.00030003
18162.5 0.4
18170 *** has no inverse ***
181810 0.1
1819>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001820\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001821%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001822The {\tt try} statement works as follows.
1823\begin{itemize}
1824\item
1825First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001826{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001827(the statement(s) between the {\tt try} and {\tt except} keywords) is
1828executed.
1829\item
1830If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001831{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001832is skipped and execution of the {\tt try} statement is finished.
1833\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001834If an exception occurs during execution of the try clause,
1835the rest of the clause is skipped. Then if
1836its type matches the exception named after the {\tt except} keyword,
1837the rest of the try clause is skipped, the except clause is executed,
1838and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001839\item
1840If an exception occurs which does not match the exception named in the
1841except clause, it is passed on to outer try statements; if no handler is
1842found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001843{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001844and execution stops with a message as shown above.
1845\end{itemize}
1846A {\tt try} statement may have more than one except clause, to specify
1847handlers for different exceptions.
1848At most one handler will be executed.
1849Handlers only handle exceptions that occur in the corresponding try
1850clause, not in other handlers of the same {\tt try} statement.
1851An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001852e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001853
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001854\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001855... except (RuntimeError, TypeError, NameError):
1856... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001857\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001858%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001859The last except clause may omit the exception name(s), to serve as a
1860wildcard.
Guido van Rossumb2c65561993-05-12 08:53:36 +00001861Use this with extreme caution, since it is easy to mask a real
1862programming error in this way!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001863
1864When an exception occurs, it may have an associated value, also known as
1865the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001866{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001867The presence and type of the argument depend on the exception type.
1868For exception types which have an argument, the except clause may
1869specify a variable after the exception name (or list) to receive the
1870argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001871
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001872\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001873>>> try:
1874... foo()
1875... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001876... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001877...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001878name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001879>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001880\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001881%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001882If an exception has an argument, it is printed as the last part
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001883(`detail') of the message for unhandled exceptions.
1884
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001885Exception handlers don't just handle exceptions if they occur
1886immediately in the try clause, but also if they occur inside functions
1887that are called (even indirectly) in the try clause.
1888For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001889
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001890\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001891>>> def this_fails():
1892... x = 1/0
1893...
1894>>> try:
1895... this_fails()
Guido van Rossumb2c65561993-05-12 08:53:36 +00001896... except ZeroDivisionError, detail:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001897... print 'Handling run-time error:', detail
1898...
Guido van Rossumb2c65561993-05-12 08:53:36 +00001899Handling run-time error: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001900>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001901\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001902
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001903\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001904
1905The {\tt raise} statement allows the programmer to force a specified
1906exception to occur.
1907For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001908
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001909\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001910>>> raise NameError, 'HiThere'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001911Stack backtrace (innermost last):
1912 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001913NameError: HiThere
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001914>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001915\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001916%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001917The first argument to {\tt raise} names the exception to be raised.
1918The optional second argument specifies the exception's argument.
1919
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001920\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001921
1922Programs may name their own exceptions by assigning a string to a
1923variable.
1924For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001925
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001926\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001927>>> my_exc = 'my_exc'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001928>>> try:
1929... raise my_exc, 2*2
1930... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001931... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001932...
1933My exception occured, value: 4
1934>>> raise my_exc, 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001935Stack backtrace (innermost last):
1936 File "<stdin>", line 7
Guido van Rossumb2c65561993-05-12 08:53:36 +00001937my_exc: 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001938>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001939\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001940%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001941Many standard modules use this to report errors that may occur in
1942functions they define.
1943
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001944\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001945
1946The {\tt try} statement has another optional clause which is intended to
1947define clean-up actions that must be executed under all circumstances.
1948For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001949
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001950\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001951>>> try:
1952... raise KeyboardInterrupt
1953... finally:
1954... print 'Goodbye, world!'
1955...
1956Goodbye, world!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001957Stack backtrace (innermost last):
1958 File "<stdin>", line 2
Guido van Rossumb2c65561993-05-12 08:53:36 +00001959KeyboardInterrupt
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001960>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001961\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001962%
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001963A {\tt finally} clause is executed whether or not an exception has
1964occurred in the {\tt try} clause. When an exception has occurred, it
1965is re-raised after the {\tt finally} clauses is executed. The
1966{\tt finally} clause is also executed ``on the way out'' when the
1967{\tt try} statement is left via a {\tt break} or {\tt return}
1968statement.
1969
1970A {\tt try} statement must either have one or more {\tt except}
1971clauses or one {\tt finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001972
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001973
1974\chapter{Classes}
1975
1976Python's class mechanism adds classes to the language with a minimum
1977of new syntax and semantics. It is a mixture of the class mechanisms
1978found in C++ and Modula-3. As is true for modules, classes in Python
1979do not put an absolute barrier between definition and user, but rather
1980rely on the politeness of the user not to ``break into the
1981definition.'' The most important features of classes are retained
1982with full power, however: the class inheritance mechanism allows
1983multiple base classes, a derived class can override any methods of its
1984base class(es), a method can call the method of a base class with the
1985same name. Objects can contain an arbitrary amount of private data.
1986
1987In C++ terminology, all class members (including the data members) are
1988{\em public}, and all member functions are {\em virtual}. There are
1989no special constructors or desctructors. As in Modula-3, there are no
1990shorthands for referencing the object's members from its methods: the
1991method function is declared with an explicit first argument
1992representing the object, which is provided implicitly by the call. As
1993in Smalltalk, classes themselves are objects, albeit in the wider
1994sense of the word: in Python, all data types are objects. This
1995provides semantics for importing and renaming. But, just like in C++
1996or Modula-3, built-in types cannot be used as base classes for
1997extension by the user. Also, like in Modula-3 but unlike in C++, the
1998built-in operators with special syntax (arithmetic operators,
1999subscriptong etc.) cannot be redefined for class members.
2000
2001
2002\section{A word about terminology}
2003
2004Lacking universally accepted terminology to talk about classes, I'll
2005make occasional use of Smalltalk and C++ terms. (I'd use Modula-3
2006terms, since its object-oriented semantics are closer to those of
2007Python than C++, but I expect that few readers have heard of it...)
2008
2009I also have to warn you that there's a terminological pitfall for
2010object-oriented readers: the word ``object'' in Python does not
2011necessarily mean a class instance. Like C++ and Modula-3, and unlike
2012Smalltalk, not all types in Python are classes: the basic built-in
2013types like integers and lists aren't, and even somewhat more exotic
2014types like files aren't. However, {\em all} Python types share a little
2015bit of common semantics that is best described by using the word
2016object.
2017
2018Objects have individuality, and multiple names (in multiple scopes)
2019can be bound to the same object. This is known as aliasing in other
2020languages. This is usually not appreciated on a first glance at
2021Python, and can be safely ignored when dealing with immutable basic
2022types (numbers, strings, tuples). However, aliasing has an
2023(intended!) effect on the semantics of Python code involving mutable
2024objects such as lists, dictionaries, and most types representing
2025entities outside the program (files, windows, etc.). This is usually
2026used to the benefit of the program, since aliases behave like pointers
2027in some respects. For example, passing an object is cheap since only
2028a pointer is passed by the implementation; and if a function modifies
2029an object passed as an argument, the caller will see the change --- this
2030obviates the need for two different argument passing mechanisms as in
2031Pascal.
2032
2033
2034\section{Python scopes and name spaces}
2035
2036Before introducing classes, I first have to tell you something about
2037Python's scope rules. Class definitions play some neat tricks with
2038name spaces, and you need to know how scopes and name spaces work to
2039fully understand what's going on. Incidentally, knowledge about this
2040subject is useful for any advanced Python programmer.
2041
2042Let's begin with some definitions.
2043
2044A {\em name space} is a mapping from names to objects. Most name
2045spaces are currently implemented as Python dictionaries, but that's
2046normally not noticeable in any way (except for performance), and it
2047may change in the future. Examples of name spaces are: the set of
2048built-in names (functions such as \verb\abs()\, and built-in exception
2049names); the global names in a module; and the local names in a
2050function invocation. In a sense the set of attributes of an object
2051also form a name space. The important things to know about name
2052spaces is that there is absolutely no relation between names in
2053different name spaces; for instance, two different modules may both
2054define a function ``maximize'' without confusion --- users of the
2055modules must prefix it with the module name.
2056
2057By the way, I use the word {\em attribute} for any name following a
2058dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2059an attribute of the object \verb\z\. Strictly speaking, references to
2060names in modules are attribute references: in the expression
2061\verb\modname.funcname\, \verb\modname\ is a module object and
2062\verb\funcname\ is an attribute of it. In this case there happens to
2063be a straightforward mapping between the module's attributes and the
2064global names defined in the module: they share the same name space!%
2065\footnote{
2066 Except for one thing. Module objects have a secret read-only
2067 attribute called {\tt __dict__} which returns the dictionary
2068 used to implement the module's name space; the name
2069 {\tt __dict__} is an attribute but not a global name.
2070 Obviously, using this violates the abstraction of name space
2071 implementation, and should be restricted to things like
2072 post-mortem debuggers...
2073}
2074
2075Attributes may be read-only or writable. In the latter case,
2076assignment to attributes is possible. Module attributes are writable:
2077you can write \verb\modname.the_answer = 42\. Writable attributes may
2078also be deleted with the del statement, e.g.
2079\verb\del modname.the_answer\.
2080
2081Name spaces are created at different moments and have different
2082lifetimes. The name space containing the built-in names is created
2083when the Python interpreter starts up, and is never deleted. The
2084global name space for a module is created when the module definition
2085is read in; normally, module name spaces also last until the
2086interpreter quits. The statements executed by the top-level
2087invocation of the interpreter, either read from a script file or
2088interactively, are considered part of a module called \verb\__main__\,
2089so they have their own global name space. (The built-in names
Guido van Rossum4bd023f1993-10-27 13:49:20 +00002090actually also live in a module; this is called \verb\__builtin__\.)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002091
2092The local name space for a function is created when the function is
2093called, and deleted when the function returns or raises an exception
2094that is not handled within the function. (Actually, forgetting would
2095be a better way to describe what actually happens.) Of course,
2096recursive invocations each have their own local name space.
2097
2098A {\em scope} is a textual region of a Python program where a name space
2099is directly accessible. ``Directly accessible'' here means that an
2100unqualified reference to a name attempts to find the name in the name
2101space.
2102
2103Although scopes are determined statically, they are used dynamically.
2104At any time during execution, exactly three nested scopes are in use
2105(i.e., exactly three name spaces are directly accessible): the
2106innermost scope, which is searched first, contains the local names,
2107the middle scope, searched next, contains the current module's global
2108names, and the outermost scope (searched last) is the name space
2109containing built-in names.
2110
2111Usually, the local scope references the local names of the (textually)
2112current function. Outside functions, the the local scope references
2113the same name space as the global scope: the module's name space.
2114Class definitions place yet another name space in the local scope.
2115
2116It is important to realize that scopes are determined textually: the
2117global scope of a function defined in a module is that module's name
2118space, no matter from where or by what alias the function is called.
2119On the other hand, the actual search for names is done dynamically, at
2120run time --- however, the the language definition is evolving towards
2121static name resolution, at ``compile'' time, so don't rely on dynamic
2122name resolution! (In fact, local variables are already determined
2123statically.)
2124
2125A special quirk of Python is that assignments always go into the
2126innermost scope. Assignments do not copy data --- they just
2127bind names to objects. The same is true for deletions: the statement
2128\verb\del x\ removes the binding of x from the name space referenced by the
2129local scope. In fact, all operations that introduce new names use the
2130local scope: in particular, import statements and function definitions
2131bind the module or function name in the local scope. (The
2132\verb\global\ statement can be used to indicate that particular
2133variables live in the global scope.)
2134
2135
2136\section{A first look at classes}
2137
2138Classes introduce a little bit of new syntax, three new object types,
2139and some new semantics.
2140
2141
2142\subsection{Class definition syntax}
2143
2144The simplest form of class definition looks like this:
2145
2146\begin{verbatim}
2147 class ClassName:
2148 <statement-1>
2149 .
2150 .
2151 .
2152 <statement-N>
2153\end{verbatim}
2154
2155Class definitions, like function definitions (\verb\def\ statements)
2156must be executed before they have any effect. (You could conceivably
2157place a class definition in a branch of an \verb\if\ statement, or
2158inside a function.)
2159
2160In practice, the statements inside a class definition will usually be
2161function definitions, but other statements are allowed, and sometimes
2162useful --- we'll come back to this later. The function definitions
2163inside a class normally have a peculiar form of argument list,
2164dictated by the calling conventions for methods --- again, this is
2165explained later.
2166
2167When a class definition is entered, a new name space is created, and
2168used as the local scope --- thus, all assignments to local variables
2169go into this new name space. In particular, function definitions bind
2170the name of the new function here.
2171
2172When a class definition is left normally (via the end), a {\em class
2173object} is created. This is basically a wrapper around the contents
2174of the name space created by the class definition; we'll learn more
2175about class objects in the next section. The original local scope
2176(the one in effect just before the class definitions was entered) is
2177reinstated, and the class object is bound here to class name given in
2178the class definition header (ClassName in the example).
2179
2180
2181\subsection{Class objects}
2182
2183Class objects support two kinds of operations: attribute references
2184and instantiation.
2185
2186{\em Attribute references} use the standard syntax used for all
2187attribute references in Python: \verb\obj.name\. Valid attribute
2188names are all the names that were in the class's name space when the
2189class object was created. So, if the class definition looked like
2190this:
2191
2192\begin{verbatim}
2193 class MyClass:
2194 i = 12345
2195 def f(x):
2196 return 'hello world'
2197\end{verbatim}
2198
2199then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
2200references, returning an integer and a function object, respectively.
2201Class attributes can also be assigned to, so you can change the
2202value of \verb\MyClass.i\ by assignment.
2203
2204Class {\em instantiation} uses function notation. Just pretend that
2205the class object is a parameterless function that returns a new
2206instance of the class. For example, (assuming the above class):
2207
2208\begin{verbatim}
2209 x = MyClass()
2210\end{verbatim}
2211
2212creates a new {\em instance} of the class and assigns this object to
2213the local variable \verb\x\.
2214
2215
2216\subsection{Instance objects}
2217
2218Now what can we do with instance objects? The only operations
2219understood by instance objects are attribute references. There are
2220two kinds of valid attribute names.
2221
2222The first I'll call {\em data attributes}. These correspond to
2223``instance variables'' in Smalltalk, and to ``data members'' in C++.
2224Data attributes need not be declared; like local variables, they
2225spring into existence when they are first assigned to. For example,
2226if \verb\x\ in the instance of \verb\MyClass\ created above, the
2227following piece of code will print the value 16, without leaving a
2228trace:
2229
2230\begin{verbatim}
2231 x.counter = 1
2232 while x.counter < 10:
2233 x.counter = x.counter * 2
2234 print x.counter
2235 del x.counter
2236\end{verbatim}
2237
2238The second kind of attribute references understood by instance objects
2239are {\em methods}. A method is a function that ``belongs to'' an
2240object. (In Python, the term method is not unique to class instances:
2241other object types can have methods as well, e.g., list objects have
2242methods called append, insert, remove, sort, and so on. However,
2243below, we'll use the term method exclusively to mean methods of class
2244instance objects, unless explicitly stated otherwise.)
2245
2246Valid method names of an instance object depend on its class. By
2247definition, all attributes of a class that are (user-defined) function
2248objects define corresponding methods of its instances. So in our
2249example, \verb\x.f\ is a valid method reference, since
2250\verb\MyClass.f\ is a function, but \verb\x.i\ is not, since
2251\verb\MyClass.i\ is not. But \verb\x.f\ is not the
2252same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a
2253function object.
2254
2255
2256\subsection{Method objects}
2257
2258Usually, a method is called immediately, e.g.:
2259
2260\begin{verbatim}
2261 x.f()
2262\end{verbatim}
2263
2264In our example, this will return the string \verb\'hello world'\.
2265However, it is not necessary to call a method right away: \verb\x.f\
2266is a method object, and can be stored away and called at a later
2267moment, for example:
2268
2269\begin{verbatim}
2270 xf = x.f
2271 while 1:
2272 print xf()
2273\end{verbatim}
2274
2275will continue to print \verb\hello world\ until the end of time.
2276
2277What exactly happens when a method is called? You may have noticed
2278that \verb\x.f()\ was called without an argument above, even though
2279the function definition for \verb\f\ specified an argument. What
2280happened to the argument? Surely Python raises an exception when a
2281function that requires an argument is called without any --- even if
2282the argument isn't actually used...
2283
2284Actually, you may have guessed the answer: the special thing about
2285methods is that the object is passed as the first argument of the
2286function. In our example, the call \verb\x.f()\ is exactly equivalent
2287to \verb\MyClass.f(x)\. In general, calling a method with a list of
2288{\em n} arguments is equivalent to calling the corresponding function
2289with an argument list that is created by inserting the method's object
2290before the first argument.
2291
2292If you still don't understand how methods work, a look at the
2293implementation can perhaps clarify matters. When an instance
2294attribute is referenced that isn't a data attribute, its class is
2295searched. If the name denotes a valid class attribute that is a
2296function object, a method object is created by packing (pointers to)
2297the instance object and the function object just found together in an
2298abstract object: this is the method object. When the method object is
2299called with an argument list, it is unpacked again, a new argument
2300list is constructed from the instance object and the original argument
2301list, and the function object is called with this new argument list.
2302
2303
2304\section{Random remarks}
2305
2306
2307[These should perhaps be placed more carefully...]
2308
2309
2310Data attributes override method attributes with the same name; to
2311avoid accidental name conflicts, which may cause hard-to-find bugs in
2312large programs, it is wise to use some kind of convention that
2313minimizes the chance of conflicts, e.g., capitalize method names,
2314prefix data attribute names with a small unique string (perhaps just
2315an undescore), or use verbs for methods and nouns for data attributes.
2316
2317
2318Data attributes may be referenced by methods as well as by ordinary
2319users (``clients'') of an object. In other words, classes are not
2320usable to implement pure abstract data types. In fact, nothing in
2321Python makes it possible to enforce data hiding --- it is all based
2322upon convention. (On the other hand, the Python implementation,
2323written in C, can completely hide implementation details and control
2324access to an object if necessary; this can be used by extensions to
2325Python written in C.)
2326
2327
2328Clients should use data attributes with care --- clients may mess up
2329invariants maintained by the methods by stamping on their data
2330attributes. Note that clients may add data attributes of their own to
2331an instance object without affecting the validity of the methods, as
2332long as name conflicts are avoided --- again, a naming convention can
2333save a lot of headaches here.
2334
2335
2336There is no shorthand for referencing data attributes (or other
2337methods!) from within methods. I find that this actually increases
2338the readability of methods: there is no chance of confusing local
2339variables and instance variables when glancing through a method.
2340
2341
2342Conventionally, the first argument of methods is often called
2343\verb\self\. This is nothing more than a convention: the name
2344\verb\self\ has absolutely no special meaning to Python. (Note,
2345however, that by not following the convention your code may be less
2346readable by other Python programmers, and it is also conceivable that
2347a {\em class browser} program be written which relies upon such a
2348convention.)
2349
2350
2351Any function object that is a class attribute defines a method for
2352instances of that class. It is not necessary that the function
2353definition is textually enclosed in the class definition: assigning a
2354function object to a local variable in the class is also ok. For
2355example:
2356
2357\begin{verbatim}
2358 # Function defined outside the class
2359 def f1(self, x, y):
2360 return min(x, x+y)
2361
2362 class C:
2363 f = f1
2364 def g(self):
2365 return 'hello world'
2366 h = g
2367\end{verbatim}
2368
2369Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
2370\verb\C\ that refer to function objects, and consequently they are all
2371methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
2372to \verb\g\. Note that this practice usually only serves to confuse
2373the reader of a program.
2374
2375
2376Methods may call other methods by using method attributes of the
2377\verb\self\ argument, e.g.:
2378
2379\begin{verbatim}
2380 class Bag:
2381 def empty(self):
2382 self.data = []
2383 def add(self, x):
2384 self.data.append(x)
2385 def addtwice(self, x):
Guido van Rossum084b0b21992-08-14 09:19:56 +00002386 self.add(x)
2387 self.add(x)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002388\end{verbatim}
2389
2390
2391The instantiation operation (``calling'' a class object) creates an
2392empty object. Many classes like to create objects in a known initial
2393state. There is no special syntax to enforce this, but a convention
2394works almost as well: add a method named \verb\init\ to the class,
2395which initializes the instance (by assigning to some important data
2396attributes) and returns the instance itself. For example, class
2397\verb\Bag\ above could have the following method:
2398
2399\begin{verbatim}
2400 def init(self):
2401 self.empty()
2402 return self
2403\end{verbatim}
2404
2405The client can then create and initialize an instance in one
2406statement, as follows:
2407
2408\begin{verbatim}
2409 x = Bag().init()
2410\end{verbatim}
2411
2412Of course, the \verb\init\ method may have arguments for greater
2413flexibility.
2414
2415Warning: a common mistake is to forget the \verb\return self\ at the
2416end of an init method!
2417
2418
2419Methods may reference global names in the same way as ordinary
2420functions. The global scope associated with a method is the module
2421containing the class definition. (The class itself is never used as a
2422global scope!) While one rarely encounters a good reason for using
2423global data in a method, there are many legitimate uses of the global
2424scope: for one thing, functions and modules imported into the global
2425scope can be used by methods, as well as functions and classes defined
2426in it. Usually, the class containing the method is itself defined in
2427this global scope, and in the next section we'll find some good
2428reasons why a method would want to reference its own class!
2429
2430
2431\section{Inheritance}
2432
2433Of course, a language feature would not be worthy of the name ``class''
2434without supporting inheritance. The syntax for a derived class
2435definition looks as follows:
2436
2437\begin{verbatim}
2438 class DerivedClassName(BaseClassName):
2439 <statement-1>
2440 .
2441 .
2442 .
2443 <statement-N>
2444\end{verbatim}
2445
2446The name \verb\BaseClassName\ must be defined in a scope containing
2447the derived class definition. Instead of a base class name, an
2448expression is also allowed. This is useful when the base class is
2449defined in another module, e.g.,
2450
2451\begin{verbatim}
2452 class DerivedClassName(modname.BaseClassName):
2453\end{verbatim}
2454
2455Execution of a derived class definition proceeds the same as for a
2456base class. When the class object is constructed, the base class is
2457remembered. This is used for resolving attribute references: if a
2458requested attribute is not found in the class, it is searched in the
2459base class. This rule is applied recursively if the base class itself
2460is derived from some other class.
2461
2462There's nothing special about instantiation of derived classes:
2463\verb\DerivedClassName()\ creates a new instance of the class. Method
2464references are resolved as follows: the corresponding class attribute
2465is searched, descending down the chain of base classes if necessary,
2466and the method reference is valid if this yields a function object.
2467
2468Derived classes may override methods of their base classes. Because
2469methods have no special privileges when calling other methods of the
2470same object, a method of a base class that calls another method
2471defined in the same base class, may in fact end up calling a method of
2472a derived class that overrides it. (For C++ programmers: all methods
2473in Python are ``virtual functions''.)
2474
2475An overriding method in a derived class may in fact want to extend
2476rather than simply replace the base class method of the same name.
2477There is a simple way to call the base class method directly: just
2478call \verb\BaseClassName.methodname(self, arguments)\. This is
2479occasionally useful to clients as well. (Note that this only works if
2480the base class is defined or imported directly in the global scope.)
2481
2482
2483\subsection{Multiple inheritance}
2484
2485Poython supports a limited form of multiple inheritance as well. A
2486class definition with multiple base classes looks as follows:
2487
2488\begin{verbatim}
2489 class DerivedClassName(Base1, Base2, Base3):
2490 <statement-1>
2491 .
2492 .
2493 .
2494 <statement-N>
2495\end{verbatim}
2496
2497The only rule necessary to explain the semantics is the resolution
2498rule used for class attribute references. This is depth-first,
2499left-to-right. Thus, if an attribute is not found in
2500\verb\DerivedClassName\, it is searched in \verb\Base1\, then
2501(recursively) in the base classes of \verb\Base1\, and only if it is
2502not found there, it is searched in \verb\Base2\, and so on.
2503
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002504(To some people breadth first---searching \verb\Base2\ and
2505\verb\Base3\ before the base classes of \verb\Base1\---looks more
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002506natural. However, this would require you to know whether a particular
2507attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
2508one of its base classes before you can figure out the consequences of
2509a name conflict with an attribute of \verb\Base2\. The depth-first
2510rule makes no differences between direct and inherited attributes of
2511\verb\Base1\.)
2512
2513It is clear that indiscriminate use of multiple inheritance is a
2514maintenance nightmare, given the reliance in Python on conventions to
2515avoid accidental name conflicts. A well-known problem with multiple
2516inheritance is a class derived from two classes that happen to have a
2517common base class. While it is easy enough to figure out what happens
2518in this case (the instance will have a single copy of ``instance
2519variables'' or data attributes used by the common base class), it is
2520not clear that these semantics are in any way useful.
2521
2522
2523\section{Odds and ends}
2524
2525Sometimes it is useful to have a data type similar to the Pascal
2526``record'' or C ``struct'', bundling together a couple of named data
2527items. An empty class definition will do nicely, e.g.:
2528
2529\begin{verbatim}
2530 class Employee:
2531 pass
2532
2533 john = Employee() # Create an empty employee record
2534
2535 # Fill the fields of the record
2536 john.name = 'John Doe'
2537 john.dept = 'computer lab'
2538 john.salary = 1000
2539\end{verbatim}
2540
2541
2542A piece of Python code that expects a particular abstract data type
2543can often be passed a class that emulates the methods of that data
2544type instead. For instance, if you have a function that formats some
2545data from a file object, you can define a class with methods
2546\verb\read()\ and \verb\readline()\ that gets the data from a string
2547buffer instead, and pass it as an argument. (Unfortunately, this
2548technique has its limitations: a class can't define operations that
2549are accessed by special syntax such as sequence subscripting or
2550arithmetic operators, and assigning such a ``pseudo-file'' to
2551\verb\sys.stdin\ will not cause the interpreter to read further input
2552from it.)
2553
2554
2555Instance method objects have attributes, too: \verb\m.im_self\ is the
2556object of which the method is an instance, and \verb\m.im_func\ is the
2557function object corresponding to the method.
2558
2559
2560XXX Mention bw compat hacks.
2561
2562
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002563\end{document}