blob: df41199f809e4750735798b7f6dfda438370f892 [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
Guido van Rossum02455691997-07-17 16:21:52 +00003% Things to do:
4% Add a section on file I/O
5% Write a chapter entitled ``Some Useful Modules''
6% --regex, math+cmath
7% Should really move the Python startup file info to an appendix
Guido van Rossum02455691997-07-17 16:21:52 +00008
Guido van Rossumdccc2981997-12-30 04:40:25 +00009\title{Python Tutorial}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000010
Guido van Rossum16cd7f91994-10-06 10:29:26 +000011\input{boilerplate}
Guido van Rossum83eb9621993-11-23 16:28:45 +000012
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000013\begin{document}
14
15\pagenumbering{roman}
16
17\maketitle
18
Guido van Rossum16cd7f91994-10-06 10:29:26 +000019\input{copyright}
20
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000021\begin{abstract}
22
23\noindent
Guido van Rossumdccc2981997-12-30 04:40:25 +000024Python is an easy to learn, powerful programming language. It has
25efficient high-level data structures and a simple but effective
26approach to object-oriented programming. Python's elegant syntax and
27dynamic typing, together with its interpreted nature, make it an ideal
28language for scripting and rapid application development in many areas
29on most platforms.
30
31The Python interpreter and the extensive standard library are freely
32available in source or binary form for all major platforms from the
33Python web site, \file{http://www.python.org}, and can be freely
34distributed. The same site also contains distributions of and
35pointers to many free third party Python modules, programs and tools,
36and additional documentation.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000037
Guido van Rossum4410c751991-06-04 20:22:18 +000038The Python interpreter is easily extended with new functions and data
Guido van Rossumdccc2981997-12-30 04:40:25 +000039types implemented in C or C++ (or other languages callable from C).
40Python is also suitable as an extension language for customizable
41applications.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000042
Guido van Rossum6fc178f1991-08-16 09:13:42 +000043This tutorial introduces the reader informally to the basic concepts
44and features of the Python language and system. It helps to have a
Guido van Rossumdccc2981997-12-30 04:40:25 +000045Python interpreter handy for hands-on experience, but all examples are
46self-contained, so the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000047
Guido van Rossumdccc2981997-12-30 04:40:25 +000048For a description of standard objects and modules, see the
49\emph{Python Library Reference} document. The \emph{Python Reference
50Manual} gives a more formal definition of the language. To write
51extensions in C or C++, read the \emph{Extending and Embedding} and
52\emph{Python/C API} manuals. There are also several books covering
53Python in depth.
54
55This tutorial does not attempt to be comprehensive and cover every
56single feature, or even every commonly used feature. Instead, it
57introduces many of Python's most noteworthy features, and will give
58you a good idea of the language's flavor and style. After reading it,
59you will be able to read and write Python modules and programs, and
60you will be ready to learn more about the various Python library
61modules described in the \emph{Python Library Reference}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000062
63\end{abstract}
64
65\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000066{
67\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000068\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000069}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000070
71\pagebreak
72
73\pagenumbering{arabic}
74
Guido van Rossum5e0759d1992-08-07 16:06:24 +000075
Guido van Rossum6fc178f1991-08-16 09:13:42 +000076\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000077
Guido van Rossum3a26dd81996-10-24 22:12:48 +000078\section{Introduction}
79
Guido van Rossum6fc178f1991-08-16 09:13:42 +000080If you ever wrote a large shell script, you probably know this
81feeling: you'd love to add yet another feature, but it's already so
82slow, and so big, and so complicated; or the feature involves a system
Guido van Rossum02455691997-07-17 16:21:52 +000083call or other function that is only accessible from C \ldots Usually
Guido van Rossum6fc178f1991-08-16 09:13:42 +000084the problem at hand isn't serious enough to warrant rewriting the
Guido van Rossum02455691997-07-17 16:21:52 +000085script in C; perhaps the problem requires variable-length strings or
86other data types (like sorted lists of file names) that are easy in
87the shell but lots of work to implement in C, or perhaps you're not
88sufficiently familiar with C.
89
90Another situation: perhaps you have to work with several C libraries,
91and the usual C write/compile/test/re-compile cycle is too slow. You
92need to develop software more quickly. Possibly perhaps you've
93written a program that could use an extension language, and you don't
94want to design a language, write and debug an interpreter for it, then
95tie it into your application.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000096
Guido van Rossum6fc178f1991-08-16 09:13:42 +000097In such cases, Python may be just the language for you. Python is
98simple to use, but it is a real programming language, offering much
99more structure and support for large programs than the shell has. On
100the other hand, it also offers much more error checking than C, and,
Fred Drakeeee08cd1997-12-04 15:43:15 +0000101being a \emph{very-high-level language}, it has high-level data types
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000102built in, such as flexible arrays and dictionaries that would cost you
103days to implement efficiently in C. Because of its more general data
Fred Drakeeee08cd1997-12-04 15:43:15 +0000104types Python is applicable to a much larger problem domain than
105\emph{Awk} or even \emph{Perl}, yet many things are at least as easy
106in Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000107
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000108Python allows you to split up your program in modules that can be
109reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000110standard modules that you can use as the basis of your programs --- or
111as examples to start learning to program in Python. There are also
112built-in modules that provide things like file I/O, system calls,
Guido van Rossum02455691997-07-17 16:21:52 +0000113sockets, and even interfaces to GUI toolkits like Tk.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000114
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000115Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000116during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000117necessary. The interpreter can be used interactively, which makes it
118easy to experiment with features of the language, to write throw-away
119programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000120It is also a handy desk calculator.
121
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000122Python allows writing very compact and readable programs. Programs
123written in Python are typically much shorter than equivalent C
124programs, for several reasons:
125\begin{itemize}
126\item
127the high-level data types allow you to express complex operations in a
128single statement;
129\item
130statement grouping is done by indentation instead of begin/end
131brackets;
132\item
133no variable or argument declarations are necessary.
134\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000135
Fred Drakeeee08cd1997-12-04 15:43:15 +0000136Python is \emph{extensible}: if you know how to program in C it is easy
Guido van Rossum02455691997-07-17 16:21:52 +0000137to add a new built-in function or module to the interpreter, either to
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000138perform critical operations at maximum speed, or to link Python
139programs to libraries that may only be available in binary form (such
140as a vendor-specific graphics library). Once you are really hooked,
141you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000142and use it as an extension or command language for that application.
143
Guido van Rossum02455691997-07-17 16:21:52 +0000144By the way, the language is named after the BBC show ``Monty Python's
145Flying Circus'' and has nothing to do with nasty reptiles. Making
146references to Monty Python skits in documentation is not only allowed,
Guido van Rossumdccc2981997-12-30 04:40:25 +0000147it is encouraged!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000148
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000149\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000150
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000151Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000152in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000153using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000154
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000155In the next chapter, the mechanics of using the interpreter are
156explained. This is rather mundane information, but essential for
157trying out the examples shown later.
158
Guido van Rossum4410c751991-06-04 20:22:18 +0000159The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000160language and system though examples, beginning with simple
161expressions, statements and data types, through functions and modules,
Guido van Rossum6938f061994-08-01 12:22:53 +0000162and finally touching upon advanced concepts like exceptions
163and user-defined classes.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000164
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000165\chapter{Using the Python Interpreter}
166
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000167\section{Invoking the Interpreter}
168
Fred Drakeeee08cd1997-12-04 15:43:15 +0000169The Python interpreter is usually installed as \file{/usr/local/bin/python}
170on those machines where it is available; putting \file{/usr/local/bin} in
Fred Drake6dc2aae1996-12-13 21:56:03 +0000171your \UNIX{} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000172typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000173
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000174\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000175python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000176\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000177%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000178to the shell. Since the choice of the directory where the interpreter
179lives is an installation option, other places are possible; check with
Fred Drakeeee08cd1997-12-04 15:43:15 +0000180your local Python guru or system administrator. (E.g.,
181\file{/usr/local/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000182
Guido van Rossum02455691997-07-17 16:21:52 +0000183Typing an EOF character (Control-D on \UNIX{}, Control-Z or F6 on DOS
184or Windows) at the primary prompt causes the interpreter to exit with
185a zero exit status. If that doesn't work, you can exit the
186interpreter by typing the following commands: \code{import sys ;
187sys.exit()}.
188
189The interpreter's line-editing features usually aren't very
190sophisticated. On Unix, whoever installed the interpreter may have
191enabled support for the GNU readline library, which adds more
192elaborate interactive editing and history features. Perhaps the
193quickest check to see whether command line editing is supported is
194typing Control-P to the first Python prompt you get. If it beeps, you
195have command line editing; see Appendix A for an introduction to the
Fred Drakeeee08cd1997-12-04 15:43:15 +0000196keys. If nothing appears to happen, or if \code{\^P} is echoed,
Guido van Rossum02455691997-07-17 16:21:52 +0000197command line editing isn't available; you'll only be able to use
198backspace to remove characters from the current line.
199
Fred Drake6dc2aae1996-12-13 21:56:03 +0000200The interpreter operates somewhat like the \UNIX{} shell: when called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000201with standard input connected to a tty device, it reads and executes
202commands interactively; when called with a file name argument or with
Fred Drakeeee08cd1997-12-04 15:43:15 +0000203a file as standard input, it reads and executes a \emph{script} from
Guido van Rossum02455691997-07-17 16:21:52 +0000204that file.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000205
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000206A third way of starting the interpreter is
Fred Drakeeee08cd1997-12-04 15:43:15 +0000207\samp{python -c command [arg] ...}, which
208executes the statement(s) in \code{command}, analogous to the shell's
209\code{-c} option. Since Python statements often contain spaces or other
210characters that are special to the shell, it is best to quote
211\code{command} in its entirety with double quotes.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000212
Fred Drakeeee08cd1997-12-04 15:43:15 +0000213Note that there is a difference between \samp{python file} and
214\samp{python <file}. In the latter case, input requests from the
215program, such as calls to \code{input()} and \code{raw_input()}, are
216satisfied from \emph{file}. Since this file has already been read
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000217until the end by the parser before the program starts executing, the
218program will encounter EOF immediately. In the former case (which is
219usually what you want) they are satisfied from whatever file or device
220is connected to standard input of the Python interpreter.
221
Guido van Rossumb2c65561993-05-12 08:53:36 +0000222When a script file is used, it is sometimes useful to be able to run
223the script and enter interactive mode afterwards. This can be done by
Fred Drakeeee08cd1997-12-04 15:43:15 +0000224passing \code{-i} before the script. (This does not work if the script
Guido van Rossumb2c65561993-05-12 08:53:36 +0000225is read from standard input, for the same reason as explained in the
226previous paragraph.)
227
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000228\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000229
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000230When known to the interpreter, the script name and additional
Fred Drakeeee08cd1997-12-04 15:43:15 +0000231arguments thereafter are passed to the script in the variable
232\code{sys.argv}, which is a list of strings. Its length is at least
233one; when no script and no arguments are given, \code{sys.argv[0]} is
234an empty string. When the script name is given as \code{'-'} (meaning
235standard input), \code{sys.argv[0]} is set to \code{'-'}. When \code{-c
236command} is used, \code{sys.argv[0]} is set to \code{'-c'}. Options
237found after \code{-c command} are not consumed by the Python
238interpreter's option processing but left in \code{sys.argv} for the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000239command to handle.
240
241\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000242
Guido van Rossumdd010801991-06-07 14:31:11 +0000243When commands are read from a tty, the interpreter is said to be in
Fred Drakeeee08cd1997-12-04 15:43:15 +0000244\emph{interactive mode}. In this mode it prompts for the next command
245with the \emph{primary prompt}, usually three greater-than signs
246(\code{>>>}); for continuation lines it prompts with the
247\emph{secondary prompt},
248by default three dots (\code{...}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000249
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000250The interpreter prints a welcome message stating its version number
251and a copyright notice before printing the first prompt, e.g.:
252
253\bcode\begin{verbatim}
254python
Fred Drakeeee08cd1997-12-04 15:43:15 +0000255Python 1.5b1 (#1, Dec 3 1997, 00:02:06) [GCC 2.7.2.2] on sunos5
256Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000257>>>
258\end{verbatim}\ecode
259
260\section{The Interpreter and its Environment}
261
262\subsection{Error Handling}
263
264When an error occurs, the interpreter prints an error
265message and a stack trace. In interactive mode, it then returns to
266the primary prompt; when input came from a file, it exits with a
267nonzero exit status after printing
Fred Drakeeee08cd1997-12-04 15:43:15 +0000268the stack trace. (Exceptions handled by an \code{except} clause in a
269\code{try} statement are not errors in this context.) Some errors are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000270unconditionally fatal and cause an exit with a nonzero exit; this
271applies to internal inconsistencies and some cases of running out of
272memory. All error messages are written to the standard error stream;
273normal output from the executed commands is written to standard
274output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000275
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000276Typing the interrupt character (usually Control-C or DEL) to the
277primary or secondary prompt cancels the input and returns to the
278primary prompt.%
279\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +0000280 A problem with the GNU Readline package may prevent this.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000281}
Fred Drakeeee08cd1997-12-04 15:43:15 +0000282Typing an interrupt while a command is executing raises the
283\code{KeyboardInterrupt} exception, which may be handled by a
284\code{try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000285
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000286\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000287
Fred Drake6dc2aae1996-12-13 21:56:03 +0000288On BSD'ish \UNIX{} systems, Python scripts can be made directly
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000289executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000290
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000291\bcode\begin{verbatim}
Fred Drake9e63faa1997-10-15 14:37:24 +0000292#! /usr/bin/env python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000293\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000294%
Fred Drake9e63faa1997-10-15 14:37:24 +0000295(assuming that the interpreter is on the user's PATH) at the beginning
Fred Drakeeee08cd1997-12-04 15:43:15 +0000296of the script and giving the file an executable mode. The \code{\#!}
Fred Drake9e63faa1997-10-15 14:37:24 +0000297must be the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000298
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000299\subsection{The Interactive Startup File}
300
Guido van Rossum02455691997-07-17 16:21:52 +0000301XXX This should probably be dumped in an appendix, since most people
302don't use Python interactively in non-trivial ways.
303
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000304When you use Python interactively, it is frequently handy to have some
305standard commands executed every time the interpreter is started. You
Fred Drakeeee08cd1997-12-04 15:43:15 +0000306can do this by setting an environment variable named
307\code{PYTHONSTARTUP} to the name of a file containing your start-up
308commands. This is similar to the \file{.profile} feature of the \UNIX{}
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000309shells.
310
311This file is only read in interactive sessions, not when Python reads
Fred Drakeeee08cd1997-12-04 15:43:15 +0000312commands from a script, and not when \file{/dev/tty} is given as the
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000313explicit source of commands (which otherwise behaves like an
314interactive session). It is executed in the same name space where
315interactive commands are executed, so that objects that it defines or
316imports can be used without qualification in the interactive session.
Fred Drakeeee08cd1997-12-04 15:43:15 +0000317You can also change the prompts \code{sys.ps1} and \code{sys.ps2} in
Guido van Rossum7b3c8a11992-09-08 09:20:13 +0000318this file.
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000319
320If you want to read an additional start-up file from the current
321directory, you can program this in the global start-up file, e.g.
Fred Drakeeee08cd1997-12-04 15:43:15 +0000322\code{execfile('.pythonrc')}. If you want to use the startup file
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000323in a script, you must write this explicitly in the script, e.g.
Fred Drakeeee08cd1997-12-04 15:43:15 +0000324\code{import os;} \code{execfile(os.environ['PYTHONSTARTUP'])}.
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000325
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000326\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000327
328In the following examples, input and output are distinguished by the
Fred Drakeeee08cd1997-12-04 15:43:15 +0000329presence or absence of prompts (\code{>>>} and \code{...}): to repeat
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000330the example, you must type everything after the prompt, when the
331prompt appears; lines that do not begin with a prompt are output from
332the interpreter.%
Guido van Rossum02455691997-07-17 16:21:52 +0000333%\footnote{
334% I'd prefer to use different fonts to distinguish input
335% from output, but the amount of LaTeX hacking that would require
336% is currently beyond my ability.
337%}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000338Note that a secondary prompt on a line by itself in an example means
339you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000340
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000341\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000342
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000343Let's try some simple Python commands. Start the interpreter and wait
Fred Drakeeee08cd1997-12-04 15:43:15 +0000344for the primary prompt, \code{>>>}. (It shouldn't take long.)
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000345
346\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000347
348The interpreter acts as a simple calculator: you can type an
349expression at it and it will write the value. Expression syntax is
Fred Drakeeee08cd1997-12-04 15:43:15 +0000350straightforward: the operators \code{+}, \code{-}, \code{*} and \code{/}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000351work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000352can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000353
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000354\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000355>>> 2+2
3564
Guido van Rossum6938f061994-08-01 12:22:53 +0000357>>> # This is a comment
358... 2+2
3594
360>>> 2+2 # and a comment on the same line as code
3614
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000362>>> (50-5*6)/4
3635
Guido van Rossum6938f061994-08-01 12:22:53 +0000364>>> # Integer division returns the floor:
365... 7/3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00003662
Guido van Rossum6938f061994-08-01 12:22:53 +0000367>>> 7/-3
368-3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000369>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000370\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000371%
Fred Drakeeee08cd1997-12-04 15:43:15 +0000372Like in C, the equal sign (\code{=}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000373variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000374
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000375\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000376>>> width = 20
377>>> height = 5*9
378>>> width * height
379900
380>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000381\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000382%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000383A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000384
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000385\bcode\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000386>>> x = y = z = 0 # Zero x, y and z
387>>> x
3880
389>>> y
3900
391>>> z
3920
393>>>
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000394\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000395%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000396There is full support for floating point; operators with mixed type
397operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000398
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000399\bcode\begin{verbatim}
400>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004013.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000402>>> 7.0 / 2
4033.5
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000404\end{verbatim}\ecode
Guido van Rossum02455691997-07-17 16:21:52 +0000405%
406Complex numbers are also supported; imaginary numbers are written with
407a suffix of \code{'j'} or \code{'J'}. Complex numbers with a nonzero
408real component are written as \code{(\var{real}+\var{imag}j)}, or can
409be created with the \code{complex(\var{real}, \var{imag})} function.
410
411\bcode\begin{verbatim}
412>>> 1j * 1J
413(-1+0j)
414>>> 1j * complex(0,1)
415(-1+0j)
416>>> 3+1j*3
417(3+3j)
418>>> (3+1j)*3
419(9+3j)
420>>> (1+2j)/(1+1j)
421(1.5+0.5j)
422\end{verbatim}\ecode
423%
424Complex numbers are always represented as two floating point numbers,
425the real and imaginary part. To extract these parts from a complex
426number \code{z}, use \code{z.real} and \code{z.imag}.
427
428\bcode\begin{verbatim}
429>>> a=1.5+0.5j
430>>> a.real
4311.5
432>>> a.imag
4330.5
434\end{verbatim}\ecode
435%
436The conversion functions to floating point and integer
437(\code{float()}, \code{int()} and \code{long()}) don't work for
438complex numbers --- there is no one correct way to convert a complex
439number to a real number. Use \code{abs(z)} to get its magnitude (as a
440float) or \code{z.real} to get its real part.
441
442\bcode\begin{verbatim}
443>>> a=1.5+0.5j
444>>> float(a)
445Traceback (innermost last):
446 File "<stdin>", line 1, in ?
447TypeError: can't convert complex to float; use e.g. abs(z)
448>>> a.real
4491.5
450>>> abs(a)
4511.58113883008
452\end{verbatim}\ecode
453%
454In interactive mode, the last printed expression is assigned to the
455variable \code{_}. This means that when you are using Python as a
456desk calculator, it is somewhat easier to continue calculations, for
457example:
458
459\begin{verbatim}
460>>> tax = 17.5 / 100
461>>> price = 3.50
462>>> price * tax
4630.6125
464>>> price + _
4654.1125
466>>> round(_, 2)
4674.11
468\end{verbatim}
469
470This variable should be treated as read-only by the user. Don't
471explicitly assign a value to it --- you would create an independent
472local variable with the same name masking the built-in variable with
473its magic behavior.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000474
475\subsection{Strings}
476
Guido van Rossum02455691997-07-17 16:21:52 +0000477Besides numbers, Python can also manipulate strings, which can be
478expressed in several ways. They can be enclosed in single quotes or
479double quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000480
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000481\bcode\begin{verbatim}
Guido van Rossume5f8b601995-01-04 19:12:49 +0000482>>> 'spam eggs'
483'spam eggs'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000484>>> 'doesn\'t'
Guido van Rossum6938f061994-08-01 12:22:53 +0000485"doesn't"
486>>> "doesn't"
487"doesn't"
488>>> '"Yes," he said.'
489'"Yes," he said.'
490>>> "\"Yes,\" he said."
491'"Yes," he said.'
492>>> '"Isn\'t," she said.'
493'"Isn\'t," she said.'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000494>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000495\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000496%
Guido van Rossum02455691997-07-17 16:21:52 +0000497String literals can span multiple lines in several ways. Newlines can be escaped with backslashes, e.g.
498
499\begin{verbatim}
500hello = "This is a rather long string containing\n\
501several lines of text just as you would do in C.\n\
502 Note that whitespace at the beginning of the line is\
503 significant.\n"
504print hello
505\end{verbatim}
506
507which would print the following:
508\begin{verbatim}
509This is a rather long string containing
510several lines of text just as you would do in C.
511 Note that whitespace at the beginning of the line is significant.
512\end{verbatim}
513
514Or, strings can be surrounded in a pair of matching triple-quotes:
515\code{"""} or \code {'''}. End of lines do not need to be escaped
516when using triple-quotes, but they will be included in the string.
517
518\begin{verbatim}
519print """
520Usage: thingy [OPTIONS]
521 -h Display this usage message
522 -H hostname Hostname to connect to
523"""
524\end{verbatim}
525
526produces the following output:
527
528\bcode\begin{verbatim}
529Usage: thingy [OPTIONS]
530 -h Display this usage message
531 -H hostname Hostname to connect to
532\end{verbatim}\ecode
533%
534The interpreter prints the result of string operations in the same way
535as they are typed for input: inside quotes, and with quotes and other
536funny characters escaped by backslashes, to show the precise
537value. The string is enclosed in double quotes if the string contains
538a single quote and no double quotes, else it's enclosed in single
Fred Drakeeee08cd1997-12-04 15:43:15 +0000539quotes. (The \code{print} statement, described later, can be used to
Guido van Rossum02455691997-07-17 16:21:52 +0000540write strings without quotes or escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000541
Fred Drakeeee08cd1997-12-04 15:43:15 +0000542Strings can be concatenated (glued together) with the \code{+}
543operator, and repeated with \code{*}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000544
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000545\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000546>>> word = 'Help' + 'A'
547>>> word
548'HelpA'
549>>> '<' + word*5 + '>'
550'<HelpAHelpAHelpAHelpAHelpA>'
551>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000552\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000553%
Guido van Rossum02455691997-07-17 16:21:52 +0000554Two string literals next to each other are automatically concatenated;
555the first line above could also have been written \code{word = 'Help'
556'A'}; this only works with two literals, not with arbitrary string expressions.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000557
Guido van Rossum02455691997-07-17 16:21:52 +0000558Strings can be subscripted (indexed); like in C, the first character
559of a string has subscript (index) 0. There is no separate character
560type; a character is simply a string of size one. Like in Icon,
Fred Drakeeee08cd1997-12-04 15:43:15 +0000561substrings can be specified with the \emph{slice} notation: two indices
Guido van Rossum02455691997-07-17 16:21:52 +0000562separated by a colon.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000563
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000564\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000565>>> word[4]
566'A'
567>>> word[0:2]
568'He'
569>>> word[2:4]
570'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000571>>>
572\end{verbatim}\ecode
573%
574Slice indices have useful defaults; an omitted first index defaults to
575zero, an omitted second index defaults to the size of the string being
576sliced.
577
578\bcode\begin{verbatim}
579>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000580'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000581>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000582'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000583>>>
584\end{verbatim}\ecode
585%
Fred Drakeeee08cd1997-12-04 15:43:15 +0000586Here's a useful invariant of slice operations: \code{s[:i] + s[i:]}
587equals \code{s}.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000588
589\bcode\begin{verbatim}
590>>> word[:2] + word[2:]
591'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000592>>> word[:3] + word[3:]
593'HelpA'
594>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000595\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000596%
597Degenerate slice indices are handled gracefully: an index that is too
598large is replaced by the string size, an upper bound smaller than the
599lower bound returns an empty string.
600
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000601\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000602>>> word[1:100]
603'elpA'
604>>> word[10:]
605''
606>>> word[2:1]
607''
608>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000609\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000610%
611Indices may be negative numbers, to start counting from the right.
612For example:
613
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000614\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000615>>> word[-1] # The last character
616'A'
617>>> word[-2] # The last-but-one character
618'p'
619>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000620'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000621>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000622'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000623>>>
624\end{verbatim}\ecode
625%
626But note that -0 is really the same as 0, so it does not count from
627the right!
628
629\bcode\begin{verbatim}
630>>> word[-0] # (since -0 equals 0)
631'H'
632>>>
633\end{verbatim}\ecode
634%
635Out-of-range negative slice indices are truncated, but don't try this
636for single-element (non-slice) indices:
637
638\bcode\begin{verbatim}
639>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000640'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000641>>> word[-10] # error
Guido van Rossum6938f061994-08-01 12:22:53 +0000642Traceback (innermost last):
643 File "<stdin>", line 1
644IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000645>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000646\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000647%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000648The best way to remember how slices work is to think of the indices as
Fred Drakeeee08cd1997-12-04 15:43:15 +0000649pointing \emph{between} characters, with the left edge of the first
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000650character numbered 0. Then the right edge of the last character of a
Fred Drakeeee08cd1997-12-04 15:43:15 +0000651string of \var{n} characters has index \var{n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000652
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000653\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000654 +---+---+---+---+---+
655 | H | e | l | p | A |
656 +---+---+---+---+---+
657 0 1 2 3 4 5
658-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000659\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000660%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000661The first row of numbers gives the position of the indices 0...5 in
662the string; the second row gives the corresponding negative indices.
Fred Drakeeee08cd1997-12-04 15:43:15 +0000663The slice from \var{i} to \var{j} consists of all characters between
664the edges labeled \var{i} and \var{j}, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000665
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000666For nonnegative indices, the length of a slice is the difference of
667the indices, if both are within bounds, e.g., the length of
Fred Drakeeee08cd1997-12-04 15:43:15 +0000668\code{word[1:3]} is 2.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000669
Fred Drakeeee08cd1997-12-04 15:43:15 +0000670The built-in function \code{len()} returns the length of a string:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000671
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000672\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000673>>> s = 'supercalifragilisticexpialidocious'
674>>> len(s)
67534
676>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000677\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000678
679\subsection{Lists}
680
Fred Drakeeee08cd1997-12-04 15:43:15 +0000681Python knows a number of \emph{compound} data types, used to group
682together other values. The most versatile is the \emph{list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000683can be written as a list of comma-separated values (items) between
684square brackets. List items need not all have the same type.
685
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000686\bcode\begin{verbatim}
Guido van Rossume5f8b601995-01-04 19:12:49 +0000687>>> a = ['spam', 'eggs', 100, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000688>>> a
Guido van Rossume5f8b601995-01-04 19:12:49 +0000689['spam', 'eggs', 100, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000690>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000691\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000692%
693Like string indices, list indices start at 0, and lists can be sliced,
694concatenated and so on:
695
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000696\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000697>>> a[0]
Guido van Rossume5f8b601995-01-04 19:12:49 +0000698'spam'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000699>>> a[3]
7001234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000701>>> a[-2]
702100
703>>> a[1:-1]
Guido van Rossume5f8b601995-01-04 19:12:49 +0000704['eggs', 100]
705>>> a[:2] + ['bacon', 2*2]
706['spam', 'eggs', 'bacon', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000707>>> 3*a[:3] + ['Boe!']
Guido van Rossume5f8b601995-01-04 19:12:49 +0000708['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000709>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000710\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000711%
Fred Drakeeee08cd1997-12-04 15:43:15 +0000712Unlike strings, which are \emph{immutable}, it is possible to change
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000713individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000714
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000715\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000716>>> a
Guido van Rossume5f8b601995-01-04 19:12:49 +0000717['spam', 'eggs', 100, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000718>>> a[2] = a[2] + 23
719>>> a
Guido van Rossume5f8b601995-01-04 19:12:49 +0000720['spam', 'eggs', 123, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000721>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000722\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000723%
724Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000725of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000726
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000727\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000728>>> # Replace some items:
Guido van Rossum6938f061994-08-01 12:22:53 +0000729... a[0:2] = [1, 12]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000730>>> a
731[1, 12, 123, 1234]
732>>> # Remove some:
Guido van Rossum6938f061994-08-01 12:22:53 +0000733... a[0:2] = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000734>>> a
735[123, 1234]
736>>> # Insert some:
Guido van Rossum6938f061994-08-01 12:22:53 +0000737... a[1:1] = ['bletch', 'xyzzy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000738>>> a
739[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000740>>> a[:0] = a # Insert (a copy of) itself at the beginning
741>>> a
742[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000743>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000744\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000745%
Fred Drakeeee08cd1997-12-04 15:43:15 +0000746The built-in function \code{len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000747
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000748\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000749>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00007508
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000751>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000752\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000753%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000754It is possible to nest lists (create lists containing other lists),
755for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000756
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000757\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000758>>> q = [2, 3]
759>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000760>>> len(p)
7613
762>>> p[1]
763[2, 3]
764>>> p[1][0]
7652
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000766>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000767>>> p
768[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000769>>> q
770[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000771>>>
772\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000773%
Fred Drakeeee08cd1997-12-04 15:43:15 +0000774Note that in the last example, \code{p[1]} and \code{q} really refer to
775the same object! We'll come back to \emph{object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000776
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000777\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000778
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000779Of course, we can use Python for more complicated tasks than adding
780two and two together. For instance, we can write an initial
Fred Drakeeee08cd1997-12-04 15:43:15 +0000781subsequence of the \emph{Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000782
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000783\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000784>>> # Fibonacci series:
Guido van Rossum6938f061994-08-01 12:22:53 +0000785... # the sum of two elements defines the next
786... a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000787>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000788... print b
789... a, b = b, a+b
790...
7911
7921
7932
7943
7955
7968
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000797>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000798\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000799%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000800This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000801
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000802\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000803
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000804\item
Fred Drakeeee08cd1997-12-04 15:43:15 +0000805The first line contains a \emph{multiple assignment}: the variables
806\code{a} and \code{b} simultaneously get the new values 0 and 1. On the
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000807last line this is used again, demonstrating that the expressions on
808the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000809assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000810
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000811\item
Fred Drakeeee08cd1997-12-04 15:43:15 +0000812The \code{while} loop executes as long as the condition (here: \code{b <
Guido van Rossum16cd7f91994-10-06 10:29:26 +000081310}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000814true; zero is false. The condition may also be a string or list value,
815in fact any sequence; anything with a non-zero length is true, empty
816sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000817comparison. The standard comparison operators are written the same as
Fred Drakeeee08cd1997-12-04 15:43:15 +0000818in C: \code{<}, \code{>}, \code{==}, \code{<=}, \code{>=} and \code{!=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000819
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000820\item
Fred Drakeeee08cd1997-12-04 15:43:15 +0000821The \emph{body} of the loop is \emph{indented}: indentation is Python's
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000822way of grouping statements. Python does not (yet!) provide an
823intelligent input line editing facility, so you have to type a tab or
824space(s) for each indented line. In practice you will prepare more
825complicated input for Python with a text editor; most text editors have
826an auto-indent facility. When a compound statement is entered
827interactively, it must be followed by a blank line to indicate
828completion (since the parser cannot guess when you have typed the last
829line).
830
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000831\item
Fred Drakeeee08cd1997-12-04 15:43:15 +0000832The \code{print} statement writes the value of the expression(s) it is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000833given. It differs from just writing the expression you want to write
834(as we did earlier in the calculator examples) in the way it handles
Guido van Rossum16cd7f91994-10-06 10:29:26 +0000835multiple expressions and strings. Strings are printed without quotes,
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000836and a space is inserted between items, so you can format things nicely,
837like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000838
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000839\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000840>>> i = 256*256
841>>> print 'The value of i is', i
842The value of i is 65536
843>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000844\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000845%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000846A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000847
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000848\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000849>>> a, b = 0, 1
850>>> while b < 1000:
851... print b,
852... a, b = b, a+b
853...
8541 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
855>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000856\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000857%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000858Note that the interpreter inserts a newline before it prints the next
859prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000860
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000861\end{itemize}
862
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000863
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000864\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000865
Fred Drakeeee08cd1997-12-04 15:43:15 +0000866Besides the \code{while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000867usual control flow statements known from other languages, with some
868twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000869
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000870\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000871
Fred Drakeeee08cd1997-12-04 15:43:15 +0000872Perhaps the most well-known statement type is the \code{if} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000873For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000874
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000875\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000876>>> if x < 0:
877... x = 0
878... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000879... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000880... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000881... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000882... print 'Single'
883... else:
884... print 'More'
885...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000886\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000887%
Fred Drakeeee08cd1997-12-04 15:43:15 +0000888There can be zero or more \code{elif} parts, and the \code{else} part is
889optional. The keyword `\code{elif}' is short for `\code{else if}', and is
890useful to avoid excessive indentation. An \code{if...elif...elif...}
891sequence is a substitute for the \emph{switch} or \emph{case} statements
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000892found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000893
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000894\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000895
Fred Drakeeee08cd1997-12-04 15:43:15 +0000896The \code{for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000897used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000898arithmetic progression of numbers (like in Pascal), or leaving the user
Fred Drakeeee08cd1997-12-04 15:43:15 +0000899completely free in the iteration test and step (as C), Python's
900\code{for} statement iterates over the items of any sequence (e.g., a
901list or a string), in the order that they appear in the sequence. For
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000902example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000903
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000904\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000905>>> # Measure some strings:
Guido van Rossum6938f061994-08-01 12:22:53 +0000906... a = ['cat', 'window', 'defenestrate']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000907>>> for x in a:
908... print x, len(x)
909...
910cat 3
911window 6
912defenestrate 12
913>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000914\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000915%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000916It is not safe to modify the sequence being iterated over in the loop
917(this can only happen for mutable sequence types, i.e., lists). If
918you need to modify the list you are iterating over, e.g., duplicate
919selected items, you must iterate over a copy. The slice notation
920makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000921
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000922\bcode\begin{verbatim}
923>>> for x in a[:]: # make a slice copy of the entire list
924... if len(x) > 6: a.insert(0, x)
925...
926>>> a
927['defenestrate', 'cat', 'window', 'defenestrate']
928>>>
929\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000930
Fred Drakeeee08cd1997-12-04 15:43:15 +0000931\section{The \sectcode{range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000932
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000933If you do need to iterate over a sequence of numbers, the built-in
Fred Drakeeee08cd1997-12-04 15:43:15 +0000934function \code{range()} comes in handy. It generates lists containing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000935arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000936
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000937\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000938>>> range(10)
939[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
940>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000941\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000942%
Fred Drakeeee08cd1997-12-04 15:43:15 +0000943The given end point is never part of the generated list; \code{range(10)}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000944generates a list of 10 values, exactly the legal indices for items of a
945sequence of length 10. It is possible to let the range start at another
946number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000947
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000948\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000949>>> range(5, 10)
950[5, 6, 7, 8, 9]
951>>> range(0, 10, 3)
952[0, 3, 6, 9]
953>>> range(-10, -100, -30)
954[-10, -40, -70]
955>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000956\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000957%
Fred Drakeeee08cd1997-12-04 15:43:15 +0000958To iterate over the indices of a sequence, combine \code{range()} and
959\code{len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000960
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000961\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000962>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000963>>> for i in range(len(a)):
964... print i, a[i]
965...
9660 Mary
9671 had
9682 a
9693 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009704 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000971>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000972\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000973
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000974\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000975
Fred Drakeeee08cd1997-12-04 15:43:15 +0000976The \code{break} statement, like in C, breaks out of the smallest
977enclosing \code{for} or \code{while} loop.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000978
Fred Drakeeee08cd1997-12-04 15:43:15 +0000979The \code{continue} statement, also borrowed from C, continues with the
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000980next iteration of the loop.
981
Fred Drakeeee08cd1997-12-04 15:43:15 +0000982Loop statements may have an \code{else} clause; it is executed when the
983loop terminates through exhaustion of the list (with \code{for}) or when
984the condition becomes false (with \code{while}), but not when the loop is
985terminated by a \code{break} statement. This is exemplified by the
Guido van Rossumcfb45e41994-11-10 23:04:43 +0000986following loop, which searches for prime numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000987
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000988\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000989>>> for n in range(2, 10):
990... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000991... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000992... print n, 'equals', x, '*', n/x
993... break
994... else:
995... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000996...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009972 is a prime number
9983 is a prime number
9994 equals 2 * 2
10005 is a prime number
10016 equals 2 * 3
10027 is a prime number
10038 equals 2 * 4
10049 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001005>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001006\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001007
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001008\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001009
Fred Drakeeee08cd1997-12-04 15:43:15 +00001010The \code{pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001011It can be used when a statement is required syntactically but the
1012program requires no action.
1013For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001014
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001015\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001016>>> while 1:
1017... pass # Busy-wait for keyboard interrupt
1018...
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001019\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001020
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001021\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001022
1023We can create a function that writes the Fibonacci series to an
1024arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001025
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001026\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001027>>> def fib(n): # write Fibonacci series up to n
Guido van Rossum02455691997-07-17 16:21:52 +00001028... "Print a Fibonacci series up to n"
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001029... a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001030... while b < n:
Fred Drakeeee08cd1997-12-04 15:43:15 +00001031... print b,
1032... a, b = b, a+b
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001033...
1034>>> # Now call the function we just defined:
Guido van Rossum6938f061994-08-01 12:22:53 +00001035... fib(2000)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000010361 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
1037>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001038\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001039%
Fred Drakeeee08cd1997-12-04 15:43:15 +00001040The keyword \code{def} introduces a function \emph{definition}. It must
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001041be followed by the function name and the parenthesized list of formal
Guido van Rossum02455691997-07-17 16:21:52 +00001042parameters. The statements that form the body of the function start
1043at the next line, indented by a tab stop. The first statement of the
1044function body can optionally be a string literal; this string literal
1045is the function's documentation string, or \dfn{docstring}. There are
1046tools which use docstrings to automatically produce printed
1047documentation, or to let the user interactively browse through code;
1048it's good practice to include docstrings in code that you write, so
1049try to make a habit of it.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001050
Fred Drakeeee08cd1997-12-04 15:43:15 +00001051The \emph{execution} of a function introduces a new symbol table used
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001052for the local variables of the function. More precisely, all variable
1053assignments in a function store the value in the local symbol table;
Guido van Rossum02455691997-07-17 16:21:52 +00001054whereas variable references first look in the local symbol table, then
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001055in the global symbol table, and then in the table of built-in names.
Guido van Rossum02455691997-07-17 16:21:52 +00001056Thus,
Guido van Rossumcfb45e41994-11-10 23:04:43 +00001057global variables cannot be directly assigned a value within a
Fred Drakeeee08cd1997-12-04 15:43:15 +00001058function (unless named in a \code{global} statement), although
Guido van Rossum6938f061994-08-01 12:22:53 +00001059they may be referenced.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001060
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001061The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001062the local symbol table of the called function when it is called; thus,
Fred Drakeeee08cd1997-12-04 15:43:15 +00001063arguments are passed using \emph{call by value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001064\footnote{
Fred Drakeeee08cd1997-12-04 15:43:15 +00001065 Actually, \emph{call by object reference} would be a better
Guido van Rossum6938f061994-08-01 12:22:53 +00001066 description, since if a mutable object is passed, the caller
1067 will see any changes the callee makes to it (e.g., items
1068 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001069}
1070When a function calls another function, a new local symbol table is
1071created for that call.
1072
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001073A function definition introduces the function name in the
1074current
1075symbol table. The value
1076of the function name
1077has a type that is recognized by the interpreter as a user-defined
1078function. This value can be assigned to another name which can then
1079also be used as a function. This serves as a general renaming
1080mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001081
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001082\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001083>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001084<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001085>>> f = fib
1086>>> f(100)
10871 1 2 3 5 8 13 21 34 55 89
1088>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001089\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001090%
Fred Drakeeee08cd1997-12-04 15:43:15 +00001091You might object that \code{fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001092Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001093value. In fact, technically speaking, procedures do return a value,
Fred Drakeeee08cd1997-12-04 15:43:15 +00001094albeit a rather boring one. This value is called \code{None} (it's a
1095built-in name). Writing the value \code{None} is normally suppressed by
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001096the interpreter if it would be the only value written. You can see it
1097if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001098
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001099\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001100>>> print fib(0)
1101None
1102>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001103\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001104%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001105It is simple to write a function that returns a list of the numbers of
1106the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001107
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001108\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001109>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum02455691997-07-17 16:21:52 +00001110... "Return a list containing the Fibonacci series up to n"
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001111... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001112... a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001113... while b < n:
Fred Drakeeee08cd1997-12-04 15:43:15 +00001114... result.append(b) # see below
1115... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001116... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001117...
1118>>> f100 = fib2(100) # call it
1119>>> f100 # write the result
1120[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1121>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001122\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001123%
Guido van Rossum4410c751991-06-04 20:22:18 +00001124This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001125
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001126\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001127
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001128\item
Fred Drakeeee08cd1997-12-04 15:43:15 +00001129The \code{return} statement returns with a value from a function.
1130\code{return} without an expression argument is used to return from
1131the middle of a procedure (falling off the end also returns from a
1132procedure), in which case the \code{None} value is returned.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001133
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001134\item
Fred Drakeeee08cd1997-12-04 15:43:15 +00001135The statement \code{result.append(b)} calls a \emph{method} of the list
1136object \code{result}. A method is a function that `belongs' to an
1137object and is named \code{obj.methodname}, where \code{obj} is some
1138object (this may be an expression), and \code{methodname} is the name
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001139of a method that is defined by the object's type. Different types
1140define different methods. Methods of different types may have the
1141same name without causing ambiguity. (It is possible to define your
Fred Drakeeee08cd1997-12-04 15:43:15 +00001142own object types and methods, using \emph{classes}, as discussed later
Guido van Rossum6938f061994-08-01 12:22:53 +00001143in this tutorial.)
Fred Drakeeee08cd1997-12-04 15:43:15 +00001144The method \code{append} shown in the example, is defined for
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001145list objects; it adds a new element at the end of the list. In this
1146example
Fred Drakeeee08cd1997-12-04 15:43:15 +00001147it is equivalent to \code{result = result + [b]}, but more efficient.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001148
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001149\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001150
Guido van Rossum02455691997-07-17 16:21:52 +00001151\section{More on Defining Functions}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001152
Guido van Rossum02455691997-07-17 16:21:52 +00001153It is also possible to define functions with a variable number of
1154arguments. There are three forms, which can be combined.
1155
1156\subsection{Default Argument Values}
1157
1158The most useful form is to specify a default value for one or more
1159arguments. This creates a function that can be called with fewer
1160arguments than it is defined, e.g.
1161
1162\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001163 def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
1164 while 1:
1165 ok = raw_input(prompt)
1166 if ok in ('y', 'ye', 'yes'): return 1
1167 if ok in ('n', 'no', 'nop', 'nope'): return 0
1168 retries = retries - 1
1169 if retries < 0: raise IOError, 'refusenik user'
1170 print complaint
Guido van Rossum02455691997-07-17 16:21:52 +00001171\end{verbatim}
1172
1173This function can be called either like this:
Fred Drakeeee08cd1997-12-04 15:43:15 +00001174\code{ask_ok('Do you really want to quit?')} or like this:
1175\code{ask_ok('OK to overwrite the file?', 2)}.
Guido van Rossum02455691997-07-17 16:21:52 +00001176
1177The default values are evaluated at the point of function definition
Fred Drakeeee08cd1997-12-04 15:43:15 +00001178in the \emph{defining} scope, so that e.g.
Guido van Rossum02455691997-07-17 16:21:52 +00001179
1180\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001181 i = 5
1182 def f(arg = i): print arg
1183 i = 6
1184 f()
Guido van Rossum02455691997-07-17 16:21:52 +00001185\end{verbatim}
1186
Fred Drakeeee08cd1997-12-04 15:43:15 +00001187will print \code{5}.
Guido van Rossum02455691997-07-17 16:21:52 +00001188
1189\subsection{Keyword Arguments}
1190
1191Functions can also be called using
1192keyword arguments of the form \code{\var{keyword} = \var{value}}. For
1193instance, the following function:
1194
1195\begin{verbatim}
1196def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
1197 print "-- This parrot wouldn't", action,
1198 print "if you put", voltage, "Volts through it."
1199 print "-- Lovely plumage, the", type
1200 print "-- It's", state, "!"
1201\end{verbatim}
1202
1203could be called in any of the following ways:
1204
1205\begin{verbatim}
1206parrot(1000)
1207parrot(action = 'VOOOOOM', voltage = 1000000)
1208parrot('a thousand', state = 'pushing up the daisies')
1209parrot('a million', 'bereft of life', 'jump')
1210\end{verbatim}
1211
1212but the following calls would all be invalid:
1213
1214\begin{verbatim}
1215parrot() # required argument missing
1216parrot(voltage=5.0, 'dead') # non-keyword argument following keyword
1217parrot(110, voltage=220) # duplicate value for argument
1218parrot(actor='John Cleese') # unknown keyword
1219\end{verbatim}
1220
1221In general, an argument list must have any positional arguments
1222followed by any keyword arguments, where the keywords must be chosen
1223from the formal parameter names. It's not important whether a formal
1224parameter has a default value or not. No argument must receive a
1225value more than once --- formal parameter names corresponding to
1226positional arguments cannot be used as keywords in the same calls.
1227
1228When a final formal parameter of the form \code{**\var{name}} is
1229present, it receives a dictionary containing all keyword arguments
1230whose keyword doesn't correspond to a formal parameter. This may be
1231combined with a formal parameter of the form \code{*\var{name}}
1232(described in the next subsection) which receives a tuple containing
1233the positional arguments beyond the formal parameter list.
1234(\code{*\var{name}} must occur before \code{**\var{name}}.) For
1235example, if we define a function like this:
1236
1237\begin{verbatim}
1238def cheeseshop(kind, *arguments, **keywords):
1239 print "-- Do you have any", kind, '?'
1240 print "-- I'm sorry, we're all out of", kind
1241 for arg in arguments: print arg
1242 print '-'*40
1243 for kw in keywords.keys(): print kw, ':', keywords[kw]
1244\end{verbatim}
1245
1246It could be called like this:
1247
1248\begin{verbatim}
1249cheeseshop('Limburger', "It's very runny, sir.",
1250 "It's really very, VERY runny, sir.",
1251 client='John Cleese',
1252 shopkeeper='Michael Palin',
1253 sketch='Cheese Shop Sketch')
1254\end{verbatim}
1255
1256and of course it would print:
1257
1258\begin{verbatim}
1259-- Do you have any Limburger ?
1260-- I'm sorry, we're all out of Limburger
1261It's very runny, sir.
1262It's really very, VERY runny, sir.
1263----------------------------------------
1264client : John Cleese
1265shopkeeper : Michael Palin
1266sketch : Cheese Shop Sketch
1267\end{verbatim}
1268
1269\subsection{Arbitrary Argument Lists}
1270
1271Finally, the least frequently used option is to specify that a
1272function can be called with an arbitrary number of arguments. These
1273arguments will be wrapped up in a tuple. Before the variable number
1274of arguments, zero or more normal arguments may occur.
1275
1276\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001277 def fprintf(file, format, *args):
1278 file.write(format % args)
Guido van Rossum02455691997-07-17 16:21:52 +00001279\end{verbatim}
1280
1281\chapter{Data Structures}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001282
1283This chapter describes some things you've learned about already in
1284more detail, and adds some new things as well.
1285
1286\section{More on Lists}
1287
1288The list data type has some more methods. Here are all of the methods
1289of lists objects:
1290
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001291\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001292
Fred Drakeeee08cd1997-12-04 15:43:15 +00001293\item[\code{insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001294Insert an item at a given position. The first argument is the index of
Fred Drakeeee08cd1997-12-04 15:43:15 +00001295the element before which to insert, so \code{a.insert(0, x)} inserts at
1296the front of the list, and \code{a.insert(len(a), x)} is equivalent to
1297\code{a.append(x)}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001298
Fred Drakeeee08cd1997-12-04 15:43:15 +00001299\item[\code{append(x)}]
1300Equivalent to \code{a.insert(len(a), x)}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001301
Fred Drakeeee08cd1997-12-04 15:43:15 +00001302\item[\code{index(x)}]
1303Return the index in the list of the first item whose value is \code{x}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001304It is an error if there is no such item.
1305
Fred Drakeeee08cd1997-12-04 15:43:15 +00001306\item[\code{remove(x)}]
1307Remove the first item from the list whose value is \code{x}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001308It is an error if there is no such item.
1309
Fred Drakeeee08cd1997-12-04 15:43:15 +00001310\item[\code{sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001311Sort the items of the list, in place.
1312
Fred Drakeeee08cd1997-12-04 15:43:15 +00001313\item[\code{reverse()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001314Reverse the elements of the list, in place.
1315
Fred Drakeeee08cd1997-12-04 15:43:15 +00001316\item[\code{count(x)}]
1317Return the number of times \code{x} appears in the list.
Guido van Rossum6938f061994-08-01 12:22:53 +00001318
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001319\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001320
1321An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001322
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001323\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001324>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossum6938f061994-08-01 12:22:53 +00001325>>> print a.count(333), a.count(66.6), a.count('x')
13262 1 0
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001327>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001328>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001329>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001330[66.6, 333, -1, 333, 1, 1234.5, 333]
1331>>> a.index(333)
13321
1333>>> a.remove(333)
1334>>> a
1335[66.6, -1, 333, 1, 1234.5, 333]
1336>>> a.reverse()
1337>>> a
1338[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001339>>> a.sort()
1340>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001341[-1, 1, 66.6, 333, 333, 1234.5]
1342>>>
1343\end{verbatim}\ecode
1344
Guido van Rossum02455691997-07-17 16:21:52 +00001345\subsection{Functional Programming Tools}
1346
1347There are three built-in functions that are very useful when used with
Fred Drakeeee08cd1997-12-04 15:43:15 +00001348lists: \code{filter()}, \code{map()}, and \code{reduce()}.
Guido van Rossum02455691997-07-17 16:21:52 +00001349
Fred Drakeeee08cd1997-12-04 15:43:15 +00001350\code{filter(function, sequence)} returns a sequence (of the same
Guido van Rossum02455691997-07-17 16:21:52 +00001351type, if possible) consisting of those items from the sequence for
Fred Drakeeee08cd1997-12-04 15:43:15 +00001352which \code{function(item)} is true. For example, to compute some
Guido van Rossum02455691997-07-17 16:21:52 +00001353primes:
1354
1355\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001356 >>> def f(x): return x%2 != 0 and x%3 != 0
1357 ...
1358 >>> filter(f, range(2, 25))
1359 [5, 7, 11, 13, 17, 19, 23]
1360 >>>
Guido van Rossum02455691997-07-17 16:21:52 +00001361\end{verbatim}
1362
Fred Drakeeee08cd1997-12-04 15:43:15 +00001363\code{map(function, sequence)} calls \code{function(item)} for each of
Guido van Rossum02455691997-07-17 16:21:52 +00001364the sequence's items and returns a list of the return values. For
1365example, to compute some cubes:
1366
1367\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001368 >>> def cube(x): return x*x*x
1369 ...
1370 >>> map(cube, range(1, 11))
1371 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
1372 >>>
Guido van Rossum02455691997-07-17 16:21:52 +00001373\end{verbatim}
1374
1375More than one sequence may be passed; the function must then have as
1376many arguments as there are sequences and is called with the
1377corresponding item from each sequence (or \verb\None\ if some sequence
1378is shorter than another). If \verb\None\ is passed for the function,
1379a function returning its argument(s) is substituted.
1380
1381Combining these two special cases, we see that
1382\verb\map(None, list1, list2)\ is a convenient way of turning a pair
1383of lists into a list of pairs. For example:
1384
1385\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001386 >>> seq = range(8)
1387 >>> def square(x): return x*x
1388 ...
1389 >>> map(None, seq, map(square, seq))
1390 [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
1391 >>>
Guido van Rossum02455691997-07-17 16:21:52 +00001392\end{verbatim}
1393
1394\verb\reduce(func, sequence)\ returns a single value constructed
1395by calling the binary function \verb\func\ on the first two items of the
1396sequence, then on the result and the next item, and so on. For
1397example, to compute the sum of the numbers 1 through 10:
1398
1399\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001400 >>> def add(x,y): return x+y
1401 ...
1402 >>> reduce(add, range(1, 11))
1403 55
1404 >>>
Guido van Rossum02455691997-07-17 16:21:52 +00001405\end{verbatim}
1406
1407If there's only one item in the sequence, its value is returned; if
1408the sequence is empty, an exception is raised.
1409
1410A third argument can be passed to indicate the starting value. In this
1411case the starting value is returned for an empty sequence, and the
1412function is first applied to the starting value and the first sequence
1413item, then to the result and the next item, and so on. For example,
1414
1415\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001416 >>> def sum(seq):
1417 ... def add(x,y): return x+y
1418 ... return reduce(add, seq, 0)
1419 ...
1420 >>> sum(range(1, 11))
1421 55
1422 >>> sum([])
1423 0
1424 >>>
Guido van Rossum02455691997-07-17 16:21:52 +00001425\end{verbatim}
1426
Fred Drakeeee08cd1997-12-04 15:43:15 +00001427\section{The \sectcode{del} statement}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001428
1429There is a way to remove an item from a list given its index instead
Fred Drakeeee08cd1997-12-04 15:43:15 +00001430of its value: the \code{del} statement. This can also be used to
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001431remove slices from a list (which we did earlier by assignment of an
1432empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001433
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001434\bcode\begin{verbatim}
1435>>> a
1436[-1, 1, 66.6, 333, 333, 1234.5]
1437>>> del a[0]
1438>>> a
1439[1, 66.6, 333, 333, 1234.5]
1440>>> del a[2:4]
1441>>> a
1442[1, 66.6, 1234.5]
1443>>>
1444\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001445%
Fred Drakeeee08cd1997-12-04 15:43:15 +00001446\code{del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001447
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001448\bcode\begin{verbatim}
1449>>> del a
1450>>>
1451\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001452%
Fred Drakeeee08cd1997-12-04 15:43:15 +00001453Referencing the name \code{a} hereafter is an error (at least until
1454another value is assigned to it). We'll find other uses for \code{del}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001455later.
1456
1457\section{Tuples and Sequences}
1458
1459We saw that lists and strings have many common properties, e.g.,
Fred Drakeeee08cd1997-12-04 15:43:15 +00001460indexing and slicing operations. They are two examples of
1461\emph{sequence} data types. Since Python is an evolving language,
1462other sequence data types may be added. There is also another
1463standard sequence data type: the \emph{tuple}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001464
1465A tuple consists of a number of values separated by commas, for
1466instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001467
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001468\bcode\begin{verbatim}
1469>>> t = 12345, 54321, 'hello!'
1470>>> t[0]
147112345
1472>>> t
1473(12345, 54321, 'hello!')
1474>>> # Tuples may be nested:
Guido van Rossum6938f061994-08-01 12:22:53 +00001475... u = t, (1, 2, 3, 4, 5)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001476>>> u
1477((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1478>>>
1479\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001480%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001481As you see, on output tuples are alway enclosed in parentheses, so
1482that nested tuples are interpreted correctly; they may be input with
1483or without surrounding parentheses, although often parentheses are
1484necessary anyway (if the tuple is part of a larger expression).
1485
1486Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1487from a database, etc. Tuples, like strings, are immutable: it is not
1488possible to assign to the individual items of a tuple (you can
1489simulate much of the same effect with slicing and concatenation,
1490though).
1491
1492A special problem is the construction of tuples containing 0 or 1
Guido van Rossum6938f061994-08-01 12:22:53 +00001493items: the syntax has some extra quirks to accommodate these. Empty
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001494tuples are constructed by an empty pair of parentheses; a tuple with
1495one item is constructed by following a value with a comma
1496(it is not sufficient to enclose a single value in parentheses).
1497Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001498
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001499\bcode\begin{verbatim}
1500>>> empty = ()
1501>>> singleton = 'hello', # <-- note trailing comma
1502>>> len(empty)
15030
1504>>> len(singleton)
15051
1506>>> singleton
1507('hello',)
1508>>>
1509\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001510%
Fred Drakeeee08cd1997-12-04 15:43:15 +00001511The statement \code{t = 12345, 54321, 'hello!'} is an example of
1512\emph{tuple packing}: the values \code{12345}, \code{54321} and
1513\code{'hello!'} are packed together in a tuple. The reverse operation
1514is also possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001515
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001516\bcode\begin{verbatim}
1517>>> x, y, z = t
1518>>>
1519\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001520%
Fred Drakeeee08cd1997-12-04 15:43:15 +00001521This is called, appropriately enough, \emph{tuple unpacking}. Tuple
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001522unpacking requires that the list of variables on the left has the same
1523number of elements as the length of the tuple. Note that multiple
1524assignment is really just a combination of tuple packing and tuple
1525unpacking!
1526
Fred Drakeeee08cd1997-12-04 15:43:15 +00001527Occasionally, the corresponding operation on lists is useful: \emph{list
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001528unpacking}. This is supported by enclosing the list of variables in
1529square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001530
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001531\bcode\begin{verbatim}
Guido van Rossume5f8b601995-01-04 19:12:49 +00001532>>> a = ['spam', 'eggs', 100, 1234]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001533>>> [a1, a2, a3, a4] = a
1534>>>
1535\end{verbatim}\ecode
1536
1537\section{Dictionaries}
1538
Fred Drakeeee08cd1997-12-04 15:43:15 +00001539Another useful data type built into Python is the \emph{dictionary}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001540Dictionaries are sometimes found in other languages as ``associative
1541memories'' or ``associative arrays''. Unlike sequences, which are
Fred Drakeeee08cd1997-12-04 15:43:15 +00001542indexed by a range of numbers, dictionaries are indexed by \emph{keys},
Guido van Rossum02455691997-07-17 16:21:52 +00001543which can be any non-mutable type; strings and numbers can always be
1544keys. Tuples can be used as keys if they contain only strings,
1545numbers, or tuples. You can't use lists as keys, since lists can be
1546modified in place using their \code{append()} method.
1547
Guido van Rossum6938f061994-08-01 12:22:53 +00001548It is best to think of a dictionary as an unordered set of
Fred Drakeeee08cd1997-12-04 15:43:15 +00001549\emph{key:value} pairs, with the requirement that the keys are unique
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001550(within one dictionary).
Fred Drakeeee08cd1997-12-04 15:43:15 +00001551A pair of braces creates an empty dictionary: \code{\{\}}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001552Placing a comma-separated list of key:value pairs within the
1553braces adds initial key:value pairs to the dictionary; this is also the
1554way dictionaries are written on output.
1555
1556The main operations on a dictionary are storing a value with some key
1557and extracting the value given the key. It is also possible to delete
1558a key:value pair
Fred Drakeeee08cd1997-12-04 15:43:15 +00001559with \code{del}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001560If you store using a key that is already in use, the old value
1561associated with that key is forgotten. It is an error to extract a
Guido van Rossum6938f061994-08-01 12:22:53 +00001562value using a non-existent key.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001563
Fred Drakeeee08cd1997-12-04 15:43:15 +00001564The \code{keys()} method of a dictionary object returns a list of all the
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001565keys used in the dictionary, in random order (if you want it sorted,
Fred Drakeeee08cd1997-12-04 15:43:15 +00001566just apply the \code{sort()} method to the list of keys). To check
1567whether a single key is in the dictionary, use the \code{has_key()}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001568method of the dictionary.
1569
1570Here is a small example using a dictionary:
1571
1572\bcode\begin{verbatim}
1573>>> tel = {'jack': 4098, 'sape': 4139}
1574>>> tel['guido'] = 4127
1575>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001576{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001577>>> tel['jack']
15784098
1579>>> del tel['sape']
1580>>> tel['irv'] = 4127
1581>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001582{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001583>>> tel.keys()
1584['guido', 'irv', 'jack']
1585>>> tel.has_key('guido')
15861
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001587>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001588\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001589
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001590\section{More on Conditions}
1591
Fred Drakeeee08cd1997-12-04 15:43:15 +00001592The conditions used in \code{while} and \code{if} statements above can
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001593contain other operators besides comparisons.
1594
Fred Drakeeee08cd1997-12-04 15:43:15 +00001595The comparison operators \code{in} and \code{not in} check whether a value
1596occurs (does not occur) in a sequence. The operators \code{is} and
1597\code{is not} compare whether two objects are really the same object; this
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001598only matters for mutable objects like lists. All comparison operators
1599have the same priority, which is lower than that of all numerical
1600operators.
1601
Fred Drakeeee08cd1997-12-04 15:43:15 +00001602Comparisons can be chained: e.g., \code{a < b == c} tests whether \code{a}
1603is less than \code{b} and moreover \code{b} equals \code{c}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001604
Fred Drakeeee08cd1997-12-04 15:43:15 +00001605Comparisons may be combined by the Boolean operators \code{and} and
1606\code{or}, and the outcome of a comparison (or of any other Boolean
1607expression) may be negated with \code{not}. These all have lower
1608priorities than comparison operators again; between them, \code{not} has
1609the highest priority, and \code{or} the lowest, so that
1610\code{A and not B or C} is equivalent to \code{(A and (not B)) or C}. Of
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001611course, parentheses can be used to express the desired composition.
1612
Fred Drakeeee08cd1997-12-04 15:43:15 +00001613The Boolean operators \code{and} and \code{or} are so-called
1614\emph{shortcut} operators: their arguments are evaluated from left to
1615right, and evaluation stops as soon as the outcome is determined.
1616E.g., if \code{A} and \code{C} are true but \code{B} is false, \code{A
1617and B and C} does not evaluate the expression C. In general, the
1618return value of a shortcut operator, when used as a general value and
1619not as a Boolean, is the last evaluated argument.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001620
1621It is possible to assign the result of a comparison or other Boolean
Guido van Rossum6938f061994-08-01 12:22:53 +00001622expression to a variable. For example,
1623
1624\bcode\begin{verbatim}
1625>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
1626>>> non_null = string1 or string2 or string3
1627>>> non_null
1628'Trondheim'
1629>>>
1630\end{verbatim}\ecode
1631%
1632Note that in Python, unlike C, assignment cannot occur inside expressions.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001633
1634\section{Comparing Sequences and Other Types}
1635
1636Sequence objects may be compared to other objects with the same
Fred Drakeeee08cd1997-12-04 15:43:15 +00001637sequence type. The comparison uses \emph{lexicographical} ordering:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001638first the first two items are compared, and if they differ this
1639determines the outcome of the comparison; if they are equal, the next
1640two items are compared, and so on, until either sequence is exhausted.
1641If two items to be compared are themselves sequences of the same type,
Guido van Rossum6938f061994-08-01 12:22:53 +00001642the lexicographical comparison is carried out recursively. If all
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001643items of two sequences compare equal, the sequences are considered
1644equal. If one sequence is an initial subsequence of the other, the
1645shorted sequence is the smaller one. Lexicographical ordering for
Guido van Rossum47b4c0f1995-03-15 11:25:32 +00001646strings uses the \ASCII{} ordering for individual characters. Some
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001647examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001648
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001649\bcode\begin{verbatim}
1650(1, 2, 3) < (1, 2, 4)
1651[1, 2, 3] < [1, 2, 4]
1652'ABC' < 'C' < 'Pascal' < 'Python'
1653(1, 2, 3, 4) < (1, 2, 4)
1654(1, 2) < (1, 2, -1)
1655(1, 2, 3) = (1.0, 2.0, 3.0)
1656(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1657\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001658%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001659Note that comparing objects of different types is legal. The outcome
1660is deterministic but arbitrary: the types are ordered by their name.
1661Thus, a list is always smaller than a string, a string is always
1662smaller than a tuple, etc. Mixed numeric types are compared according
1663to their numeric value, so 0 equals 0.0, etc.%
1664\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001665 The rules for comparing objects of different types should
1666 not be relied upon; they may change in a future version of
1667 the language.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001668}
1669
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001670
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001671\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001672
Guido van Rossum4410c751991-06-04 20:22:18 +00001673If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001674definitions you have made (functions and variables) are lost.
1675Therefore, if you want to write a somewhat longer program, you are
1676better off using a text editor to prepare the input for the interpreter
Guido van Rossum16d6e711994-08-08 12:30:22 +00001677and running it with that file as input instead. This is known as creating a
Fred Drakeeee08cd1997-12-04 15:43:15 +00001678\emph{script}. As your program gets longer, you may want to split it
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001679into several files for easier maintenance. You may also want to use a
1680handy function that you've written in several programs without copying
1681its definition into each program.
1682
Guido van Rossum4410c751991-06-04 20:22:18 +00001683To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001684them in a script or in an interactive instance of the interpreter.
Fred Drakeeee08cd1997-12-04 15:43:15 +00001685Such a file is called a \emph{module}; definitions from a module can be
1686\emph{imported} into other modules or into the \emph{main} module (the
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001687collection of variables that you have access to in a script
1688executed at the top level
1689and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001690
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001691A module is a file containing Python definitions and statements. The
Fred Drakeeee08cd1997-12-04 15:43:15 +00001692file name is the module name with the suffix \file{.py} appended. Within
Guido van Rossum6938f061994-08-01 12:22:53 +00001693a module, the module's name (as a string) is available as the value of
Fred Drakeeee08cd1997-12-04 15:43:15 +00001694the global variable \code{__name__}. For instance, use your favorite text
1695editor to create a file called \file{fibo.py} in the current directory
Guido van Rossum6938f061994-08-01 12:22:53 +00001696with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001697
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001698\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001699# Fibonacci numbers module
1700
1701def fib(n): # write Fibonacci series up to n
1702 a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001703 while b < n:
Fred Drakeeee08cd1997-12-04 15:43:15 +00001704 print b,
1705 a, b = b, a+b
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001706
1707def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001708 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001709 a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001710 while b < n:
Fred Drakeeee08cd1997-12-04 15:43:15 +00001711 result.append(b)
1712 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001713 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001714\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001715%
Guido van Rossum4410c751991-06-04 20:22:18 +00001716Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001717following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001718
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001719\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001720>>> import fibo
1721>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001722\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001723%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001724This does not enter the names of the functions defined in
Fred Drakeeee08cd1997-12-04 15:43:15 +00001725\code{fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001726directly in the current symbol table; it only enters the module name
Fred Drakeeee08cd1997-12-04 15:43:15 +00001727\code{fibo}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001728there.
1729Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001730
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001731\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001732>>> fibo.fib(1000)
17331 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1734>>> fibo.fib2(100)
1735[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Guido van Rossum6938f061994-08-01 12:22:53 +00001736>>> fibo.__name__
1737'fibo'
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 +00001741If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001742
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001743\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001744>>> fib = fibo.fib
1745>>> fib(500)
17461 1 2 3 5 8 13 21 34 55 89 144 233 377
1747>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001748\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001749
Guido van Rossum02455691997-07-17 16:21:52 +00001750
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001751\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001752
1753A module can contain executable statements as well as function
1754definitions.
1755These statements are intended to initialize the module.
1756They are executed only the
Fred Drakeeee08cd1997-12-04 15:43:15 +00001757\emph{first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001758time the module is imported somewhere.%
1759\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001760 In fact function definitions are also `statements' that are
1761 `executed'; the execution enters the function name in the
1762 module's global symbol table.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001763}
1764
1765Each module has its own private symbol table, which is used as the
1766global symbol table by all functions defined in the module.
1767Thus, the author of a module can use global variables in the module
1768without worrying about accidental clashes with a user's global
1769variables.
1770On the other hand, if you know what you are doing you can touch a
1771module's global variables with the same notation used to refer to its
1772functions,
Fred Drakeeee08cd1997-12-04 15:43:15 +00001773\code{modname.itemname}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001774
1775Modules can import other modules.
1776It is customary but not required to place all
Fred Drakeeee08cd1997-12-04 15:43:15 +00001777\code{import}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001778statements at the beginning of a module (or script, for that matter).
1779The imported module names are placed in the importing module's global
1780symbol table.
1781
1782There is a variant of the
Fred Drakeeee08cd1997-12-04 15:43:15 +00001783\code{import}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001784statement that imports names from a module directly into the importing
1785module's symbol table.
1786For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001787
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001788\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001789>>> from fibo import fib, fib2
1790>>> fib(500)
17911 1 2 3 5 8 13 21 34 55 89 144 233 377
1792>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001793\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001794%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001795This does not introduce the module name from which the imports are taken
Fred Drakeeee08cd1997-12-04 15:43:15 +00001796in the local symbol table (so in the example, \code{fibo} is not
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001797defined).
1798
1799There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001800
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001801\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001802>>> from fibo import *
1803>>> fib(500)
18041 1 2 3 5 8 13 21 34 55 89 144 233 377
1805>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001806\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001807%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001808This imports all names except those beginning with an underscore
Fred Drakeeee08cd1997-12-04 15:43:15 +00001809(\code{_}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001810
Guido van Rossum02455691997-07-17 16:21:52 +00001811\subsection{The Module Search Path}
1812
Fred Drakeeee08cd1997-12-04 15:43:15 +00001813When a module named \code{spam} is imported, the interpreter searches
1814for a file named \file{spam.py} in the current directory,
Guido van Rossum02455691997-07-17 16:21:52 +00001815and then in the list of directories specified by
Fred Drakeeee08cd1997-12-04 15:43:15 +00001816the environment variable \code{PYTHONPATH}. This has the same syntax as
1817the \UNIX{} shell variable \code{PATH}, i.e., a list of colon-separated
1818directory names. When \code{PYTHONPATH} is not set, or when the file
Guido van Rossum02455691997-07-17 16:21:52 +00001819is not found there, the search continues in an installation-dependent
Fred Drakeeee08cd1997-12-04 15:43:15 +00001820default path, usually \code{.:/usr/local/lib/python}.
Guido van Rossum02455691997-07-17 16:21:52 +00001821
1822Actually, modules are searched in the list of directories given by the
Fred Drakeeee08cd1997-12-04 15:43:15 +00001823variable \code{sys.path} which is initialized from the directory
1824containing the input script (or the current directory),
1825\code{PYTHONPATH} and the installation-dependent default. This allows
Guido van Rossum02455691997-07-17 16:21:52 +00001826Python programs that know what they're doing to modify or replace the
1827module search path. See the section on Standard Modules later.
1828
1829\subsection{``Compiled'' Python files}
1830
1831As an important speed-up of the start-up time for short programs that
Fred Drakeeee08cd1997-12-04 15:43:15 +00001832use a lot of standard modules, if a file called \file{spam.pyc} exists
1833in the directory where \file{spam.py} is found, this is assumed to
1834contain an already-``compiled'' version of the module \code{spam}. The
1835modification time of the version of \file{spam.py} used to create
1836\file{spam.pyc} is recorded in \file{spam.pyc}, and the file is
1837ignored if these don't match.
Guido van Rossum02455691997-07-17 16:21:52 +00001838
Fred Drakeeee08cd1997-12-04 15:43:15 +00001839Normally, you don't need to do anything to create the \file{spam.pyc} file.
1840Whenever \file{spam.py} is successfully compiled, an attempt is made to
1841write the compiled version to \file{spam.pyc}. It is not an error if
Guido van Rossum02455691997-07-17 16:21:52 +00001842this attempt fails; if for any reason the file is not written
Fred Drakeeee08cd1997-12-04 15:43:15 +00001843completely, the resulting \file{spam.pyc} file will be recognized as
1844invalid and thus ignored later. The contents of the \file{spam.pyc}
Guido van Rossum02455691997-07-17 16:21:52 +00001845file is platform independent, so a Python module directory can be
1846shared by machines of different architectures. (Tip for experts:
Fred Drakeeee08cd1997-12-04 15:43:15 +00001847the module \code{compileall} creates file{.pyc} files for all modules.)
Guido van Rossum02455691997-07-17 16:21:52 +00001848
1849XXX Should optimization with -O be covered here?
1850
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001851\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001852
Guido van Rossum4410c751991-06-04 20:22:18 +00001853Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001854document (Python Library Reference). Some modules are built into the
1855interpreter; these provide access to operations that are not part of the
1856core of the language but are nevertheless built in, either for
1857efficiency or to provide access to operating system primitives such as
1858system calls. The set of such modules is a configuration option; e.g.,
Fred Drakeeee08cd1997-12-04 15:43:15 +00001859the \code{amoeba} module is only provided on systems that somehow support
1860Amoeba primitives. One particular module deserves some attention:
1861\code{sys}, which is built into every Python interpreter. The
1862variables \code{sys.ps1} and \code{sys.ps2} define the strings used as
1863primary and secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001864
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001865\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001866>>> import sys
1867>>> sys.ps1
1868'>>> '
1869>>> sys.ps2
1870'... '
1871>>> sys.ps1 = 'C> '
1872C> print 'Yuck!'
1873Yuck!
1874C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001875\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001876%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001877These two variables are only defined if the interpreter is in
1878interactive mode.
1879
1880The variable
Fred Drakeeee08cd1997-12-04 15:43:15 +00001881\code{sys.path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001882is a list of strings that determine the interpreter's search path for
1883modules.
1884It is initialized to a default path taken from the environment variable
Fred Drakeeee08cd1997-12-04 15:43:15 +00001885\code{PYTHONPATH},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001886or from a built-in default if
Fred Drakeeee08cd1997-12-04 15:43:15 +00001887\code{PYTHONPATH}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001888is not set.
1889You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001890
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001891\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001892>>> import sys
1893>>> sys.path.append('/ufs/guido/lib/python')
1894>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001895\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001896
Fred Drakeeee08cd1997-12-04 15:43:15 +00001897\section{The \sectcode{dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001898
Fred Drakeeee08cd1997-12-04 15:43:15 +00001899The built-in function \code{dir()} is used to find out which names a module
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001900defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001901
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001902\bcode\begin{verbatim}
1903>>> import fibo, sys
1904>>> dir(fibo)
Guido van Rossum6938f061994-08-01 12:22:53 +00001905['__name__', 'fib', 'fib2']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001906>>> dir(sys)
Guido van Rossum6938f061994-08-01 12:22:53 +00001907['__name__', 'argv', 'builtin_module_names', 'copyright', 'exit',
1908'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',
1909'stderr', 'stdin', 'stdout', 'version']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001910>>>
1911\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001912%
Fred Drakeeee08cd1997-12-04 15:43:15 +00001913Without arguments, \code{dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001914
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001915\bcode\begin{verbatim}
1916>>> a = [1, 2, 3, 4, 5]
1917>>> import fibo, sys
1918>>> fib = fibo.fib
1919>>> dir()
Guido van Rossum6938f061994-08-01 12:22:53 +00001920['__name__', 'a', 'fib', 'fibo', 'sys']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001921>>>
1922\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001923%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001924Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001925
Fred Drakeeee08cd1997-12-04 15:43:15 +00001926\code{dir()} does not list the names of built-in functions and variables.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001927If you want a list of those, they are defined in the standard module
Fred Drakeeee08cd1997-12-04 15:43:15 +00001928\code{__builtin__}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001929
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001930\bcode\begin{verbatim}
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001931>>> import __builtin__
1932>>> dir(__builtin__)
Guido van Rossum6938f061994-08-01 12:22:53 +00001933['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError',
1934'ImportError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
1935'MemoryError', 'NameError', 'None', 'OverflowError', 'RuntimeError',
1936'SyntaxError', 'SystemError', 'SystemExit', 'TypeError', 'ValueError',
1937'ZeroDivisionError', '__name__', 'abs', 'apply', 'chr', 'cmp', 'coerce',
1938'compile', 'dir', 'divmod', 'eval', 'execfile', 'filter', 'float',
1939'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'len', 'long',
1940'map', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input',
1941'reduce', 'reload', 'repr', 'round', 'setattr', 'str', 'type', 'xrange']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001942>>>
1943\end{verbatim}\ecode
1944
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001945
Guido van Rossum02455691997-07-17 16:21:52 +00001946\chapter{Input and Output}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001947
Guido van Rossum02455691997-07-17 16:21:52 +00001948There are several ways to present the output of a program; data can be
1949printed in a human-readable form, or written to a file for future use.
1950This chapter will discuss some of the possibilities.
1951
1952\section{Fancier Output Formatting}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001953So far we've encountered two ways of writing values: \emph{expression
1954statements} and the \code{print} statement. (A third way is using the
1955\code{write} method of file objects; the standard output file can be
1956referenced as \code{sys.stdout}. See the Library Reference for more
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001957information on this.)
1958
1959Often you'll want more control over the formatting of your output than
Guido van Rossum02455691997-07-17 16:21:52 +00001960simply printing space-separated values. There are two ways to format
1961your output; the first way is to do all the string handling yourself;
1962using string slicing and concatenation operations you can create any
Fred Drakeeee08cd1997-12-04 15:43:15 +00001963lay-out you can imagine. The standard module \code{string} contains
Guido van Rossum02455691997-07-17 16:21:52 +00001964some useful operations for padding strings to a given column width;
1965these will be discussed shortly. The second way is to use the
1966\code{\%} operator with a string as the left argument. \code{\%}
Fred Drakeeee08cd1997-12-04 15:43:15 +00001967interprets the left argument as a \C{} \code{sprintf()}-style format
Guido van Rossum02455691997-07-17 16:21:52 +00001968string to be applied to the right argument, and returns the string
1969resulting from this formatting operation.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001970
1971One question remains, of course: how do you convert values to strings?
Guido van Rossum02455691997-07-17 16:21:52 +00001972Luckily, Python has a way to convert any value to a string: pass it to
Fred Drakeeee08cd1997-12-04 15:43:15 +00001973the \code{repr()} function, or just write the value between reverse
1974quotes (\code{``}). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001975
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001976\bcode\begin{verbatim}
1977>>> x = 10 * 3.14
1978>>> y = 200*200
1979>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1980>>> print s
1981The value of x is 31.4, and y is 40000...
1982>>> # Reverse quotes work on other types besides numbers:
Guido van Rossum6938f061994-08-01 12:22:53 +00001983... p = [x, y]
Guido van Rossum02455691997-07-17 16:21:52 +00001984>>> ps = repr(p)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001985>>> ps
1986'[31.4, 40000]'
1987>>> # Converting a string adds string quotes and backslashes:
Guido van Rossum6938f061994-08-01 12:22:53 +00001988... hello = 'hello, world\n'
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001989>>> hellos = `hello`
1990>>> print hellos
1991'hello, world\012'
1992>>> # The argument of reverse quotes may be a tuple:
Guido van Rossume5f8b601995-01-04 19:12:49 +00001993... `x, y, ('spam', 'eggs')`
1994"(31.4, 40000, ('spam', 'eggs'))"
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001995>>>
1996\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001997%
Guido van Rossum6938f061994-08-01 12:22:53 +00001998Here are two ways to write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001999
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002000\bcode\begin{verbatim}
2001>>> import string
2002>>> for x in range(1, 11):
2003... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
2004... # Note trailing comma on previous line
2005... print string.rjust(`x*x*x`, 4)
2006...
2007 1 1 1
2008 2 4 8
2009 3 9 27
2010 4 16 64
2011 5 25 125
2012 6 36 216
2013 7 49 343
2014 8 64 512
2015 9 81 729
201610 100 1000
Guido van Rossum6938f061994-08-01 12:22:53 +00002017>>> for x in range(1,11):
2018... print '%2d %3d %4d' % (x, x*x, x*x*x)
2019...
2020 1 1 1
2021 2 4 8
2022 3 9 27
2023 4 16 64
2024 5 25 125
2025 6 36 216
2026 7 49 343
2027 8 64 512
2028 9 81 729
202910 100 1000
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002030>>>
2031\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002032%
Fred Drakeeee08cd1997-12-04 15:43:15 +00002033(Note that one space between each column was added by the way \code{print}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002034works: it always adds spaces between its arguments.)
2035
Fred Drakeeee08cd1997-12-04 15:43:15 +00002036This example demonstrates the function \code{string.rjust()}, which
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002037right-justifies a string in a field of a given width by padding it with
Fred Drakeeee08cd1997-12-04 15:43:15 +00002038spaces on the left. There are similar functions \code{string.ljust()}
2039and \code{string.center()}. These functions do not write anything, they
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002040just return a new string. If the input string is too long, they don't
2041truncate it, but return it unchanged; this will mess up your column
2042lay-out but that's usually better than the alternative, which would be
2043lying about a value. (If you really want truncation you can always add
Fred Drakeeee08cd1997-12-04 15:43:15 +00002044a slice operation, as in \code{string.ljust(x,~n)[0:n]}.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002045
Fred Drakeeee08cd1997-12-04 15:43:15 +00002046There is another function, \code{string.zfill()}, which pads a numeric
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002047string on the left with zeros. It understands about plus and minus
Guido van Rossum6938f061994-08-01 12:22:53 +00002048signs:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002049
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002050\bcode\begin{verbatim}
2051>>> string.zfill('12', 5)
2052'00012'
2053>>> string.zfill('-3.14', 7)
2054'-003.14'
2055>>> string.zfill('3.14159265359', 5)
2056'3.14159265359'
2057>>>
2058\end{verbatim}\ecode
Guido van Rossum02455691997-07-17 16:21:52 +00002059%
2060Using the \code{\%} operator looks like this:
2061
2062\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002063 >>> import math
2064 >>> print 'The value of PI is approximately %5.3f.' % math.pi
2065 The value of PI is approximately 3.142.
2066 >>>
Guido van Rossum02455691997-07-17 16:21:52 +00002067\end{verbatim}
2068
2069If there is more than one format in the string you pass a tuple as
2070right operand, e.g.
2071
2072\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002073 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
2074 >>> for name, phone in table.items():
2075 ... print '%-10s ==> %10d' % (name, phone)
2076 ...
2077 Jack ==> 4098
2078 Dcab ==> 8637678
2079 Sjoerd ==> 4127
2080 >>>
Guido van Rossum02455691997-07-17 16:21:52 +00002081\end{verbatim}
2082
2083Most formats work exactly as in C and require that you pass the proper
2084type; however, if you don't you get an exception, not a core dump.
2085The \verb\%s\ format is more relaxed: if the corresponding argument is
2086not a string object, it is converted to string using the \verb\str()\
2087built-in function. Using \verb\*\ to pass the width or precision in
2088as a separate (integer) argument is supported. The C formats
2089\verb\%n\ and \verb\%p\ are not supported.
2090
2091If you have a really long format string that you don't want to split
2092up, it would be nice if you could reference the variables to be
2093formatted by name instead of by position. This can be done by using
2094an extension of C formats using the form \verb\%(name)format\, e.g.
2095
2096\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002097 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
2098 >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table
2099 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
2100 >>>
Guido van Rossum02455691997-07-17 16:21:52 +00002101\end{verbatim}
2102
2103This is particularly useful in combination with the new built-in
2104\verb\vars()\ function, which returns a dictionary containing all
2105local variables.
2106
2107\section{Reading and Writing Files}
2108% Opening files
2109\code{open()} returns a file object, and is most commonly used with
2110two arguments: \code{open(\var{filename},\var{mode})}.
2111
2112\bcode\begin{verbatim}
2113>>> f=open('/tmp/workfile', 'w')
2114>>> print f
2115<open file '/tmp/workfile', mode 'w' at 80a0960>
2116\end{verbatim}\ecode
2117%
2118The first argument is a string containing the filename. The second
2119argument is another string containing a few characters describing the
2120way in which the file will be used. \var{mode} can be \code{'r'} when
2121the file will only be read, \code{'w'} for only writing (an existing
2122file with the same name will be erased), and \code{'a'} opens the file
2123for appending; any data written to the file is automatically added to
2124the end. \code{'r+'} opens the file for both reading and writing.
2125The \var{mode} argument is optional; \code{'r'} will be assumed if
2126it's omitted.
2127
2128On Windows, (XXX does the Mac need this too?) \code{'b'} appended to the
2129mode opens the file in binary mode, so there are also modes like
2130\code{'rb'}, \code{'wb'}, and \code{'r+b'}. Windows makes a
2131distinction between text and binary files; the end-of-line characters
2132in text files are automatically altered slightly when data is read or
2133written. This behind-the-scenes modification to file data is fine for
2134ASCII text files, but it'll corrupt binary data like that in JPEGs or
2135.EXE files. Be very careful to use binary mode when reading and
2136writing such files.
2137
2138\subsection{Methods of file objects}
2139
2140The rest of the examples in this section will assume that a file
2141object called \code{f} has already been created.
2142
2143To read a file's contents, call \code{f.read(\var{size})}, which reads
2144some quantity of data and returns it as a string. \var{size} is an
2145optional numeric argument. When \var{size} is omitted or negative,
2146the entire contents of the file will be read and returned; it's your
2147problem if the file is twice as large as your machine's memory.
2148Otherwise, at most \var{size} bytes are read and returned. If the end
2149of the file has been reached, \code{f.read()} will return an empty
2150string (\code {""}).
2151\bcode\begin{verbatim}
2152>>> f.read()
2153'This is the entire file.\012'
2154>>> f.read()
2155''
2156\end{verbatim}\ecode
2157%
2158\code{f.readline()} reads a single line from the file; a newline
Fred Drakeeee08cd1997-12-04 15:43:15 +00002159character (\code{\\n}) is left at the end of the string, and is only
Guido van Rossum02455691997-07-17 16:21:52 +00002160omitted on the last line of the file if the file doesn't end in a
2161newline. This makes the return value unambiguous; if
2162\code{f.readline()} returns an empty string, the end of the file has
Fred Drakeeee08cd1997-12-04 15:43:15 +00002163been reached, while a blank line is represented by \code{'\\n'}, a
Guido van Rossum02455691997-07-17 16:21:52 +00002164string containing only a single newline.
2165
2166\bcode\begin{verbatim}
2167>>> f.readline()
2168'This is the first line of the file.\012'
2169>>> f.readline()
2170'Second line of the file\012'
2171>>> f.readline()
2172''
2173\end{verbatim}\ecode
2174%
Fred Drakeeee08cd1997-12-04 15:43:15 +00002175\code{f.readlines()} uses \code{f.readline()} repeatedly, and returns
Guido van Rossum02455691997-07-17 16:21:52 +00002176a list containing all the lines of data in the file.
2177
2178\bcode\begin{verbatim}
2179>>> f.readlines()
2180['This is the first line of the file.\012', 'Second line of the file\012']
2181\end{verbatim}\ecode
2182%
2183\code{f.write(\var{string})} writes the contents of \var{string} to
2184the file, returning \code{None}.
2185
2186\bcode\begin{verbatim}
2187>>> f.write('This is a test\n')
2188\end{verbatim}\ecode
2189%
2190\code{f.tell()} returns an integer giving the file object's current
2191position in the file, measured in bytes from the beginning of the
2192file. To change the file object's position, use
2193\code{f.seek(\var{offset}, \var{from_what})}. The position is
2194computed from adding \var{offset} to a reference point; the reference
2195point is selected by the \var{from_what} argument. A \var{from_what}
2196value of 0 measures from the beginning of the file, 1 uses the current
2197file position, and 2 uses the end of the file as the reference point.
2198\var{from_what}
2199can be omitted and defaults to 0, using the beginning of the file as the reference point.
2200
2201\bcode\begin{verbatim}
2202>>> f=open('/tmp/workfile', 'r+')
2203>>> f.write('0123456789abcdef')
2204>>> f.seek(5) # Go to the 5th byte in the file
2205>>> f.read(1)
2206'5'
2207>>> f.seek(-3, 2) # Go to the 3rd byte before the end
2208>>> f.read(1)
2209'd'
2210\end{verbatim}\ecode
2211%
2212When you're done with a file, call \code{f.close()} to close it and
2213free up any system resources taken up by the open file. After calling
2214\code{f.close()}, attempts to use the file object will automatically fail.
2215
2216\bcode\begin{verbatim}
2217>>> f.close()
2218>>> f.read()
2219Traceback (innermost last):
2220 File "<stdin>", line 1, in ?
2221ValueError: I/O operation on closed file
2222\end{verbatim}\ecode
2223%
2224File objects have some additional methods, such as \code{isatty()} and
2225\code{truncate()} which are less frequently used; consult the Library
2226Reference for a complete guide to file objects.
2227
2228\subsection{The pickle module}
2229
2230Strings can easily be written to and read from a file. Numbers take a
2231bit more effort, since the \code{read()} method only returns strings,
2232which will have to be passed to a function like \code{string.atoi()},
2233which takes a string like \code{'123'} and returns its numeric value
2234123. However, when you want to save more complex data types like
2235lists, dictionaries, or class instances, things get a lot more
2236complicated.
2237
2238Rather than have users be constantly writing and debugging code to
2239save complicated data types, Python provides a standard module called
Fred Drake9e63faa1997-10-15 14:37:24 +00002240\code{pickle}. This is an amazing module that can take almost
Guido van Rossum02455691997-07-17 16:21:52 +00002241any Python object (even some forms of Python code!), and convert it to
2242a string representation; this process is called \dfn{pickling}.
2243Reconstructing the object from the string representation is called
2244\dfn{unpickling}. Between pickling and unpickling, the string
2245representing the object may have been stored in a file or data, or
2246sent over a network connection to some distant machine.
2247
2248If you have an object \code{x}, and a file object \code{f} that's been
2249opened for writing, the simplest way to pickle the object takes only
2250one line of code:
2251
2252\bcode\begin{verbatim}
2253pickle.dump(x, f)
2254\end{verbatim}\ecode
2255%
2256To unpickle the object again, if \code{f} is a file object which has been
2257opened for reading:
2258
2259\bcode\begin{verbatim}
2260x = pickle.load(f)
2261\end{verbatim}\ecode
2262%
2263(There are other variants of this, used when pickling many objects or
2264when you don't want to write the pickled data to a file; consult the
2265complete documentation for \code{pickle} in the Library Reference.)
2266
2267\code{pickle} is the standard way to make Python objects which can be
2268stored and reused by other programs or by a future invocation of the
2269same program; the technical term for this is a \dfn{persistent}
2270object. Because \code{pickle} is so widely used, many authors who
2271write Python extensions take care to ensure that new data types such
2272as matrices, XXX more examples needed XXX, can be properly pickled and
2273unpickled.
2274
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002275
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002276
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002277\chapter{Errors and Exceptions}
2278
2279Until now error messages haven't been more than mentioned, but if you
2280have tried out the examples you have probably seen some. There are
Fred Drakeeee08cd1997-12-04 15:43:15 +00002281(at least) two distinguishable kinds of errors: \emph{syntax errors}
2282and \emph{exceptions}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002283
2284\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002285
2286Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00002287kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002288
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002289\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002290>>> while 1 print 'Hello world'
Guido van Rossum6938f061994-08-01 12:22:53 +00002291 File "<stdin>", line 1
2292 while 1 print 'Hello world'
2293 ^
2294SyntaxError: invalid syntax
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002295>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002296\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002297%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002298The parser repeats the offending line and displays a little `arrow'
2299pointing at the earliest point in the line where the error was detected.
2300The error is caused by (or at least detected at) the token
Fred Drakeeee08cd1997-12-04 15:43:15 +00002301\emph{preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002302the arrow: in the example, the error is detected at the keyword
Fred Drakeeee08cd1997-12-04 15:43:15 +00002303\code{print}, since a colon (\code{:}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002304File name and line number are printed so you know where to look in case
2305the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002306
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002307\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002308
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002309Even if a statement or expression is syntactically correct, it may
2310cause an error when an attempt is made to execute it.
Fred Drakeeee08cd1997-12-04 15:43:15 +00002311Errors detected during execution are called \emph{exceptions} and are
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002312not unconditionally fatal: you will soon learn how to handle them in
2313Python programs. Most exceptions are not handled by programs,
2314however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002315
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002316\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002317>>> 10 * (1/0)
Guido van Rossum3cbc16d1993-12-17 12:13:53 +00002318Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002319 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00002320ZeroDivisionError: integer division or modulo
Guido van Rossume5f8b601995-01-04 19:12:49 +00002321>>> 4 + spam*3
Guido van Rossum6938f061994-08-01 12:22:53 +00002322Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002323 File "<stdin>", line 1
Guido van Rossume5f8b601995-01-04 19:12:49 +00002324NameError: spam
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002325>>> '2' + 2
Guido van Rossum6938f061994-08-01 12:22:53 +00002326Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002327 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00002328TypeError: illegal argument type for built-in operation
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002329>>>
Fred Drakefd255e41996-10-29 15:50:05 +00002330\end{verbatim}\normalsize\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002331%
Guido van Rossumb2c65561993-05-12 08:53:36 +00002332The last line of the error message indicates what happened.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002333Exceptions come in different types, and the type is printed as part of
2334the message: the types in the example are
Fred Drakeeee08cd1997-12-04 15:43:15 +00002335\code{ZeroDivisionError},
2336\code{NameError}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002337and
Fred Drakeeee08cd1997-12-04 15:43:15 +00002338\code{TypeError}.
Guido van Rossumb2c65561993-05-12 08:53:36 +00002339The string printed as the exception type is the name of the built-in
2340name for the exception that occurred. This is true for all built-in
2341exceptions, but need not be true for user-defined exceptions (although
2342it is a useful convention).
2343Standard exception names are built-in identifiers (not reserved
2344keywords).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002345
Guido van Rossumb2c65561993-05-12 08:53:36 +00002346The rest of the line is a detail whose interpretation depends on the
2347exception type; its meaning is dependent on the exception type.
2348
2349The preceding part of the error message shows the context where the
2350exception happened, in the form of a stack backtrace.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002351In general it contains a stack backtrace listing source lines; however,
2352it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002353
Guido van Rossum6a05f951996-10-22 19:27:46 +00002354The Python Library Reference Manual lists the built-in exceptions and
Guido van Rossumb2c65561993-05-12 08:53:36 +00002355their meanings.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002356
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002357\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002358
2359It is possible to write programs that handle selected exceptions.
2360Look at the following example, which prints a table of inverses of
2361some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002362
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002363\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002364>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002365>>> for x in numbers:
2366... print x,
2367... try:
2368... print 1.0 / x
Guido van Rossumb2c65561993-05-12 08:53:36 +00002369... except ZeroDivisionError:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002370... print '*** has no inverse ***'
Guido van Rossum02455691997-07-17 16:21:52 +00002371...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000023720.3333 3.00030003
23732.5 0.4
23740 *** has no inverse ***
237510 0.1
2376>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002377\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002378%
Fred Drakeeee08cd1997-12-04 15:43:15 +00002379The \code{try} statement works as follows.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002380\begin{itemize}
2381\item
2382First, the
Fred Drakeeee08cd1997-12-04 15:43:15 +00002383\emph{try\ clause}
2384(the statement(s) between the \code{try} and \code{except} keywords) is
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002385executed.
2386\item
2387If no exception occurs, the
Fred Drakeeee08cd1997-12-04 15:43:15 +00002388\emph{except\ clause}
2389is skipped and execution of the \code{try} statement is finished.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002390\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002391If an exception occurs during execution of the try clause,
2392the rest of the clause is skipped. Then if
Fred Drakeeee08cd1997-12-04 15:43:15 +00002393its type matches the exception named after the \code{except} keyword,
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002394the rest of the try clause is skipped, the except clause is executed,
Fred Drakeeee08cd1997-12-04 15:43:15 +00002395and then execution continues after the \code{try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002396\item
2397If an exception occurs which does not match the exception named in the
2398except clause, it is passed on to outer try statements; if no handler is
2399found, it is an
Fred Drakeeee08cd1997-12-04 15:43:15 +00002400\emph{unhandled exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002401and execution stops with a message as shown above.
2402\end{itemize}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002403A \code{try} statement may have more than one except clause, to specify
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002404handlers for different exceptions.
2405At most one handler will be executed.
2406Handlers only handle exceptions that occur in the corresponding try
Fred Drakeeee08cd1997-12-04 15:43:15 +00002407clause, not in other handlers of the same \code{try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002408An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002409e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002410
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002411\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002412... except (RuntimeError, TypeError, NameError):
2413... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002414\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002415%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002416The last except clause may omit the exception name(s), to serve as a
2417wildcard.
Guido van Rossumb2c65561993-05-12 08:53:36 +00002418Use this with extreme caution, since it is easy to mask a real
2419programming error in this way!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002420
Guido van Rossum02455691997-07-17 16:21:52 +00002421The \verb\try...except\ statement has an optional \verb\else\ clause,
2422which must follow all \verb\except\ clauses. It is useful to place
2423code that must be executed if the \verb\try\ clause does not raise an
2424exception. For example:
2425
2426\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002427 for arg in sys.argv:
2428 try:
2429 f = open(arg, 'r')
2430 except IOError:
2431 print 'cannot open', arg
2432 else:
2433 print arg, 'has', len(f.readlines()), 'lines'
2434 f.close()
Guido van Rossum02455691997-07-17 16:21:52 +00002435\end{verbatim}
2436
2437
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002438When an exception occurs, it may have an associated value, also known as
2439the exceptions's
Fred Drakeeee08cd1997-12-04 15:43:15 +00002440\emph{argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002441The presence and type of the argument depend on the exception type.
2442For exception types which have an argument, the except clause may
2443specify a variable after the exception name (or list) to receive the
2444argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002445
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002446\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002447>>> try:
Guido van Rossume5f8b601995-01-04 19:12:49 +00002448... spam()
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002449... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002450... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002451...
Guido van Rossume5f8b601995-01-04 19:12:49 +00002452name spam undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002453>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002454\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002455%
Guido van Rossumb2c65561993-05-12 08:53:36 +00002456If an exception has an argument, it is printed as the last part
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002457(`detail') of the message for unhandled exceptions.
2458
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002459Exception handlers don't just handle exceptions if they occur
2460immediately in the try clause, but also if they occur inside functions
2461that are called (even indirectly) in the try clause.
2462For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002463
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002464\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002465>>> def this_fails():
2466... x = 1/0
2467...
2468>>> try:
2469... this_fails()
Guido van Rossumb2c65561993-05-12 08:53:36 +00002470... except ZeroDivisionError, detail:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002471... print 'Handling run-time error:', detail
2472...
Guido van Rossumb2c65561993-05-12 08:53:36 +00002473Handling run-time error: integer division or modulo
Guido van Rossum02455691997-07-17 16:21:52 +00002474>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002475\end{verbatim}\ecode
Guido van Rossum02455691997-07-17 16:21:52 +00002476%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002477
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002478\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002479
Fred Drakeeee08cd1997-12-04 15:43:15 +00002480The \code{raise} statement allows the programmer to force a specified
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002481exception to occur.
2482For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002483
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002484\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00002485>>> raise NameError, 'HiThere'
Guido van Rossum6938f061994-08-01 12:22:53 +00002486Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002487 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00002488NameError: HiThere
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002489>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002490\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002491%
Fred Drakeeee08cd1997-12-04 15:43:15 +00002492The first argument to \code{raise} names the exception to be raised.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002493The optional second argument specifies the exception's argument.
2494
Guido van Rossum02455691997-07-17 16:21:52 +00002495%
2496
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002497\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002498
2499Programs may name their own exceptions by assigning a string to a
2500variable.
2501For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002502
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002503\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00002504>>> my_exc = 'my_exc'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002505>>> try:
2506... raise my_exc, 2*2
2507... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00002508... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002509...
Guido van Rossum6938f061994-08-01 12:22:53 +00002510My exception occurred, value: 4
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002511>>> raise my_exc, 1
Guido van Rossum6938f061994-08-01 12:22:53 +00002512Traceback (innermost last):
2513 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00002514my_exc: 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002515>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002516\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002517%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002518Many standard modules use this to report errors that may occur in
2519functions they define.
2520
Guido van Rossum02455691997-07-17 16:21:52 +00002521%
2522
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002523\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002524
Fred Drakeeee08cd1997-12-04 15:43:15 +00002525The \code{try} statement has another optional clause which is intended to
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002526define clean-up actions that must be executed under all circumstances.
2527For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002528
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002529\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002530>>> try:
2531... raise KeyboardInterrupt
2532... finally:
2533... print 'Goodbye, world!'
2534...
2535Goodbye, world!
Guido van Rossum6938f061994-08-01 12:22:53 +00002536Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002537 File "<stdin>", line 2
Guido van Rossumb2c65561993-05-12 08:53:36 +00002538KeyboardInterrupt
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002539>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002540\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002541%
Fred Drakeeee08cd1997-12-04 15:43:15 +00002542A \code{finally} clause is executed whether or not an exception has
2543occurred in the \code{try} clause. When an exception has occurred, it
2544is re-raised after the \code{finally} clause is executed. The
2545\code{finally} clause is also executed ``on the way out'' when the
2546\code{try} statement is left via a \code{break} or \code{return}
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00002547statement.
2548
Fred Drakeeee08cd1997-12-04 15:43:15 +00002549A \code{try} statement must either have one or more \code{except}
2550clauses or one \code{finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002551
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002552\chapter{Classes}
2553
2554Python's class mechanism adds classes to the language with a minimum
2555of new syntax and semantics. It is a mixture of the class mechanisms
Guido van Rossum16d6e711994-08-08 12:30:22 +00002556found in \Cpp{} and Modula-3. As is true for modules, classes in Python
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002557do not put an absolute barrier between definition and user, but rather
2558rely on the politeness of the user not to ``break into the
2559definition.'' The most important features of classes are retained
2560with full power, however: the class inheritance mechanism allows
2561multiple base classes, a derived class can override any methods of its
2562base class(es), a method can call the method of a base class with the
2563same name. Objects can contain an arbitrary amount of private data.
2564
Guido van Rossum16d6e711994-08-08 12:30:22 +00002565In \Cpp{} terminology, all class members (including the data members) are
Fred Drakeeee08cd1997-12-04 15:43:15 +00002566\emph{public}, and all member functions are \emph{virtual}. There are
Guido van Rossum6938f061994-08-01 12:22:53 +00002567no special constructors or destructors. As in Modula-3, there are no
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002568shorthands for referencing the object's members from its methods: the
2569method function is declared with an explicit first argument
2570representing the object, which is provided implicitly by the call. As
2571in Smalltalk, classes themselves are objects, albeit in the wider
2572sense of the word: in Python, all data types are objects. This
Guido van Rossum16d6e711994-08-08 12:30:22 +00002573provides semantics for importing and renaming. But, just like in \Cpp{}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002574or Modula-3, built-in types cannot be used as base classes for
Guido van Rossum16d6e711994-08-08 12:30:22 +00002575extension by the user. Also, like in \Cpp{} but unlike in Modula-3, most
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002576built-in operators with special syntax (arithmetic operators,
Guido van Rossum6938f061994-08-01 12:22:53 +00002577subscripting etc.) can be redefined for class members.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002578
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002579\section{A word about terminology}
2580
2581Lacking universally accepted terminology to talk about classes, I'll
Guido van Rossum16d6e711994-08-08 12:30:22 +00002582make occasional use of Smalltalk and \Cpp{} terms. (I'd use Modula-3
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002583terms, since its object-oriented semantics are closer to those of
Guido van Rossum16d6e711994-08-08 12:30:22 +00002584Python than \Cpp{}, but I expect that few readers have heard of it...)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002585
2586I also have to warn you that there's a terminological pitfall for
2587object-oriented readers: the word ``object'' in Python does not
Guido van Rossum16d6e711994-08-08 12:30:22 +00002588necessarily mean a class instance. Like \Cpp{} and Modula-3, and unlike
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002589Smalltalk, not all types in Python are classes: the basic built-in
2590types like integers and lists aren't, and even somewhat more exotic
Fred Drakeeee08cd1997-12-04 15:43:15 +00002591types like files aren't. However, \emph{all} Python types share a little
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002592bit of common semantics that is best described by using the word
2593object.
2594
2595Objects have individuality, and multiple names (in multiple scopes)
2596can be bound to the same object. This is known as aliasing in other
2597languages. This is usually not appreciated on a first glance at
2598Python, and can be safely ignored when dealing with immutable basic
2599types (numbers, strings, tuples). However, aliasing has an
Guido van Rossum6938f061994-08-01 12:22:53 +00002600(intended!) effect on the semantics of Python code involving mutable
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002601objects such as lists, dictionaries, and most types representing
2602entities outside the program (files, windows, etc.). This is usually
2603used to the benefit of the program, since aliases behave like pointers
2604in some respects. For example, passing an object is cheap since only
2605a pointer is passed by the implementation; and if a function modifies
2606an object passed as an argument, the caller will see the change --- this
2607obviates the need for two different argument passing mechanisms as in
2608Pascal.
2609
2610
2611\section{Python scopes and name spaces}
2612
2613Before introducing classes, I first have to tell you something about
2614Python's scope rules. Class definitions play some neat tricks with
2615name spaces, and you need to know how scopes and name spaces work to
2616fully understand what's going on. Incidentally, knowledge about this
2617subject is useful for any advanced Python programmer.
2618
2619Let's begin with some definitions.
2620
Fred Drakeeee08cd1997-12-04 15:43:15 +00002621A \emph{name space} is a mapping from names to objects. Most name
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002622spaces are currently implemented as Python dictionaries, but that's
2623normally not noticeable in any way (except for performance), and it
2624may change in the future. Examples of name spaces are: the set of
2625built-in names (functions such as \verb\abs()\, and built-in exception
2626names); the global names in a module; and the local names in a
2627function invocation. In a sense the set of attributes of an object
Guido van Rossum16cd7f91994-10-06 10:29:26 +00002628also form a name space. The important thing to know about name
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002629spaces is that there is absolutely no relation between names in
2630different name spaces; for instance, two different modules may both
2631define a function ``maximize'' without confusion --- users of the
2632modules must prefix it with the module name.
2633
Fred Drakeeee08cd1997-12-04 15:43:15 +00002634By the way, I use the word \emph{attribute} for any name following a
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002635dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2636an attribute of the object \verb\z\. Strictly speaking, references to
2637names in modules are attribute references: in the expression
2638\verb\modname.funcname\, \verb\modname\ is a module object and
2639\verb\funcname\ is an attribute of it. In this case there happens to
2640be a straightforward mapping between the module's attributes and the
2641global names defined in the module: they share the same name space!%
2642\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00002643 Except for one thing. Module objects have a secret read-only
Fred Drakeeee08cd1997-12-04 15:43:15 +00002644 attribute called \code{__dict__} which returns the dictionary
Guido van Rossum6938f061994-08-01 12:22:53 +00002645 used to implement the module's name space; the name
Fred Drakeeee08cd1997-12-04 15:43:15 +00002646 \code{__dict__} is an attribute but not a global name.
Guido van Rossum6938f061994-08-01 12:22:53 +00002647 Obviously, using this violates the abstraction of name space
2648 implementation, and should be restricted to things like
2649 post-mortem debuggers...
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002650}
2651
2652Attributes may be read-only or writable. In the latter case,
2653assignment to attributes is possible. Module attributes are writable:
2654you can write \verb\modname.the_answer = 42\. Writable attributes may
2655also be deleted with the del statement, e.g.
2656\verb\del modname.the_answer\.
2657
2658Name spaces are created at different moments and have different
2659lifetimes. The name space containing the built-in names is created
2660when the Python interpreter starts up, and is never deleted. The
2661global name space for a module is created when the module definition
2662is read in; normally, module name spaces also last until the
2663interpreter quits. The statements executed by the top-level
2664invocation of the interpreter, either read from a script file or
2665interactively, are considered part of a module called \verb\__main__\,
2666so they have their own global name space. (The built-in names
Guido van Rossum4bd023f1993-10-27 13:49:20 +00002667actually also live in a module; this is called \verb\__builtin__\.)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002668
2669The local name space for a function is created when the function is
2670called, and deleted when the function returns or raises an exception
2671that is not handled within the function. (Actually, forgetting would
2672be a better way to describe what actually happens.) Of course,
2673recursive invocations each have their own local name space.
2674
Fred Drakeeee08cd1997-12-04 15:43:15 +00002675A \emph{scope} is a textual region of a Python program where a name space
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002676is directly accessible. ``Directly accessible'' here means that an
2677unqualified reference to a name attempts to find the name in the name
2678space.
2679
2680Although scopes are determined statically, they are used dynamically.
2681At any time during execution, exactly three nested scopes are in use
2682(i.e., exactly three name spaces are directly accessible): the
2683innermost scope, which is searched first, contains the local names,
2684the middle scope, searched next, contains the current module's global
2685names, and the outermost scope (searched last) is the name space
2686containing built-in names.
2687
2688Usually, the local scope references the local names of the (textually)
Guido van Rossum96628a91995-04-10 11:34:00 +00002689current function. Outside of functions, the local scope references
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002690the same name space as the global scope: the module's name space.
2691Class definitions place yet another name space in the local scope.
2692
2693It is important to realize that scopes are determined textually: the
2694global scope of a function defined in a module is that module's name
2695space, no matter from where or by what alias the function is called.
2696On the other hand, the actual search for names is done dynamically, at
Guido van Rossum96628a91995-04-10 11:34:00 +00002697run time --- however, the language definition is evolving towards
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002698static name resolution, at ``compile'' time, so don't rely on dynamic
2699name resolution! (In fact, local variables are already determined
2700statically.)
2701
2702A special quirk of Python is that assignments always go into the
2703innermost scope. Assignments do not copy data --- they just
2704bind names to objects. The same is true for deletions: the statement
2705\verb\del x\ removes the binding of x from the name space referenced by the
2706local scope. In fact, all operations that introduce new names use the
2707local scope: in particular, import statements and function definitions
2708bind the module or function name in the local scope. (The
2709\verb\global\ statement can be used to indicate that particular
2710variables live in the global scope.)
2711
2712
2713\section{A first look at classes}
2714
2715Classes introduce a little bit of new syntax, three new object types,
2716and some new semantics.
2717
2718
2719\subsection{Class definition syntax}
2720
2721The simplest form of class definition looks like this:
2722
2723\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002724 class ClassName:
2725 <statement-1>
2726 .
2727 .
2728 .
2729 <statement-N>
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002730\end{verbatim}
2731
2732Class definitions, like function definitions (\verb\def\ statements)
2733must be executed before they have any effect. (You could conceivably
2734place a class definition in a branch of an \verb\if\ statement, or
2735inside a function.)
2736
2737In practice, the statements inside a class definition will usually be
2738function definitions, but other statements are allowed, and sometimes
2739useful --- we'll come back to this later. The function definitions
2740inside a class normally have a peculiar form of argument list,
2741dictated by the calling conventions for methods --- again, this is
2742explained later.
2743
2744When a class definition is entered, a new name space is created, and
2745used as the local scope --- thus, all assignments to local variables
2746go into this new name space. In particular, function definitions bind
2747the name of the new function here.
2748
Fred Drakeeee08cd1997-12-04 15:43:15 +00002749When a class definition is left normally (via the end), a \emph{class
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002750object} is created. This is basically a wrapper around the contents
2751of the name space created by the class definition; we'll learn more
2752about class objects in the next section. The original local scope
2753(the one in effect just before the class definitions was entered) is
2754reinstated, and the class object is bound here to class name given in
2755the class definition header (ClassName in the example).
2756
2757
2758\subsection{Class objects}
2759
2760Class objects support two kinds of operations: attribute references
2761and instantiation.
2762
Fred Drakeeee08cd1997-12-04 15:43:15 +00002763\emph{Attribute references} use the standard syntax used for all
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002764attribute references in Python: \verb\obj.name\. Valid attribute
2765names are all the names that were in the class's name space when the
2766class object was created. So, if the class definition looked like
2767this:
2768
2769\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002770 class MyClass:
2771 "A simple example class"
2772 i = 12345
2773 def f(x):
2774 return 'hello world'
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002775\end{verbatim}
2776
2777then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
2778references, returning an integer and a function object, respectively.
Guido van Rossum02455691997-07-17 16:21:52 +00002779Class attributes can also be assigned to, so you can change the value
2780of \verb\MyClass.i\ by assignment. \verb\__doc__\ is also a valid
2781attribute that's read-only, returning the docstring belonging to
2782the class: \verb\"A simple example class"\).
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002783
Fred Drakeeee08cd1997-12-04 15:43:15 +00002784Class \emph{instantiation} uses function notation. Just pretend that
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002785the class object is a parameterless function that returns a new
2786instance of the class. For example, (assuming the above class):
2787
2788\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002789 x = MyClass()
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002790\end{verbatim}
2791
Fred Drakeeee08cd1997-12-04 15:43:15 +00002792creates a new \emph{instance} of the class and assigns this object to
2793the local variable \code{x}.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002794
2795
2796\subsection{Instance objects}
2797
2798Now what can we do with instance objects? The only operations
2799understood by instance objects are attribute references. There are
2800two kinds of valid attribute names.
2801
Fred Drakeeee08cd1997-12-04 15:43:15 +00002802The first I'll call \emph{data attributes}. These correspond to
Guido van Rossum16d6e711994-08-08 12:30:22 +00002803``instance variables'' in Smalltalk, and to ``data members'' in \Cpp{}.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002804Data attributes need not be declared; like local variables, they
2805spring into existence when they are first assigned to. For example,
2806if \verb\x\ in the instance of \verb\MyClass\ created above, the
2807following piece of code will print the value 16, without leaving a
2808trace:
2809
2810\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002811 x.counter = 1
2812 while x.counter < 10:
2813 x.counter = x.counter * 2
2814 print x.counter
2815 del x.counter
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002816\end{verbatim}
2817
2818The second kind of attribute references understood by instance objects
Fred Drakeeee08cd1997-12-04 15:43:15 +00002819are \emph{methods}. A method is a function that ``belongs to'' an
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002820object. (In Python, the term method is not unique to class instances:
2821other object types can have methods as well, e.g., list objects have
2822methods called append, insert, remove, sort, and so on. However,
2823below, we'll use the term method exclusively to mean methods of class
2824instance objects, unless explicitly stated otherwise.)
2825
2826Valid method names of an instance object depend on its class. By
2827definition, all attributes of a class that are (user-defined) function
2828objects define corresponding methods of its instances. So in our
Fred Drakeeee08cd1997-12-04 15:43:15 +00002829example, \code{x.f} is a valid method reference, since
2830\code{MyClass.f} is a function, but \code{x.i} is not, since
2831\code{MyClass.i} is not. But \code{x.f} is not the
2832same thing as \verb\MyClass.f\ --- it is a \emph{method object}, not a
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002833function object.
2834
2835
2836\subsection{Method objects}
2837
2838Usually, a method is called immediately, e.g.:
2839
2840\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002841 x.f()
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002842\end{verbatim}
2843
2844In our example, this will return the string \verb\'hello world'\.
2845However, it is not necessary to call a method right away: \verb\x.f\
2846is a method object, and can be stored away and called at a later
2847moment, for example:
2848
2849\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002850 xf = x.f
2851 while 1:
2852 print xf()
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002853\end{verbatim}
2854
2855will continue to print \verb\hello world\ until the end of time.
2856
2857What exactly happens when a method is called? You may have noticed
2858that \verb\x.f()\ was called without an argument above, even though
2859the function definition for \verb\f\ specified an argument. What
2860happened to the argument? Surely Python raises an exception when a
2861function that requires an argument is called without any --- even if
2862the argument isn't actually used...
2863
2864Actually, you may have guessed the answer: the special thing about
2865methods is that the object is passed as the first argument of the
2866function. In our example, the call \verb\x.f()\ is exactly equivalent
2867to \verb\MyClass.f(x)\. In general, calling a method with a list of
Fred Drakeeee08cd1997-12-04 15:43:15 +00002868\var{n} arguments is equivalent to calling the corresponding function
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002869with an argument list that is created by inserting the method's object
2870before the first argument.
2871
2872If you still don't understand how methods work, a look at the
2873implementation can perhaps clarify matters. When an instance
2874attribute is referenced that isn't a data attribute, its class is
2875searched. If the name denotes a valid class attribute that is a
2876function object, a method object is created by packing (pointers to)
2877the instance object and the function object just found together in an
2878abstract object: this is the method object. When the method object is
2879called with an argument list, it is unpacked again, a new argument
2880list is constructed from the instance object and the original argument
2881list, and the function object is called with this new argument list.
2882
2883
2884\section{Random remarks}
2885
2886
2887[These should perhaps be placed more carefully...]
2888
2889
2890Data attributes override method attributes with the same name; to
2891avoid accidental name conflicts, which may cause hard-to-find bugs in
2892large programs, it is wise to use some kind of convention that
2893minimizes the chance of conflicts, e.g., capitalize method names,
2894prefix data attribute names with a small unique string (perhaps just
Guido van Rossum6938f061994-08-01 12:22:53 +00002895an underscore), or use verbs for methods and nouns for data attributes.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002896
2897
2898Data attributes may be referenced by methods as well as by ordinary
2899users (``clients'') of an object. In other words, classes are not
2900usable to implement pure abstract data types. In fact, nothing in
2901Python makes it possible to enforce data hiding --- it is all based
2902upon convention. (On the other hand, the Python implementation,
2903written in C, can completely hide implementation details and control
2904access to an object if necessary; this can be used by extensions to
2905Python written in C.)
2906
2907
2908Clients should use data attributes with care --- clients may mess up
2909invariants maintained by the methods by stamping on their data
2910attributes. Note that clients may add data attributes of their own to
2911an instance object without affecting the validity of the methods, as
2912long as name conflicts are avoided --- again, a naming convention can
2913save a lot of headaches here.
2914
2915
2916There is no shorthand for referencing data attributes (or other
2917methods!) from within methods. I find that this actually increases
2918the readability of methods: there is no chance of confusing local
2919variables and instance variables when glancing through a method.
2920
2921
2922Conventionally, the first argument of methods is often called
2923\verb\self\. This is nothing more than a convention: the name
2924\verb\self\ has absolutely no special meaning to Python. (Note,
2925however, that by not following the convention your code may be less
2926readable by other Python programmers, and it is also conceivable that
Fred Drakeeee08cd1997-12-04 15:43:15 +00002927a \emph{class browser} program be written which relies upon such a
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002928convention.)
2929
2930
2931Any function object that is a class attribute defines a method for
2932instances of that class. It is not necessary that the function
2933definition is textually enclosed in the class definition: assigning a
2934function object to a local variable in the class is also ok. For
2935example:
2936
2937\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002938 # Function defined outside the class
2939 def f1(self, x, y):
2940 return min(x, x+y)
2941
2942 class C:
2943 f = f1
2944 def g(self):
2945 return 'hello world'
2946 h = g
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002947\end{verbatim}
2948
2949Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
2950\verb\C\ that refer to function objects, and consequently they are all
2951methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
2952to \verb\g\. Note that this practice usually only serves to confuse
2953the reader of a program.
2954
2955
2956Methods may call other methods by using method attributes of the
2957\verb\self\ argument, e.g.:
2958
2959\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002960 class Bag:
2961 def empty(self):
2962 self.data = []
2963 def add(self, x):
2964 self.data.append(x)
2965 def addtwice(self, x):
2966 self.add(x)
2967 self.add(x)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002968\end{verbatim}
2969
2970
2971The instantiation operation (``calling'' a class object) creates an
2972empty object. Many classes like to create objects in a known initial
Guido van Rossumca3f6c81994-10-06 14:08:53 +00002973state. Therefore a class may define a special method named
Fred Drakeeee08cd1997-12-04 15:43:15 +00002974\code{__init__()}, like this:
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002975
Guido van Rossum6938f061994-08-01 12:22:53 +00002976\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002977 def __init__(self):
2978 self.empty()
Guido van Rossum6938f061994-08-01 12:22:53 +00002979\end{verbatim}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002980
Fred Drakeeee08cd1997-12-04 15:43:15 +00002981When a class defines an \code{__init__()} method, class instantiation
2982automatically invokes \code{__init__()} for the newly-created class
2983instance. So in the \code{Bag} example, a new and initialized instance
Guido van Rossum6938f061994-08-01 12:22:53 +00002984can be obtained by:
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002985
Guido van Rossum6938f061994-08-01 12:22:53 +00002986\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00002987 x = Bag()
Guido van Rossum6938f061994-08-01 12:22:53 +00002988\end{verbatim}
2989
2990Of course, the \verb\__init__\ method may have arguments for greater
2991flexibility. In that case, arguments given to the class instantiation
2992operator are passed on to \verb\__init__\. For example,
2993
2994\bcode\begin{verbatim}
2995>>> class Complex:
2996... def __init__(self, realpart, imagpart):
2997... self.r = realpart
2998... self.i = imagpart
2999...
3000>>> x = Complex(3.0,-4.5)
3001>>> x.r, x.i
3002(3.0, -4.5)
3003>>>
3004\end{verbatim}\ecode
3005%
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003006Methods may reference global names in the same way as ordinary
3007functions. The global scope associated with a method is the module
3008containing the class definition. (The class itself is never used as a
3009global scope!) While one rarely encounters a good reason for using
3010global data in a method, there are many legitimate uses of the global
3011scope: for one thing, functions and modules imported into the global
3012scope can be used by methods, as well as functions and classes defined
3013in it. Usually, the class containing the method is itself defined in
3014this global scope, and in the next section we'll find some good
3015reasons why a method would want to reference its own class!
3016
3017
3018\section{Inheritance}
3019
3020Of course, a language feature would not be worthy of the name ``class''
3021without supporting inheritance. The syntax for a derived class
3022definition looks as follows:
3023
3024\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00003025 class DerivedClassName(BaseClassName):
3026 <statement-1>
3027 .
3028 .
3029 .
3030 <statement-N>
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003031\end{verbatim}
3032
3033The name \verb\BaseClassName\ must be defined in a scope containing
3034the derived class definition. Instead of a base class name, an
3035expression is also allowed. This is useful when the base class is
3036defined in another module, e.g.,
3037
3038\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00003039 class DerivedClassName(modname.BaseClassName):
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003040\end{verbatim}
3041
3042Execution of a derived class definition proceeds the same as for a
3043base class. When the class object is constructed, the base class is
3044remembered. This is used for resolving attribute references: if a
3045requested attribute is not found in the class, it is searched in the
3046base class. This rule is applied recursively if the base class itself
3047is derived from some other class.
3048
3049There's nothing special about instantiation of derived classes:
3050\verb\DerivedClassName()\ creates a new instance of the class. Method
3051references are resolved as follows: the corresponding class attribute
3052is searched, descending down the chain of base classes if necessary,
3053and the method reference is valid if this yields a function object.
3054
3055Derived classes may override methods of their base classes. Because
3056methods have no special privileges when calling other methods of the
3057same object, a method of a base class that calls another method
3058defined in the same base class, may in fact end up calling a method of
Guido van Rossum16d6e711994-08-08 12:30:22 +00003059a derived class that overrides it. (For \Cpp{} programmers: all methods
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003060in Python are ``virtual functions''.)
3061
3062An overriding method in a derived class may in fact want to extend
3063rather than simply replace the base class method of the same name.
3064There is a simple way to call the base class method directly: just
3065call \verb\BaseClassName.methodname(self, arguments)\. This is
3066occasionally useful to clients as well. (Note that this only works if
3067the base class is defined or imported directly in the global scope.)
3068
3069
3070\subsection{Multiple inheritance}
3071
Guido van Rossum6938f061994-08-01 12:22:53 +00003072Python supports a limited form of multiple inheritance as well. A
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003073class definition with multiple base classes looks as follows:
3074
3075\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00003076 class DerivedClassName(Base1, Base2, Base3):
3077 <statement-1>
3078 .
3079 .
3080 .
3081 <statement-N>
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003082\end{verbatim}
3083
3084The only rule necessary to explain the semantics is the resolution
3085rule used for class attribute references. This is depth-first,
3086left-to-right. Thus, if an attribute is not found in
3087\verb\DerivedClassName\, it is searched in \verb\Base1\, then
3088(recursively) in the base classes of \verb\Base1\, and only if it is
3089not found there, it is searched in \verb\Base2\, and so on.
3090
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00003091(To some people breadth first---searching \verb\Base2\ and
3092\verb\Base3\ before the base classes of \verb\Base1\---looks more
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003093natural. However, this would require you to know whether a particular
3094attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
3095one of its base classes before you can figure out the consequences of
3096a name conflict with an attribute of \verb\Base2\. The depth-first
3097rule makes no differences between direct and inherited attributes of
3098\verb\Base1\.)
3099
3100It is clear that indiscriminate use of multiple inheritance is a
3101maintenance nightmare, given the reliance in Python on conventions to
3102avoid accidental name conflicts. A well-known problem with multiple
3103inheritance is a class derived from two classes that happen to have a
3104common base class. While it is easy enough to figure out what happens
3105in this case (the instance will have a single copy of ``instance
3106variables'' or data attributes used by the common base class), it is
3107not clear that these semantics are in any way useful.
3108
3109
Guido van Rossum02455691997-07-17 16:21:52 +00003110\section{Private variables through name mangling}
3111
3112There is now limited support for class-private
3113identifiers. Any identifier of the form \code{__spam} (at least two
3114leading underscores, at most one trailing underscore) is now textually
3115replaced with \code{_classname__spam}, where \code{classname} is the
3116current class name with leading underscore(s) stripped. This mangling
3117is done without regard of the syntactic position of the identifier, so
3118it can be used to define class-private instance and class variables,
3119methods, as well as globals, and even to store instance variables
Fred Drakeeee08cd1997-12-04 15:43:15 +00003120private to this class on instances of \emph{other} classes. Truncation
Guido van Rossum02455691997-07-17 16:21:52 +00003121may occur when the mangled name would be longer than 255 characters.
3122Outside classes, or when the class name consists of only underscores,
3123no mangling occurs.
3124
3125Name mangling is intended to give classes an easy way to define
3126``private'' instance variables and methods, without having to worry
3127about instance variables defined by derived classes, or mucking with
3128instance variables by code outside the class. Note that the mangling
3129rules are designed mostly to avoid accidents; it still is possible for
3130a determined soul to access or modify a variable that is considered
3131private. This can even be useful, e.g. for the debugger, and that's
3132one reason why this loophole is not closed. (Buglet: derivation of a
3133class with the same name as the base class makes use of private
3134variables of the base class possible.)
3135
3136Notice that code passed to \code{exec}, \code{eval()} or
3137\code{evalfile()} does not consider the classname of the invoking
3138class to be the current class; this is similar to the effect of the
3139\code{global} statement, the effect of which is likewise restricted to
3140code that is byte-compiled together. The same restriction applies to
3141\code{getattr()}, \code{setattr()} and \code{delattr()}, as well as
3142when referencing \code{__dict__} directly.
3143
3144Here's an example of a class that implements its own
3145\code{__getattr__} and \code{__setattr__} methods and stores all
3146attributes in a private variable, in a way that works in Python 1.4 as
3147well as in previous versions:
3148
3149\begin{verbatim}
3150class VirtualAttributes:
3151 __vdict = None
3152 __vdict_name = locals().keys()[0]
3153
3154 def __init__(self):
3155 self.__dict__[self.__vdict_name] = {}
3156
3157 def __getattr__(self, name):
3158 return self.__vdict[name]
3159
3160 def __setattr__(self, name, value):
3161 self.__vdict[name] = value
3162\end{verbatim}
3163
3164%{\em Warning: this is an experimental feature.} To avoid all
3165%potential problems, refrain from using identifiers starting with
3166%double underscore except for predefined uses like \code{__init__}. To
3167%use private names while maintaining future compatibility: refrain from
3168%using the same private name in classes related via subclassing; avoid
3169%explicit (manual) mangling/unmangling; and assume that at some point
3170%in the future, leading double underscore will revert to being just a
3171%naming convention. Discussion on extensive compile-time declarations
3172%are currently underway, and it is impossible to predict what solution
3173%will eventually be chosen for private names. Double leading
3174%underscore is still a candidate, of course --- just not the only one.
3175%It is placed in the distribution in the belief that it is useful, and
3176%so that widespread experience with its use can be gained. It will not
3177%be removed without providing a better solution and a migration path.
3178
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003179\section{Odds and ends}
3180
3181Sometimes it is useful to have a data type similar to the Pascal
3182``record'' or C ``struct'', bundling together a couple of named data
3183items. An empty class definition will do nicely, e.g.:
3184
3185\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00003186 class Employee:
3187 pass
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003188
Fred Drakeeee08cd1997-12-04 15:43:15 +00003189 john = Employee() # Create an empty employee record
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003190
Fred Drakeeee08cd1997-12-04 15:43:15 +00003191 # Fill the fields of the record
3192 john.name = 'John Doe'
3193 john.dept = 'computer lab'
3194 john.salary = 1000
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003195\end{verbatim}
3196
3197
3198A piece of Python code that expects a particular abstract data type
3199can often be passed a class that emulates the methods of that data
3200type instead. For instance, if you have a function that formats some
3201data from a file object, you can define a class with methods
3202\verb\read()\ and \verb\readline()\ that gets the data from a string
3203buffer instead, and pass it as an argument. (Unfortunately, this
3204technique has its limitations: a class can't define operations that
3205are accessed by special syntax such as sequence subscripting or
3206arithmetic operators, and assigning such a ``pseudo-file'' to
3207\verb\sys.stdin\ will not cause the interpreter to read further input
3208from it.)
3209
3210
3211Instance method objects have attributes, too: \verb\m.im_self\ is the
3212object of which the method is an instance, and \verb\m.im_func\ is the
3213function object corresponding to the method.
3214
Guido van Rossum02455691997-07-17 16:21:52 +00003215\subsection{Exceptions Can Be Classes}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003216
3217User-defined exceptions are no longer limited to being string objects
3218--- they can be identified by classes as well. Using this mechanism it
3219is possible to create extensible hierarchies of exceptions.
3220
3221There are two new valid (semantic) forms for the raise statement:
3222
3223\begin{verbatim}
3224raise Class, instance
3225
3226raise instance
3227\end{verbatim}
3228
3229In the first form, \code{instance} must be an instance of \code{Class}
3230or of a class derived from it. The second form is a shorthand for
3231
3232\begin{verbatim}
3233raise instance.__class__, instance
3234\end{verbatim}
3235
3236An except clause may list classes as well as string objects. A class
3237in an except clause is compatible with an exception if it is the same
3238class or a base class thereof (but not the other way around --- an
3239except clause listing a derived class is not compatible with a base
3240class). For example, the following code will print B, C, D in that
3241order:
3242
3243\begin{verbatim}
3244class B:
3245 pass
3246class C(B):
3247 pass
3248class D(C):
3249 pass
3250
3251for c in [B, C, D]:
3252 try:
3253 raise c()
3254 except D:
3255 print "D"
3256 except C:
3257 print "C"
3258 except B:
3259 print "B"
3260\end{verbatim}
3261
3262Note that if the except clauses were reversed (with ``\code{except B}''
3263first), it would have printed B, B, B --- the first matching except
3264clause is triggered.
3265
3266When an error message is printed for an unhandled exception which is a
3267class, the class name is printed, then a colon and a space, and
3268finally the instance converted to a string using the built-in function
3269\code{str()}.
3270
3271In this release, the built-in exceptions are still strings.
3272
Guido van Rossum02455691997-07-17 16:21:52 +00003273\chapter{What Now?}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003274
Guido van Rossum02455691997-07-17 16:21:52 +00003275Hopefully reading this tutorial has reinforced your interest in using
3276Python. Now what should you do?
Guido van Rossum194e57c1995-02-15 15:51:38 +00003277
Guido van Rossum02455691997-07-17 16:21:52 +00003278You should read, or at least page through, the Library Reference,
3279which gives complete (though terse) reference material about types,
3280functions, and modules that can save you a lot of time when writing
3281Python programs. The standard Python distribution includes a
3282\emph{lot} of code in both C and Python; there are modules to read
3283Unix mailboxes, retrieve documents via HTTP, generate random numbers,
3284parse command-line options, write CGI programs, compress data, and a
3285lot more; skimming through the Library Reference will give you an idea
3286of what's available.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003287
Guido van Rossum02455691997-07-17 16:21:52 +00003288The major Python Web site is \code{http://www.python.org}; it contains
3289code, documentation, and pointers to Python-related pages around the
3290Web. \code{www.python.org} is mirrored in various places around the
3291world, such as Europe, Japan, and Australia; a mirror may be faster
3292than the main site, depending on your geographical location. A more
3293informal site is \code{http://starship.skyport.net}, which contains a
3294bunch of Python-related personal home pages; many people have
3295downloadable software here.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003296
Guido van Rossum02455691997-07-17 16:21:52 +00003297For Python-related questions and problem reports, you can post to the
3298newsgroup \code{comp.lang.python}, or send them to the mailing list at
3299\code{python-list@cwi.nl}. The newsgroup and mailing list are
3300gatewayed, so messages posted to one will automatically be forwarded
3301to the other. There are around 20--30 postings a day, asking (and
3302answering) questions, suggesting new features, and announcing new
3303modules. But before posting, be sure to check the list of Frequently
3304Asked Questions (also called the FAQ), at
3305\code{http://www.python.org/doc/FAQ.html}, or look for it in the
3306\code{Misc/} directory of the Python source distribution. The FAQ
3307answers many of the questions that come up again and again, and may
3308already contain the solution for your problem.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003309
Guido van Rossum02455691997-07-17 16:21:52 +00003310You can support the Python community by joining the Python Software
3311Activity, which runs the python.org web, ftp and email servers, and
3312organizes Python workshops. See \code{http://www.python.org/psa/} for
3313information on how to join.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003314
Guido van Rossum194e57c1995-02-15 15:51:38 +00003315
Guido van Rossum02455691997-07-17 16:21:52 +00003316\chapter{Recent Additions as of Release 1.1}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003317
Guido van Rossum02455691997-07-17 16:21:52 +00003318XXX Should the stuff in this chapter be deleted, or can a home be found or it elsewhere in the Tutorial?
Guido van Rossum194e57c1995-02-15 15:51:38 +00003319
Guido van Rossum02455691997-07-17 16:21:52 +00003320\section{Lambda Forms}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003321
Guido van Rossum02455691997-07-17 16:21:52 +00003322XXX Where to put this? Or just leave it out?
Guido van Rossum194e57c1995-02-15 15:51:38 +00003323
Guido van Rossum02455691997-07-17 16:21:52 +00003324By popular demand, a few features commonly found in functional
3325programming languages and Lisp have been added to Python. With the
3326\verb\lambda\ keyword, small anonymous functions can be created.
3327Here's a function that returns the sum of its two arguments:
3328\verb\lambda a, b: a+b\. Lambda forms can be used wherever function
3329objects are required. They are syntactically restricted to a single
3330expression. Semantically, they are just syntactic sugar for a normal
3331function definition. Like nested function definitions, lambda forms
3332cannot reference variables from the containing scope, but this can be
3333overcome through the judicious use of default argument values, e.g.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003334
Guido van Rossum02455691997-07-17 16:21:52 +00003335\begin{verbatim}
Fred Drakeeee08cd1997-12-04 15:43:15 +00003336 def make_incrementor(n):
3337 return lambda x, incr=n: x+incr
Guido van Rossum02455691997-07-17 16:21:52 +00003338\end{verbatim}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003339
3340\section{Documentation Strings}
3341
Guido van Rossum02455691997-07-17 16:21:52 +00003342XXX Where to put this? Or just leave it out?
Guido van Rossum194e57c1995-02-15 15:51:38 +00003343
3344There are emerging conventions about the content and formatting of
3345documentation strings.
3346
3347The first line should always be a short, concise summary of the
3348object's purpose. For brevity, it should not explicitly state the
3349object's name or type, since these are available by other means
3350(except if the name happens to be a verb describing a function's
3351operation). This line should begin with a capital letter and end with
3352a period.
3353
3354If there are more lines in the documentation string, the second line
3355should be blank, visually separating the summary from the rest of the
3356description. The following lines should be one of more of paragraphs
3357describing the objects calling conventions, its side effects, etc.
3358
3359Some people like to copy the Emacs convention of using UPPER CASE for
3360function parameters --- this often saves a few words or lines.
3361
3362The Python parser does not strip indentation from multi-line string
3363literals in Python, so tools that process documentation have to strip
3364indentation. This is done using the following convention. The first
Fred Drakeeee08cd1997-12-04 15:43:15 +00003365non-blank line \emph{after} the first line of the string determines the
Guido van Rossum194e57c1995-02-15 15:51:38 +00003366amount of indentation for the entire documentation string. (We can't
3367use the first line since it is generally adjacent to the string's
3368opening quotes so its indentation is not apparent in the string
3369literal.) Whitespace ``equivalent'' to this indentation is then
3370stripped from the start of all lines of the string. Lines that are
3371indented less should not occur, but if they occur all their leading
3372whitespace should be stripped. Equivalence of whitespace should be
3373tested after expansion of tabs (to 8 spaces, normally).
3374
Guido van Rossum194e57c1995-02-15 15:51:38 +00003375
Guido van Rossum02455691997-07-17 16:21:52 +00003376\appendix\chapter{Interactive Input Editing and History Substitution}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003377
Guido van Rossum02455691997-07-17 16:21:52 +00003378Some versions of the Python interpreter support editing of the current
3379input line and history substitution, similar to facilities found in
3380the Korn shell and the GNU Bash shell. This is implemented using the
Fred Drakeeee08cd1997-12-04 15:43:15 +00003381\emph{GNU Readline} library, which supports Emacs-style and vi-style
Guido van Rossum02455691997-07-17 16:21:52 +00003382editing. This library has its own documentation which I won't
3383duplicate here; however, the basics are easily explained.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003384
Guido van Rossum02455691997-07-17 16:21:52 +00003385\subsection{Line Editing}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003386
Guido van Rossum02455691997-07-17 16:21:52 +00003387If supported, input line editing is active whenever the interpreter
3388prints a primary or secondary prompt. The current line can be edited
3389using the conventional Emacs control characters. The most important
3390of these are: C-A (Control-A) moves the cursor to the beginning of the
3391line, C-E to the end, C-B moves it one position to the left, C-F to
3392the right. Backspace erases the character to the left of the cursor,
3393C-D the character to its right. C-K kills (erases) the rest of the
3394line to the right of the cursor, C-Y yanks back the last killed
3395string. C-underscore undoes the last change you made; it can be
3396repeated for cumulative effect.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003397
Guido van Rossum02455691997-07-17 16:21:52 +00003398\subsection{History Substitution}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003399
Guido van Rossum02455691997-07-17 16:21:52 +00003400History substitution works as follows. All non-empty input lines
3401issued are saved in a history buffer, and when a new prompt is given
3402you are positioned on a new line at the bottom of this buffer. C-P
3403moves one line up (back) in the history buffer, C-N moves one down.
3404Any line in the history buffer can be edited; an asterisk appears in
3405front of the prompt to mark a line as modified. Pressing the Return
3406key passes the current line to the interpreter. C-R starts an
3407incremental reverse search; C-S starts a forward search.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003408
Guido van Rossum02455691997-07-17 16:21:52 +00003409\subsection{Key Bindings}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003410
Guido van Rossum02455691997-07-17 16:21:52 +00003411The key bindings and some other parameters of the Readline library can
3412be customized by placing commands in an initialization file called
Fred Drakeeee08cd1997-12-04 15:43:15 +00003413\file{\$HOME/.inputrc}. Key bindings have the form
Guido van Rossum194e57c1995-02-15 15:51:38 +00003414
Guido van Rossum02455691997-07-17 16:21:52 +00003415\bcode\begin{verbatim}
3416key-name: function-name
3417\end{verbatim}\ecode
3418%
3419or
Guido van Rossum194e57c1995-02-15 15:51:38 +00003420
Guido van Rossum02455691997-07-17 16:21:52 +00003421\bcode\begin{verbatim}
3422"string": function-name
3423\end{verbatim}\ecode
3424%
3425and options can be set with
Guido van Rossum194e57c1995-02-15 15:51:38 +00003426
Guido van Rossum02455691997-07-17 16:21:52 +00003427\bcode\begin{verbatim}
3428set option-name value
3429\end{verbatim}\ecode
3430%
3431For example:
Guido van Rossum194e57c1995-02-15 15:51:38 +00003432
Guido van Rossum02455691997-07-17 16:21:52 +00003433\bcode\begin{verbatim}
3434# I prefer vi-style editing:
3435set editing-mode vi
3436# Edit using a single line:
3437set horizontal-scroll-mode On
3438# Rebind some keys:
3439Meta-h: backward-kill-word
3440"\C-u": universal-argument
3441"\C-x\C-r": re-read-init-file
3442\end{verbatim}\ecode
3443%
3444Note that the default binding for TAB in Python is to insert a TAB
3445instead of Readline's default filename completion function. If you
3446insist, you can override this by putting
Guido van Rossum194e57c1995-02-15 15:51:38 +00003447
Guido van Rossum02455691997-07-17 16:21:52 +00003448\bcode\begin{verbatim}
3449TAB: complete
3450\end{verbatim}\ecode
3451%
Fred Drakeeee08cd1997-12-04 15:43:15 +00003452in your \file{\$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossum02455691997-07-17 16:21:52 +00003453indented continuation lines...)
Guido van Rossum194e57c1995-02-15 15:51:38 +00003454
Guido van Rossum02455691997-07-17 16:21:52 +00003455\subsection{Commentary}
Guido van Rossum194e57c1995-02-15 15:51:38 +00003456
Guido van Rossum02455691997-07-17 16:21:52 +00003457This facility is an enormous step forward compared to previous
3458versions of the interpreter; however, some wishes are left: It would
3459be nice if the proper indentation were suggested on continuation lines
3460(the parser knows if an indent token is required next). The
3461completion mechanism might use the interpreter's symbol table. A
3462command to check (or even suggest) matching parentheses, quotes etc.
3463would also be useful.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003464
Guido van Rossum02455691997-07-17 16:21:52 +00003465XXX Lele Gaifax's readline module, which adds name completion...
Guido van Rossum97662c81996-08-23 15:35:47 +00003466
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00003467\end{document}
Guido van Rossum02455691997-07-17 16:21:52 +00003468