blob: c29d3a765d8fdbeb6194153f7b5d643eae7942f5 [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 Rossum6938f061994-08-01 12:22:53 +00003\title{Python Tutorial}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004
Guido van Rossum16cd7f91994-10-06 10:29:26 +00005\input{boilerplate}
Guido van Rossum83eb9621993-11-23 16:28:45 +00006
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00007\begin{document}
8
9\pagenumbering{roman}
10
11\maketitle
12
Guido van Rossum16cd7f91994-10-06 10:29:26 +000013\input{copyright}
14
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000015\begin{abstract}
16
17\noindent
Guido van Rossum4410c751991-06-04 20:22:18 +000018Python is a simple, yet powerful programming language that bridges the
Guido van Rossum6fc178f1991-08-16 09:13:42 +000019gap between C and shell programming, and is thus ideally suited for
20``throw-away programming''
21and rapid prototyping. Its syntax is put
22together from constructs borrowed from a variety of other languages;
23most prominent are influences from ABC, C, Modula-3 and Icon.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000024
Guido van Rossum4410c751991-06-04 20:22:18 +000025The Python interpreter is easily extended with new functions and data
Guido van Rossum6fc178f1991-08-16 09:13:42 +000026types implemented in C. Python is also suitable as an extension
27language for highly customizable C applications such as editors or
28window managers.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000029
Guido van Rossum4410c751991-06-04 20:22:18 +000030Python is available for various operating systems, amongst which
Guido van Rossum6fc178f1991-08-16 09:13:42 +000031several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
32and MS-DOS.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000033
Guido van Rossum6fc178f1991-08-16 09:13:42 +000034This tutorial introduces the reader informally to the basic concepts
35and features of the Python language and system. It helps to have a
36Python interpreter handy for hands-on experience, but as the examples
37are self-contained, the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000038
Guido van Rossum481ae681991-11-25 17:28:03 +000039For a description of standard objects and modules, see the {\em Python
40Library Reference} document. The {\em Python Reference Manual} gives
41a more formal definition of the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000042
43\end{abstract}
44
45\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000046{
47\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000048\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000049}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000050
51\pagebreak
52
53\pagenumbering{arabic}
54
Guido van Rossum5e0759d1992-08-07 16:06:24 +000055
Guido van Rossum6fc178f1991-08-16 09:13:42 +000056\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000057
Guido van Rossum6fc178f1991-08-16 09:13:42 +000058If you ever wrote a large shell script, you probably know this
59feeling: you'd love to add yet another feature, but it's already so
60slow, and so big, and so complicated; or the feature involves a system
Guido van Rossum6938f061994-08-01 12:22:53 +000061call or other function that is only accessible from C \ldots Usually
Guido van Rossum6fc178f1991-08-16 09:13:42 +000062the problem at hand isn't serious enough to warrant rewriting the
63script in C; perhaps because the problem requires variable-length
64strings or other data types (like sorted lists of file names) that are
65easy in the shell but lots of work to implement in C; or perhaps just
66because you're not sufficiently familiar with C.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000067
Guido van Rossum6fc178f1991-08-16 09:13:42 +000068In such cases, Python may be just the language for you. Python is
69simple to use, but it is a real programming language, offering much
70more structure and support for large programs than the shell has. On
71the other hand, it also offers much more error checking than C, and,
72being a {\em very-high-level language}, it has high-level data types
73built in, such as flexible arrays and dictionaries that would cost you
74days to implement efficiently in C. Because of its more general data
75types Python is applicable to a much larger problem domain than {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +000076Awk} or even {\em Perl}, yet many things are at least as easy in
77Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000078
Guido van Rossum6fc178f1991-08-16 09:13:42 +000079Python allows you to split up your program in modules that can be
80reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +000081standard modules that you can use as the basis of your programs --- or
82as examples to start learning to program in Python. There are also
83built-in modules that provide things like file I/O, system calls,
84sockets, and even a generic interface to window systems (STDWIN).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000085
Guido van Rossuma8d754e1992-01-07 16:44:35 +000086Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000087during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +000088necessary. The interpreter can be used interactively, which makes it
89easy to experiment with features of the language, to write throw-away
90programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000091It is also a handy desk calculator.
92
Guido van Rossum6fc178f1991-08-16 09:13:42 +000093Python allows writing very compact and readable programs. Programs
94written in Python are typically much shorter than equivalent C
95programs, for several reasons:
96\begin{itemize}
97\item
98the high-level data types allow you to express complex operations in a
99single statement;
100\item
101statement grouping is done by indentation instead of begin/end
102brackets;
103\item
104no variable or argument declarations are necessary.
105\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000106
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000107Python is {\em extensible}: if you know how to program in C it is easy
108to add a new built-in
109function or
110module to the interpreter, either to
111perform critical operations at maximum speed, or to link Python
112programs to libraries that may only be available in binary form (such
113as a vendor-specific graphics library). Once you are really hooked,
114you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000115and use it as an extension or command language for that application.
116
117By the way, the language is named after the BBC show ``Monty
118Python's Flying Circus'' and has nothing to do with nasty reptiles...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000119
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000120\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000121
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000122Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000123in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000124using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000125
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000126In the next chapter, the mechanics of using the interpreter are
127explained. This is rather mundane information, but essential for
128trying out the examples shown later.
129
Guido van Rossum4410c751991-06-04 20:22:18 +0000130The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000131language and system though examples, beginning with simple
132expressions, statements and data types, through functions and modules,
Guido van Rossum6938f061994-08-01 12:22:53 +0000133and finally touching upon advanced concepts like exceptions
134and user-defined classes.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000135
Guido van Rossum6938f061994-08-01 12:22:53 +0000136When you're through with the tutorial (or just getting bored), you
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000137should read the Library Reference, which gives complete (though terse)
138reference material about built-in and standard types, functions and
139modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000140
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000141
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000142\chapter{Using the Python Interpreter}
143
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000144\section{Invoking the Interpreter}
145
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000146The Python interpreter is usually installed as {\tt /usr/local/bin/python}
147on those machines where it is available; putting {\tt /usr/local/bin} in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000148your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000149typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000150
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000151\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000152python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000153\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000154%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000155to the shell. Since the choice of the directory where the interpreter
156lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000157your local Python guru or system administrator. (E.g., {\tt
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000158/usr/local/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000159
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000160The interpreter operates somewhat like the {\UNIX} shell: when called
161with standard input connected to a tty device, it reads and executes
162commands interactively; when called with a file name argument or with
163a file as standard input, it reads and executes a {\em script} from
164that file.
165
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000166A third way of starting the interpreter is
167``{\tt python -c command [arg] ...}'', which
168executes the statement(s) in {\tt command}, analogous to the shell's
169{\tt -c} option. Since Python statements often contain spaces or other
170characters that are special to the shell, it is best to quote {\tt
171command} in its entirety with double quotes.
172
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000173Note that there is a difference between ``{\tt python file}'' and
174``{\tt python $<$file}''. In the latter case, input requests from the
Guido van Rossum573805a1992-03-06 10:56:03 +0000175program, such as calls to {\tt input()} and {\tt raw_input()}, are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000176satisfied from {\em file}. Since this file has already been read
177until the end by the parser before the program starts executing, the
178program will encounter EOF immediately. In the former case (which is
179usually what you want) they are satisfied from whatever file or device
180is connected to standard input of the Python interpreter.
181
Guido van Rossumb2c65561993-05-12 08:53:36 +0000182When a script file is used, it is sometimes useful to be able to run
183the script and enter interactive mode afterwards. This can be done by
184passing {\tt -i} before the script. (This does not work if the script
185is read from standard input, for the same reason as explained in the
186previous paragraph.)
187
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000188\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000189
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000190When known to the interpreter, the script name and additional
191arguments thereafter are passed to the script in the variable {\tt
192sys.argv}, which is a list of strings. Its length is at least one;
193when no script and no arguments are given, {\tt sys.argv[0]} is an
194empty string. When the script name is given as {\tt '-'} (meaning
195standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
196command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
197found after {\tt -c command} are not consumed by the Python
198interpreter's option processing but left in {\tt sys.argv} for the
199command to handle.
200
201\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000202
Guido van Rossumdd010801991-06-07 14:31:11 +0000203When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000204{\em interactive\ mode}. In this mode it prompts for the next command
205with the {\em primary\ prompt}, usually three greater-than signs ({\tt
206>>>}); for continuation lines it prompts with the {\em secondary\
207prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
208at the primary prompt causes the interpreter to exit with a zero exit
209status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000210
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000211The interpreter prints a welcome message stating its version number
212and a copyright notice before printing the first prompt, e.g.:
213
214\bcode\begin{verbatim}
215python
Guido van Rossum94ed6f51994-10-06 17:08:42 +0000216Python 1.1 (Oct 6 1994)
Guido van Rossum6938f061994-08-01 12:22:53 +0000217Copyright 1991-1994 Stichting Mathematisch Centrum, Amsterdam
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000218>>>
219\end{verbatim}\ecode
220
221\section{The Interpreter and its Environment}
222
223\subsection{Error Handling}
224
225When an error occurs, the interpreter prints an error
226message and a stack trace. In interactive mode, it then returns to
227the primary prompt; when input came from a file, it exits with a
228nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000229the stack trace. (Exceptions handled by an {\tt except} clause in a
230{\tt try} statement are not errors in this context.) Some errors are
231unconditionally fatal and cause an exit with a nonzero exit; this
232applies to internal inconsistencies and some cases of running out of
233memory. All error messages are written to the standard error stream;
234normal output from the executed commands is written to standard
235output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000236
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000237Typing the interrupt character (usually Control-C or DEL) to the
238primary or secondary prompt cancels the input and returns to the
239primary prompt.%
240\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +0000241 A problem with the GNU Readline package may prevent this.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000242}
243Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000244KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000245statement.
246
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000247\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000248
Guido van Rossume5f8b601995-01-04 19:12:49 +0000249When a module named {\tt spam} is imported, the interpreter searches
250for a file named {\tt spam.py} in the list of directories specified by
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000251the environment variable {\tt PYTHONPATH}. It has the same syntax as
252the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000253directory names. When {\tt PYTHONPATH} is not set, or when the file
254is not found there, the search continues in an installation-dependent
255default path, usually {\tt .:/usr/local/lib/python}.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000256
257Actually, modules are searched in the list of directories given by the
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000258variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} and
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000259the installation-dependent default. This allows Python programs that
260know what they're doing to modify or replace the module search path.
261See the section on Standard Modules later.
262
263\subsection{``Compiled'' Python files}
264
265As an important speed-up of the start-up time for short programs that
Guido van Rossume5f8b601995-01-04 19:12:49 +0000266use a lot of standard modules, if a file called {\tt spam.pyc} exists
267in the directory where {\tt spam.py} is found, this is assumed to
268contain an already-``compiled'' version of the module {\tt spam}. The
269modification time of the version of {\tt spam.py} used to create {\tt
270spam.pyc} is recorded in {\tt spam.pyc}, and the file is ignored if
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000271these don't match.
272
Guido van Rossume5f8b601995-01-04 19:12:49 +0000273Whenever {\tt spam.py} is successfully compiled, an attempt is made to
274write the compiled version to {\tt spam.pyc}. It is not an error if
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000275this attempt fails; if for any reason the file is not written
Guido van Rossume5f8b601995-01-04 19:12:49 +0000276completely, the resulting {\tt spam.pyc} file will be recognized as
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000277invalid and thus ignored later.
278
279\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000280
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000281On BSD'ish {\UNIX} systems, Python scripts can be made directly
282executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000283
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000284\bcode\begin{verbatim}
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000285#! /usr/local/bin/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000286\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000287%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000288(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000289script and giving the file an executable mode. The {\tt \#!} must be
290the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000291
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000292\subsection{The Interactive Startup File}
293
294When you use Python interactively, it is frequently handy to have some
295standard commands executed every time the interpreter is started. You
296can do this by setting an environment variable named {\tt
297PYTHONSTARTUP} to the name of a file containing your start-up
Guido van Rossum6938f061994-08-01 12:22:53 +0000298commands. This is similar to the {\tt .profile} feature of the UNIX
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000299shells.
300
301This file is only read in interactive sessions, not when Python reads
302commands from a script, and not when {\tt /dev/tty} is given as the
303explicit source of commands (which otherwise behaves like an
304interactive session). It is executed in the same name space where
305interactive commands are executed, so that objects that it defines or
306imports can be used without qualification in the interactive session.
Guido van Rossum7b3c8a11992-09-08 09:20:13 +0000307You can also change the prompts {\tt sys.ps1} and {\tt sys.ps2} in
308this file.
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000309
310If you want to read an additional start-up file from the current
311directory, you can program this in the global start-up file, e.g.
312\verb\execfile('.pythonrc')\. If you want to use the startup file
313in a script, you must write this explicitly in the script, e.g.
314\verb\import os;\ \verb\execfile(os.environ['PYTHONSTARTUP'])\.
315
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000316\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000317
Guido van Rossum4410c751991-06-04 20:22:18 +0000318Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000319input line and history substitution, similar to facilities found in
320the Korn shell and the GNU Bash shell. This is implemented using the
321{\em GNU\ Readline} library, which supports Emacs-style and vi-style
322editing. This library has its own documentation which I won't
323duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000324
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000325Perhaps the quickest check to see whether command line editing is
326supported is typing Control-P to the first Python prompt you get. If
327it beeps, you have command line editing. If nothing appears to
328happen, or if \verb/^P/ is echoed, you can skip the rest of this
329section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000330
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000331\subsection{Line Editing}
332
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000333If supported, input line editing is active whenever the interpreter
334prints a primary or secondary prompt. The current line can be edited
335using the conventional Emacs control characters. The most important
336of these are: C-A (Control-A) moves the cursor to the beginning of the
337line, C-E to the end, C-B moves it one position to the left, C-F to
338the right. Backspace erases the character to the left of the cursor,
339C-D the character to its right. C-K kills (erases) the rest of the
340line to the right of the cursor, C-Y yanks back the last killed
341string. C-underscore undoes the last change you made; it can be
342repeated for cumulative effect.
343
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000344\subsection{History Substitution}
345
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000346History substitution works as follows. All non-empty input lines
347issued are saved in a history buffer, and when a new prompt is given
348you are positioned on a new line at the bottom of this buffer. C-P
349moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000350Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000351front of the prompt to mark a line as modified. Pressing the Return
352key passes the current line to the interpreter. C-R starts an
353incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000354
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000355\subsection{Key Bindings}
356
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000357The key bindings and some other parameters of the Readline library can
358be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000359{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000360
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000361\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000362key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000363\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000364%
365or
366
367\bcode\begin{verbatim}
368"string": function-name
369\end{verbatim}\ecode
370%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000371and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000372
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000373\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000374set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000375\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000376%
377For example:
378
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000379\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000380# I prefer vi-style editing:
381set editing-mode vi
382# Edit using a single line:
383set horizontal-scroll-mode On
384# Rebind some keys:
385Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000386"\C-u": universal-argument
387"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000388\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000389%
Guido van Rossum4410c751991-06-04 20:22:18 +0000390Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000391instead of Readline's default filename completion function. If you
392insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000393
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000394\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000395TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000396\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000397%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000398in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000399indented continuation lines...)
400
401\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000402
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000403This facility is an enormous step forward compared to previous
404versions of the interpreter; however, some wishes are left: It would
405be nice if the proper indentation were suggested on continuation lines
406(the parser knows if an indent token is required next). The
407completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000408command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000409would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000410
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000411
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000412\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000413
414In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000415presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
416the example, you must type everything after the prompt, when the
417prompt appears; lines that do not begin with a prompt are output from
418the interpreter.%
419\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +0000420 I'd prefer to use different fonts to distinguish input
421 from output, but the amount of LaTeX hacking that would require
422 is currently beyond my ability.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000423}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000424Note that a secondary prompt on a line by itself in an example means
425you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000426
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000427\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000428
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000429Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000430for the primary prompt, {\tt >>>}. (It shouldn't take long.)
431
432\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000433
434The interpreter acts as a simple calculator: you can type an
435expression at it and it will write the value. Expression syntax is
436straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000437work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000438can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000439
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000440\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000441>>> 2+2
4424
Guido van Rossum6938f061994-08-01 12:22:53 +0000443>>> # This is a comment
444... 2+2
4454
446>>> 2+2 # and a comment on the same line as code
4474
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000448>>> (50-5*6)/4
4495
Guido van Rossum6938f061994-08-01 12:22:53 +0000450>>> # Integer division returns the floor:
451... 7/3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004522
Guido van Rossum6938f061994-08-01 12:22:53 +0000453>>> 7/-3
454-3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000455>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000456\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000457%
458Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000459variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000460
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000461\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000462>>> width = 20
463>>> height = 5*9
464>>> width * height
465900
466>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000467\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000468%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000469A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000470
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000471\bcode\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000472>>> x = y = z = 0 # Zero x, y and z
473>>> x
4740
475>>> y
4760
477>>> z
4780
479>>>
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000480\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000481%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000482There is full support for floating point; operators with mixed type
483operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000484
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000485\bcode\begin{verbatim}
486>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004873.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000488>>> 7.0 / 2
4893.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000490>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000491\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000492
493\subsection{Strings}
494
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000495Besides numbers, Python can also manipulate strings, enclosed in
Guido van Rossum6938f061994-08-01 12:22:53 +0000496single quotes or double quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000497
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000498\bcode\begin{verbatim}
Guido van Rossume5f8b601995-01-04 19:12:49 +0000499>>> 'spam eggs'
500'spam eggs'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000501>>> 'doesn\'t'
Guido van Rossum6938f061994-08-01 12:22:53 +0000502"doesn't"
503>>> "doesn't"
504"doesn't"
505>>> '"Yes," he said.'
506'"Yes," he said.'
507>>> "\"Yes,\" he said."
508'"Yes," he said.'
509>>> '"Isn\'t," she said.'
510'"Isn\'t," she said.'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000511>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000512\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000513%
514Strings are written the same way as they are typed for input: inside
Guido van Rossum6938f061994-08-01 12:22:53 +0000515quotes and with quotes and other funny characters escaped by backslashes,
516to show the precise value. The string is enclosed in double quotes if
517the string contains a single quote and no double quotes, else it's
518enclosed in single quotes. (The {\tt print} statement, described later,
519can be used to write strings without quotes or escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000520
521Strings can be concatenated (glued together) with the {\tt +}
522operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000523
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000524\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000525>>> word = 'Help' + 'A'
526>>> word
527'HelpA'
528>>> '<' + word*5 + '>'
529'<HelpAHelpAHelpAHelpAHelpA>'
530>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000531\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000532%
533Strings can be subscripted (indexed); like in C, the first character of
534a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000535
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000536There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000537size one. Like in Icon, substrings can be specified with the {\em
538slice} notation: two indices separated by a colon.
539
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000540\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000541>>> word[4]
542'A'
543>>> word[0:2]
544'He'
545>>> word[2:4]
546'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000547>>>
548\end{verbatim}\ecode
549%
550Slice indices have useful defaults; an omitted first index defaults to
551zero, an omitted second index defaults to the size of the string being
552sliced.
553
554\bcode\begin{verbatim}
555>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000556'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000557>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000558'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000559>>>
560\end{verbatim}\ecode
561%
562Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
563equals \verb\s\.
564
565\bcode\begin{verbatim}
566>>> word[:2] + word[2:]
567'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000568>>> word[:3] + word[3:]
569'HelpA'
570>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000571\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000572%
573Degenerate slice indices are handled gracefully: an index that is too
574large is replaced by the string size, an upper bound smaller than the
575lower bound returns an empty string.
576
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000577\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000578>>> word[1:100]
579'elpA'
580>>> word[10:]
581''
582>>> word[2:1]
583''
584>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000585\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000586%
587Indices may be negative numbers, to start counting from the right.
588For example:
589
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000590\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000591>>> word[-1] # The last character
592'A'
593>>> word[-2] # The last-but-one character
594'p'
595>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000596'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000597>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000598'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000599>>>
600\end{verbatim}\ecode
601%
602But note that -0 is really the same as 0, so it does not count from
603the right!
604
605\bcode\begin{verbatim}
606>>> word[-0] # (since -0 equals 0)
607'H'
608>>>
609\end{verbatim}\ecode
610%
611Out-of-range negative slice indices are truncated, but don't try this
612for single-element (non-slice) indices:
613
614\bcode\begin{verbatim}
615>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000616'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000617>>> word[-10] # error
Guido van Rossum6938f061994-08-01 12:22:53 +0000618Traceback (innermost last):
619 File "<stdin>", line 1
620IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000621>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000622\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000623%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000624The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000625pointing {\em between} characters, with the left edge of the first
626character numbered 0. Then the right edge of the last character of a
627string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000628
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000629\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000630 +---+---+---+---+---+
631 | H | e | l | p | A |
632 +---+---+---+---+---+
633 0 1 2 3 4 5
634-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000635\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000636%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000637The first row of numbers gives the position of the indices 0...5 in
638the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000639The slice from \verb\i\ to \verb\j\ consists of all characters between
640the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000641
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000642For nonnegative indices, the length of a slice is the difference of
643the indices, if both are within bounds, e.g., the length of
644\verb\word[1:3]\ is 2.
645
646The built-in function {\tt len()} returns the length of a string:
647
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000648\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000649>>> s = 'supercalifragilisticexpialidocious'
650>>> len(s)
65134
652>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000653\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000654
655\subsection{Lists}
656
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000657Python knows a number of {\em compound} data types, used to group
658together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000659can be written as a list of comma-separated values (items) between
660square brackets. List items need not all have the same type.
661
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000662\bcode\begin{verbatim}
Guido van Rossume5f8b601995-01-04 19:12:49 +0000663>>> a = ['spam', 'eggs', 100, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000664>>> a
Guido van Rossume5f8b601995-01-04 19:12:49 +0000665['spam', 'eggs', 100, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000666>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000667\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000668%
669Like string indices, list indices start at 0, and lists can be sliced,
670concatenated and so on:
671
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000672\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000673>>> a[0]
Guido van Rossume5f8b601995-01-04 19:12:49 +0000674'spam'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000675>>> a[3]
6761234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000677>>> a[-2]
678100
679>>> a[1:-1]
Guido van Rossume5f8b601995-01-04 19:12:49 +0000680['eggs', 100]
681>>> a[:2] + ['bacon', 2*2]
682['spam', 'eggs', 'bacon', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000683>>> 3*a[:3] + ['Boe!']
Guido van Rossume5f8b601995-01-04 19:12:49 +0000684['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000685>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000686\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000687%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000688Unlike strings, which are {\em immutable}, it is possible to change
689individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000690
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000691\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000692>>> a
Guido van Rossume5f8b601995-01-04 19:12:49 +0000693['spam', 'eggs', 100, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000694>>> a[2] = a[2] + 23
695>>> a
Guido van Rossume5f8b601995-01-04 19:12:49 +0000696['spam', 'eggs', 123, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000697>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000698\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000699%
700Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000701of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000702
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000703\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000704>>> # Replace some items:
Guido van Rossum6938f061994-08-01 12:22:53 +0000705... a[0:2] = [1, 12]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000706>>> a
707[1, 12, 123, 1234]
708>>> # Remove some:
Guido van Rossum6938f061994-08-01 12:22:53 +0000709... a[0:2] = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000710>>> a
711[123, 1234]
712>>> # Insert some:
Guido van Rossum6938f061994-08-01 12:22:53 +0000713... a[1:1] = ['bletch', 'xyzzy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000714>>> a
715[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000716>>> a[:0] = a # Insert (a copy of) itself at the beginning
717>>> a
718[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000719>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000720\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000721%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000722The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000723
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000724\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000725>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00007268
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000727>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000728\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000729%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000730It is possible to nest lists (create lists containing other lists),
731for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000732
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000733\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000734>>> q = [2, 3]
735>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000736>>> len(p)
7373
738>>> p[1]
739[2, 3]
740>>> p[1][0]
7412
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000742>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000743>>> p
744[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000745>>> q
746[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000747>>>
748\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000749%
750Note that in the last example, {\tt p[1]} and {\tt q} really refer to
751the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000752
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000753\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000754
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000755Of course, we can use Python for more complicated tasks than adding
756two and two together. For instance, we can write an initial
757subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000758
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000759\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000760>>> # Fibonacci series:
Guido van Rossum6938f061994-08-01 12:22:53 +0000761... # the sum of two elements defines the next
762... a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000763>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000764... print b
765... a, b = b, a+b
766...
7671
7681
7692
7703
7715
7728
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000773>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000774\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000775%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000776This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000777
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000778\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000779
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000780\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000781The first line contains a {\em multiple assignment}: the variables
782{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
783last line this is used again, demonstrating that the expressions on
784the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000785assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000786
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000787\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000788The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossum16cd7f91994-10-06 10:29:26 +000078910}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000790true; zero is false. The condition may also be a string or list value,
791in fact any sequence; anything with a non-zero length is true, empty
792sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000793comparison. The standard comparison operators are written the same as
794in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000795
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000796\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000797The {\em body} of the loop is {\em indented}: indentation is Python's
798way of grouping statements. Python does not (yet!) provide an
799intelligent input line editing facility, so you have to type a tab or
800space(s) for each indented line. In practice you will prepare more
801complicated input for Python with a text editor; most text editors have
802an auto-indent facility. When a compound statement is entered
803interactively, it must be followed by a blank line to indicate
804completion (since the parser cannot guess when you have typed the last
805line).
806
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000807\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000808The {\tt print} statement writes the value of the expression(s) it is
809given. It differs from just writing the expression you want to write
810(as we did earlier in the calculator examples) in the way it handles
Guido van Rossum16cd7f91994-10-06 10:29:26 +0000811multiple expressions and strings. Strings are printed without quotes,
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000812and a space is inserted between items, so you can format things nicely,
813like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000814
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000815\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000816>>> i = 256*256
817>>> print 'The value of i is', i
818The value of i is 65536
819>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000820\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000821%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000822A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000823
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000824\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000825>>> a, b = 0, 1
826>>> while b < 1000:
827... print b,
828... a, b = b, a+b
829...
8301 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
831>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000832\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000833%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000834Note that the interpreter inserts a newline before it prints the next
835prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000836
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000837\end{itemize}
838
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000839
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000840\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000841
Guido van Rossum4410c751991-06-04 20:22:18 +0000842Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000843usual control flow statements known from other languages, with some
844twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000845
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000846\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000847
848Perhaps the most well-known statement type is the {\tt if} statement.
849For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000850
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000851\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000852>>> if x < 0:
853... x = 0
854... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000855... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000856... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000857... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000858... print 'Single'
859... else:
860... print 'More'
861...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000862\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000863%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000864There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000865optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
866useful to avoid excessive indentation. An {\tt if...elif...elif...}
867sequence is a substitute for the {\em switch} or {\em case} statements
868found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000869
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000870\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000871
Guido van Rossum4410c751991-06-04 20:22:18 +0000872The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000873used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000874arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000875completely free in the iteration test and step (as C), Python's {\tt
876for} statement iterates over the items of any sequence (e.g., a list
877or a string), in the order that they appear in the sequence. For
878example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000879
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000880\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000881>>> # Measure some strings:
Guido van Rossum6938f061994-08-01 12:22:53 +0000882... a = ['cat', 'window', 'defenestrate']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000883>>> for x in a:
884... print x, len(x)
885...
886cat 3
887window 6
888defenestrate 12
889>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000890\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000891%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000892It is not safe to modify the sequence being iterated over in the loop
893(this can only happen for mutable sequence types, i.e., lists). If
894you need to modify the list you are iterating over, e.g., duplicate
895selected items, you must iterate over a copy. The slice notation
896makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000897
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000898\bcode\begin{verbatim}
899>>> for x in a[:]: # make a slice copy of the entire list
900... if len(x) > 6: a.insert(0, x)
901...
902>>> a
903['defenestrate', 'cat', 'window', 'defenestrate']
904>>>
905\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000906
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000907\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000908
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000909If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000910function {\tt range()} comes in handy. It generates lists containing
911arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000912
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000913\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000914>>> range(10)
915[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
916>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000917\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000918%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000919The given end point is never part of the generated list; {\tt range(10)}
920generates a list of 10 values, exactly the legal indices for items of a
921sequence of length 10. It is possible to let the range start at another
922number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000923
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000924\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000925>>> range(5, 10)
926[5, 6, 7, 8, 9]
927>>> range(0, 10, 3)
928[0, 3, 6, 9]
929>>> range(-10, -100, -30)
930[-10, -40, -70]
931>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000932\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000933%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000934To iterate over the indices of a sequence, combine {\tt range()} and
935{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000936
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000937\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000938>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000939>>> for i in range(len(a)):
940... print i, a[i]
941...
9420 Mary
9431 had
9442 a
9453 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009464 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000947>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000948\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000949
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000950\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000951
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000952The {\tt break} statement, like in C, breaks out of the smallest
953enclosing {\tt for} or {\tt while} loop.
954
955The {\tt continue} statement, also borrowed from C, continues with the
956next iteration of the loop.
957
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000958Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000959loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000960the condition becomes false (with {\tt while}), but not when the loop is
961terminated by a {\tt break} statement. This is exemplified by the
Guido van Rossumcfb45e41994-11-10 23:04:43 +0000962following loop, which searches for prime numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000963
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000964\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000965>>> for n in range(2, 10):
966... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000967... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000968... print n, 'equals', x, '*', n/x
969... break
970... else:
971... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000972...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009732 is a prime number
9743 is a prime number
9754 equals 2 * 2
9765 is a prime number
9776 equals 2 * 3
9787 is a prime number
9798 equals 2 * 4
9809 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000981>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000982\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000983
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000984\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000985
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000986The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000987It can be used when a statement is required syntactically but the
988program requires no action.
989For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000990
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000991\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000992>>> while 1:
993... pass # Busy-wait for keyboard interrupt
994...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000995\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000996
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000997\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000998
999We can create a function that writes the Fibonacci series to an
1000arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001001
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001002\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001003>>> def fib(n): # write Fibonacci series up to n
1004... a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001005... while b < n:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001006... print b,
1007... a, b = b, a+b
1008...
1009>>> # Now call the function we just defined:
Guido van Rossum6938f061994-08-01 12:22:53 +00001010... fib(2000)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000010111 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
1012>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001013\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001014%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001015The keyword {\tt def} introduces a function {\em definition}. It must
1016be followed by the function name and the parenthesized list of formal
1017parameters. The statements that form the body of the function starts at
1018the next line, indented by a tab stop.
1019
1020The {\em execution} of a function introduces a new symbol table used
1021for the local variables of the function. More precisely, all variable
1022assignments in a function store the value in the local symbol table;
1023whereas
Guido van Rossum6938f061994-08-01 12:22:53 +00001024variable references first look in the local symbol table, then
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001025in the global symbol table, and then in the table of built-in names.
1026Thus,
Guido van Rossumcfb45e41994-11-10 23:04:43 +00001027global variables cannot be directly assigned a value within a
Guido van Rossum6938f061994-08-01 12:22:53 +00001028function (unless named in a {\tt global} statement), although
1029they may be referenced.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001030
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001031The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001032the local symbol table of the called function when it is called; thus,
1033arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001034\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001035 Actually, {\em call by object reference} would be a better
1036 description, since if a mutable object is passed, the caller
1037 will see any changes the callee makes to it (e.g., items
1038 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001039}
1040When a function calls another function, a new local symbol table is
1041created for that call.
1042
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001043A function definition introduces the function name in the
1044current
1045symbol table. The value
1046of the function name
1047has a type that is recognized by the interpreter as a user-defined
1048function. This value can be assigned to another name which can then
1049also be used as a function. This serves as a general renaming
1050mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001051
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001052\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001053>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001054<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001055>>> f = fib
1056>>> f(100)
10571 1 2 3 5 8 13 21 34 55 89
1058>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001059\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001060%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001061You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001062Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001063value. In fact, technically speaking, procedures do return a value,
1064albeit a rather boring one. This value is called {\tt None} (it's a
1065built-in name). Writing the value {\tt None} is normally suppressed by
1066the interpreter if it would be the only value written. You can see it
1067if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001068
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001069\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001070>>> print fib(0)
1071None
1072>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001073\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001074%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001075It is simple to write a function that returns a list of the numbers of
1076the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001077
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001078\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001079>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001080... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001081... a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001082... while b < n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001083... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001084... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001085... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001086...
1087>>> f100 = fib2(100) # call it
1088>>> f100 # write the result
1089[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1090>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001091\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001092%
Guido van Rossum4410c751991-06-04 20:22:18 +00001093This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001094
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001095\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001096
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001097\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001098The {\tt return} statement returns with a value from a function. {\tt
1099return} without an expression argument is used to return from the middle
Guido van Rossum6938f061994-08-01 12:22:53 +00001100of a procedure (falling off the end also returns from a procedure), in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001101which case the {\tt None} value is returned.
1102
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001103\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001104The statement {\tt result.append(b)} calls a {\em method} of the list
1105object {\tt result}. A method is a function that `belongs' to an
1106object and is named {\tt obj.methodname}, where {\tt obj} is some
1107object (this may be an expression), and {\tt methodname} is the name
1108of a method that is defined by the object's type. Different types
1109define different methods. Methods of different types may have the
1110same name without causing ambiguity. (It is possible to define your
Guido van Rossum6938f061994-08-01 12:22:53 +00001111own object types and methods, using {\em classes}, as discussed later
1112in this tutorial.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001113The method {\tt append} shown in the example, is defined for
1114list objects; it adds a new element at the end of the list. In this
1115example
1116it is equivalent to {\tt result = result + [b]}, but more efficient.
1117
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001118\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001119
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001120
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001121\chapter{Odds and Ends}
1122
1123This chapter describes some things you've learned about already in
1124more detail, and adds some new things as well.
1125
1126\section{More on Lists}
1127
1128The list data type has some more methods. Here are all of the methods
1129of lists objects:
1130
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001131\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001132
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001133\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001134Insert an item at a given position. The first argument is the index of
1135the element before which to insert, so {\tt a.insert(0, x)} inserts at
1136the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1137{\tt a.append(x)}.
1138
1139\item[{\tt append(x)}]
1140Equivalent to {\tt a.insert(len(a), x)}.
1141
1142\item[{\tt index(x)}]
1143Return the index in the list of the first item whose value is {\tt x}.
1144It is an error if there is no such item.
1145
1146\item[{\tt remove(x)}]
1147Remove the first item from the list whose value is {\tt x}.
1148It is an error if there is no such item.
1149
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001150\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001151Sort the items of the list, in place.
1152
1153\item[{\tt reverse()}]
1154Reverse the elements of the list, in place.
1155
Guido van Rossum6938f061994-08-01 12:22:53 +00001156\item[{\tt count(x)}]
1157Return the number of times {\tt x} appears in the list.
1158
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001159\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001160
1161An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001162
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001163\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001164>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossum6938f061994-08-01 12:22:53 +00001165>>> print a.count(333), a.count(66.6), a.count('x')
11662 1 0
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001167>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001168>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001169>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001170[66.6, 333, -1, 333, 1, 1234.5, 333]
1171>>> a.index(333)
11721
1173>>> a.remove(333)
1174>>> a
1175[66.6, -1, 333, 1, 1234.5, 333]
1176>>> a.reverse()
1177>>> a
1178[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001179>>> a.sort()
1180>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001181[-1, 1, 66.6, 333, 333, 1234.5]
1182>>>
1183\end{verbatim}\ecode
1184
1185\section{The {\tt del} statement}
1186
1187There is a way to remove an item from a list given its index instead
1188of its value: the {\tt del} statement. This can also be used to
1189remove slices from a list (which we did earlier by assignment of an
1190empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001191
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001192\bcode\begin{verbatim}
1193>>> a
1194[-1, 1, 66.6, 333, 333, 1234.5]
1195>>> del a[0]
1196>>> a
1197[1, 66.6, 333, 333, 1234.5]
1198>>> del a[2:4]
1199>>> a
1200[1, 66.6, 1234.5]
1201>>>
1202\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001203%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001204{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001205
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001206\bcode\begin{verbatim}
1207>>> del a
1208>>>
1209\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001210%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001211Referencing the name {\tt a} hereafter is an error (at least until
1212another value is assigned to it). We'll find other uses for {\tt del}
1213later.
1214
1215\section{Tuples and Sequences}
1216
1217We saw that lists and strings have many common properties, e.g.,
Guido van Rossum6938f061994-08-01 12:22:53 +00001218indexing and slicing operations. They are two examples of {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001219sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001220sequence data types may be added. There is also another standard
1221sequence data type: the {\em tuple}.
1222
1223A tuple consists of a number of values separated by commas, for
1224instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001225
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001226\bcode\begin{verbatim}
1227>>> t = 12345, 54321, 'hello!'
1228>>> t[0]
122912345
1230>>> t
1231(12345, 54321, 'hello!')
1232>>> # Tuples may be nested:
Guido van Rossum6938f061994-08-01 12:22:53 +00001233... u = t, (1, 2, 3, 4, 5)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001234>>> u
1235((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1236>>>
1237\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001238%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001239As you see, on output tuples are alway enclosed in parentheses, so
1240that nested tuples are interpreted correctly; they may be input with
1241or without surrounding parentheses, although often parentheses are
1242necessary anyway (if the tuple is part of a larger expression).
1243
1244Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1245from a database, etc. Tuples, like strings, are immutable: it is not
1246possible to assign to the individual items of a tuple (you can
1247simulate much of the same effect with slicing and concatenation,
1248though).
1249
1250A special problem is the construction of tuples containing 0 or 1
Guido van Rossum6938f061994-08-01 12:22:53 +00001251items: the syntax has some extra quirks to accommodate these. Empty
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001252tuples are constructed by an empty pair of parentheses; a tuple with
1253one item is constructed by following a value with a comma
1254(it is not sufficient to enclose a single value in parentheses).
1255Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001256
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001257\bcode\begin{verbatim}
1258>>> empty = ()
1259>>> singleton = 'hello', # <-- note trailing comma
1260>>> len(empty)
12610
1262>>> len(singleton)
12631
1264>>> singleton
1265('hello',)
1266>>>
1267\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001268%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001269The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1270tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1271are packed together in a tuple. The reverse operation is also
1272possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001273
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001274\bcode\begin{verbatim}
1275>>> x, y, z = t
1276>>>
1277\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001278%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001279This is called, appropriately enough, {\em tuple unpacking}. Tuple
1280unpacking requires that the list of variables on the left has the same
1281number of elements as the length of the tuple. Note that multiple
1282assignment is really just a combination of tuple packing and tuple
1283unpacking!
1284
1285Occasionally, the corresponding operation on lists is useful: {\em list
1286unpacking}. This is supported by enclosing the list of variables in
1287square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001288
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001289\bcode\begin{verbatim}
Guido van Rossume5f8b601995-01-04 19:12:49 +00001290>>> a = ['spam', 'eggs', 100, 1234]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001291>>> [a1, a2, a3, a4] = a
1292>>>
1293\end{verbatim}\ecode
1294
1295\section{Dictionaries}
1296
1297Another useful data type built into Python is the {\em dictionary}.
1298Dictionaries are sometimes found in other languages as ``associative
1299memories'' or ``associative arrays''. Unlike sequences, which are
1300indexed by a range of numbers, dictionaries are indexed by {\em keys},
Guido van Rossum6938f061994-08-01 12:22:53 +00001301which are strings (the use of non-string values as keys
1302is supported, but beyond the scope of this tutorial).
1303It is best to think of a dictionary as an unordered set of
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001304{\em key:value} pairs, with the requirement that the keys are unique
1305(within one dictionary).
1306A pair of braces creates an empty dictionary: \verb/{}/.
1307Placing a comma-separated list of key:value pairs within the
1308braces adds initial key:value pairs to the dictionary; this is also the
1309way dictionaries are written on output.
1310
1311The main operations on a dictionary are storing a value with some key
1312and extracting the value given the key. It is also possible to delete
1313a key:value pair
1314with {\tt del}.
1315If you store using a key that is already in use, the old value
1316associated with that key is forgotten. It is an error to extract a
Guido van Rossum6938f061994-08-01 12:22:53 +00001317value using a non-existent key.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001318
1319The {\tt keys()} method of a dictionary object returns a list of all the
1320keys used in the dictionary, in random order (if you want it sorted,
1321just apply the {\tt sort()} method to the list of keys). To check
1322whether a single key is in the dictionary, use the \verb/has_key()/
1323method of the dictionary.
1324
1325Here is a small example using a dictionary:
1326
1327\bcode\begin{verbatim}
1328>>> tel = {'jack': 4098, 'sape': 4139}
1329>>> tel['guido'] = 4127
1330>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001331{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001332>>> tel['jack']
13334098
1334>>> del tel['sape']
1335>>> tel['irv'] = 4127
1336>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001337{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001338>>> tel.keys()
1339['guido', 'irv', 'jack']
1340>>> tel.has_key('guido')
13411
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001342>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001343\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001344
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001345\section{More on Conditions}
1346
1347The conditions used in {\tt while} and {\tt if} statements above can
1348contain other operators besides comparisons.
1349
1350The comparison operators {\tt in} and {\tt not in} check whether a value
1351occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1352is not} compare whether two objects are really the same object; this
1353only matters for mutable objects like lists. All comparison operators
1354have the same priority, which is lower than that of all numerical
1355operators.
1356
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001357Comparisons can be chained: e.g., {\tt a < b == c} tests whether {\tt a}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001358is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1359
1360Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1361or}, and the outcome of a comparison (or of any other Boolean
1362expression) may be negated with {\tt not}. These all have lower
1363priorities than comparison operators again; between them, {\tt not} has
1364the highest priority, and {\tt or} the lowest, so that
1365{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1366course, parentheses can be used to express the desired composition.
1367
1368The Boolean operators {\tt and} and {\tt or} are so-called {\em
1369shortcut} operators: their arguments are evaluated from left to right,
1370and evaluation stops as soon as the outcome is determined. E.g., if
1371{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1372does not evaluate the expression C. In general, the return value of a
1373shortcut operator, when used as a general value and not as a Boolean, is
1374the last evaluated argument.
1375
1376It is possible to assign the result of a comparison or other Boolean
Guido van Rossum6938f061994-08-01 12:22:53 +00001377expression to a variable. For example,
1378
1379\bcode\begin{verbatim}
1380>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
1381>>> non_null = string1 or string2 or string3
1382>>> non_null
1383'Trondheim'
1384>>>
1385\end{verbatim}\ecode
1386%
1387Note that in Python, unlike C, assignment cannot occur inside expressions.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001388
1389\section{Comparing Sequences and Other Types}
1390
1391Sequence objects may be compared to other objects with the same
1392sequence type. The comparison uses {\em lexicographical} ordering:
1393first the first two items are compared, and if they differ this
1394determines the outcome of the comparison; if they are equal, the next
1395two items are compared, and so on, until either sequence is exhausted.
1396If two items to be compared are themselves sequences of the same type,
Guido van Rossum6938f061994-08-01 12:22:53 +00001397the lexicographical comparison is carried out recursively. If all
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001398items of two sequences compare equal, the sequences are considered
1399equal. If one sequence is an initial subsequence of the other, the
1400shorted sequence is the smaller one. Lexicographical ordering for
1401strings uses the ASCII ordering for individual characters. Some
1402examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001403
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001404\bcode\begin{verbatim}
1405(1, 2, 3) < (1, 2, 4)
1406[1, 2, 3] < [1, 2, 4]
1407'ABC' < 'C' < 'Pascal' < 'Python'
1408(1, 2, 3, 4) < (1, 2, 4)
1409(1, 2) < (1, 2, -1)
1410(1, 2, 3) = (1.0, 2.0, 3.0)
1411(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1412\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001413%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001414Note that comparing objects of different types is legal. The outcome
1415is deterministic but arbitrary: the types are ordered by their name.
1416Thus, a list is always smaller than a string, a string is always
1417smaller than a tuple, etc. Mixed numeric types are compared according
1418to their numeric value, so 0 equals 0.0, etc.%
1419\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001420 The rules for comparing objects of different types should
1421 not be relied upon; they may change in a future version of
1422 the language.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001423}
1424
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001425
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001426\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001427
Guido van Rossum4410c751991-06-04 20:22:18 +00001428If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001429definitions you have made (functions and variables) are lost.
1430Therefore, if you want to write a somewhat longer program, you are
1431better off using a text editor to prepare the input for the interpreter
Guido van Rossum16d6e711994-08-08 12:30:22 +00001432and running it with that file as input instead. This is known as creating a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001433{\em script}. As your program gets longer, you may want to split it
1434into several files for easier maintenance. You may also want to use a
1435handy function that you've written in several programs without copying
1436its definition into each program.
1437
Guido van Rossum4410c751991-06-04 20:22:18 +00001438To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001439them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001440Such a file is called a {\em module}; definitions from a module can be
1441{\em imported} into other modules or into the {\em main} module (the
1442collection of variables that you have access to in a script
1443executed at the top level
1444and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001445
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001446A module is a file containing Python definitions and statements. The
Guido van Rossum6938f061994-08-01 12:22:53 +00001447file name is the module name with the suffix {\tt .py} appended. Within
1448a module, the module's name (as a string) is available as the value of
1449the global variable {\tt __name__}. For instance, use your favorite text
1450editor to create a file called {\tt fibo.py} in the current directory
1451with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001452
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001453\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001454# Fibonacci numbers module
1455
1456def fib(n): # write Fibonacci series up to n
1457 a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001458 while b < n:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001459 print b,
1460 a, b = b, a+b
1461
1462def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001463 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001464 a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001465 while b < n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001466 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001467 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001468 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001469\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001470%
Guido van Rossum4410c751991-06-04 20:22:18 +00001471Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001472following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001473
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001474\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001475>>> import fibo
1476>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001477\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001478%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001479This does not enter the names of the functions defined in
1480{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001481directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001482{\tt fibo}
1483there.
1484Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001485
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001486\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001487>>> fibo.fib(1000)
14881 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1489>>> fibo.fib2(100)
1490[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Guido van Rossum6938f061994-08-01 12:22:53 +00001491>>> fibo.__name__
1492'fibo'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001493>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001494\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001495%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001496If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001497
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001498\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001499>>> fib = fibo.fib
1500>>> fib(500)
15011 1 2 3 5 8 13 21 34 55 89 144 233 377
1502>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001503\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001504
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001505\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001506
1507A module can contain executable statements as well as function
1508definitions.
1509These statements are intended to initialize the module.
1510They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001511{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001512time the module is imported somewhere.%
1513\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001514 In fact function definitions are also `statements' that are
1515 `executed'; the execution enters the function name in the
1516 module's global symbol table.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001517}
1518
1519Each module has its own private symbol table, which is used as the
1520global symbol table by all functions defined in the module.
1521Thus, the author of a module can use global variables in the module
1522without worrying about accidental clashes with a user's global
1523variables.
1524On the other hand, if you know what you are doing you can touch a
1525module's global variables with the same notation used to refer to its
1526functions,
1527{\tt modname.itemname}.
1528
1529Modules can import other modules.
1530It is customary but not required to place all
1531{\tt import}
1532statements at the beginning of a module (or script, for that matter).
1533The imported module names are placed in the importing module's global
1534symbol table.
1535
1536There is a variant of the
1537{\tt import}
1538statement that imports names from a module directly into the importing
1539module's symbol table.
1540For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001541
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001542\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001543>>> from fibo import fib, fib2
1544>>> fib(500)
15451 1 2 3 5 8 13 21 34 55 89 144 233 377
1546>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001547\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001548%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001549This does not introduce the module name from which the imports are taken
1550in the local symbol table (so in the example, {\tt fibo} is not
1551defined).
1552
1553There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001554
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001555\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001556>>> from fibo import *
1557>>> fib(500)
15581 1 2 3 5 8 13 21 34 55 89 144 233 377
1559>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001560\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001561%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001562This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001563({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001564
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001565\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001566
Guido van Rossum4410c751991-06-04 20:22:18 +00001567Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001568document (Python Library Reference). Some modules are built into the
1569interpreter; these provide access to operations that are not part of the
1570core of the language but are nevertheless built in, either for
1571efficiency or to provide access to operating system primitives such as
1572system calls. The set of such modules is a configuration option; e.g.,
1573the {\tt amoeba} module is only provided on systems that somehow support
1574Amoeba primitives. One particular module deserves some attention: {\tt
1575sys}, which is built into every Python interpreter. The variables {\tt
1576sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1577secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001578
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001579\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001580>>> import sys
1581>>> sys.ps1
1582'>>> '
1583>>> sys.ps2
1584'... '
1585>>> sys.ps1 = 'C> '
1586C> print 'Yuck!'
1587Yuck!
1588C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001589\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001590%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001591These two variables are only defined if the interpreter is in
1592interactive mode.
1593
1594The variable
1595{\tt sys.path}
1596is a list of strings that determine the interpreter's search path for
1597modules.
1598It is initialized to a default path taken from the environment variable
1599{\tt PYTHONPATH},
1600or from a built-in default if
1601{\tt PYTHONPATH}
1602is not set.
1603You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001604
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001605\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001606>>> import sys
1607>>> sys.path.append('/ufs/guido/lib/python')
1608>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001609\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001610
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001611\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001612
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001613The built-in function {\tt dir} is used to find out which names a module
1614defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001615
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001616\bcode\begin{verbatim}
1617>>> import fibo, sys
1618>>> dir(fibo)
Guido van Rossum6938f061994-08-01 12:22:53 +00001619['__name__', 'fib', 'fib2']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001620>>> dir(sys)
Guido van Rossum6938f061994-08-01 12:22:53 +00001621['__name__', 'argv', 'builtin_module_names', 'copyright', 'exit',
1622'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',
1623'stderr', 'stdin', 'stdout', 'version']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001624>>>
1625\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001626%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001627Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001628
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001629\bcode\begin{verbatim}
1630>>> a = [1, 2, 3, 4, 5]
1631>>> import fibo, sys
1632>>> fib = fibo.fib
1633>>> dir()
Guido van Rossum6938f061994-08-01 12:22:53 +00001634['__name__', 'a', 'fib', 'fibo', 'sys']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001635>>>
1636\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001637%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001638Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001639
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001640{\tt dir()} does not list the names of built-in functions and variables.
1641If you want a list of those, they are defined in the standard module
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001642{\tt __builtin__}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001643
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001644\bcode\begin{verbatim}
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001645>>> import __builtin__
1646>>> dir(__builtin__)
Guido van Rossum6938f061994-08-01 12:22:53 +00001647['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError',
1648'ImportError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
1649'MemoryError', 'NameError', 'None', 'OverflowError', 'RuntimeError',
1650'SyntaxError', 'SystemError', 'SystemExit', 'TypeError', 'ValueError',
1651'ZeroDivisionError', '__name__', 'abs', 'apply', 'chr', 'cmp', 'coerce',
1652'compile', 'dir', 'divmod', 'eval', 'execfile', 'filter', 'float',
1653'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'len', 'long',
1654'map', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input',
1655'reduce', 'reload', 'repr', 'round', 'setattr', 'str', 'type', 'xrange']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001656>>>
1657\end{verbatim}\ecode
1658
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001659
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001660\chapter{Output Formatting}
1661
1662So far we've encountered two ways of writing values: {\em expression
1663statements} and the {\tt print} statement. (A third way is using the
1664{\tt write} method of file objects; the standard output file can be
1665referenced as {\tt sys.stdout}. See the Library Reference for more
1666information on this.)
1667
1668Often you'll want more control over the formatting of your output than
1669simply printing space-separated values. The key to nice formatting in
1670Python is to do all the string handling yourself; using string slicing
1671and concatenation operations you can create any lay-out you can imagine.
1672The standard module {\tt string} contains some useful operations for
1673padding strings to a given column width; these will be discussed shortly.
Guido van Rossum6938f061994-08-01 12:22:53 +00001674Finally, the \code{\%} operator (modulo) with a string left argument
1675interprets this string as a C sprintf format string to be applied to the
1676right argument, and returns the string resulting from this formatting
1677operation.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001678
1679One question remains, of course: how do you convert values to strings?
1680Luckily, Python has a way to convert any value to a string: just write
1681the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001682
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001683\bcode\begin{verbatim}
1684>>> x = 10 * 3.14
1685>>> y = 200*200
1686>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1687>>> print s
1688The value of x is 31.4, and y is 40000...
1689>>> # Reverse quotes work on other types besides numbers:
Guido van Rossum6938f061994-08-01 12:22:53 +00001690... p = [x, y]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001691>>> ps = `p`
1692>>> ps
1693'[31.4, 40000]'
1694>>> # Converting a string adds string quotes and backslashes:
Guido van Rossum6938f061994-08-01 12:22:53 +00001695... hello = 'hello, world\n'
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001696>>> hellos = `hello`
1697>>> print hellos
1698'hello, world\012'
1699>>> # The argument of reverse quotes may be a tuple:
Guido van Rossume5f8b601995-01-04 19:12:49 +00001700... `x, y, ('spam', 'eggs')`
1701"(31.4, 40000, ('spam', 'eggs'))"
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001702>>>
1703\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001704%
Guido van Rossum6938f061994-08-01 12:22:53 +00001705Here are two ways to write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001706
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001707\bcode\begin{verbatim}
1708>>> import string
1709>>> for x in range(1, 11):
1710... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1711... # Note trailing comma on previous line
1712... print string.rjust(`x*x*x`, 4)
1713...
1714 1 1 1
1715 2 4 8
1716 3 9 27
1717 4 16 64
1718 5 25 125
1719 6 36 216
1720 7 49 343
1721 8 64 512
1722 9 81 729
172310 100 1000
Guido van Rossum6938f061994-08-01 12:22:53 +00001724>>> for x in range(1,11):
1725... print '%2d %3d %4d' % (x, x*x, x*x*x)
1726...
1727 1 1 1
1728 2 4 8
1729 3 9 27
1730 4 16 64
1731 5 25 125
1732 6 36 216
1733 7 49 343
1734 8 64 512
1735 9 81 729
173610 100 1000
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001737>>>
1738\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001739%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001740(Note that one space between each column was added by the way {\tt print}
1741works: it always adds spaces between its arguments.)
1742
1743This example demonstrates the function {\tt string.rjust()}, which
1744right-justifies a string in a field of a given width by padding it with
1745spaces on the left. There are similar functions {\tt string.ljust()}
1746and {\tt string.center()}. These functions do not write anything, they
1747just return a new string. If the input string is too long, they don't
1748truncate it, but return it unchanged; this will mess up your column
1749lay-out but that's usually better than the alternative, which would be
1750lying about a value. (If you really want truncation you can always add
1751a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1752
1753There is another function, {\tt string.zfill}, which pads a numeric
1754string on the left with zeros. It understands about plus and minus
Guido van Rossum6938f061994-08-01 12:22:53 +00001755signs:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001756
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001757\bcode\begin{verbatim}
1758>>> string.zfill('12', 5)
1759'00012'
1760>>> string.zfill('-3.14', 7)
1761'-003.14'
1762>>> string.zfill('3.14159265359', 5)
1763'3.14159265359'
1764>>>
1765\end{verbatim}\ecode
1766
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001767
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001768\chapter{Errors and Exceptions}
1769
1770Until now error messages haven't been more than mentioned, but if you
1771have tried out the examples you have probably seen some. There are
1772(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1773and {\em exceptions}.
1774
1775\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001776
1777Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001778kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001779
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001780\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001781>>> while 1 print 'Hello world'
Guido van Rossum6938f061994-08-01 12:22:53 +00001782 File "<stdin>", line 1
1783 while 1 print 'Hello world'
1784 ^
1785SyntaxError: invalid syntax
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001786>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001787\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001788%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001789The parser repeats the offending line and displays a little `arrow'
1790pointing at the earliest point in the line where the error was detected.
1791The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001792{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001793the arrow: in the example, the error is detected at the keyword
1794{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001795File name and line number are printed so you know where to look in case
1796the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001797
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001798\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001799
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001800Even if a statement or expression is syntactically correct, it may
1801cause an error when an attempt is made to execute it.
1802Errors detected during execution are called {\em exceptions} and are
1803not unconditionally fatal: you will soon learn how to handle them in
1804Python programs. Most exceptions are not handled by programs,
1805however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001806
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001807\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001808>>> 10 * (1/0)
Guido van Rossum3cbc16d1993-12-17 12:13:53 +00001809Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001810 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001811ZeroDivisionError: integer division or modulo
Guido van Rossume5f8b601995-01-04 19:12:49 +00001812>>> 4 + spam*3
Guido van Rossum6938f061994-08-01 12:22:53 +00001813Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001814 File "<stdin>", line 1
Guido van Rossume5f8b601995-01-04 19:12:49 +00001815NameError: spam
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001816>>> '2' + 2
Guido van Rossum6938f061994-08-01 12:22:53 +00001817Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001818 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001819TypeError: illegal argument type for built-in operation
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001820>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001821\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001822%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001823The last line of the error message indicates what happened.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001824Exceptions come in different types, and the type is printed as part of
1825the message: the types in the example are
Guido van Rossumb2c65561993-05-12 08:53:36 +00001826{\tt ZeroDivisionError},
1827{\tt NameError}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001828and
Guido van Rossumb2c65561993-05-12 08:53:36 +00001829{\tt TypeError}.
1830The string printed as the exception type is the name of the built-in
1831name for the exception that occurred. This is true for all built-in
1832exceptions, but need not be true for user-defined exceptions (although
1833it is a useful convention).
1834Standard exception names are built-in identifiers (not reserved
1835keywords).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001836
Guido van Rossumb2c65561993-05-12 08:53:36 +00001837The rest of the line is a detail whose interpretation depends on the
1838exception type; its meaning is dependent on the exception type.
1839
1840The preceding part of the error message shows the context where the
1841exception happened, in the form of a stack backtrace.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001842In general it contains a stack backtrace listing source lines; however,
1843it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001844
Guido van Rossumb2c65561993-05-12 08:53:36 +00001845The Python library reference manual lists the built-in exceptions and
1846their meanings.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001847
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001848\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001849
1850It is possible to write programs that handle selected exceptions.
1851Look at the following example, which prints a table of inverses of
1852some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001853
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001854\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001855>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001856>>> for x in numbers:
1857... print x,
1858... try:
1859... print 1.0 / x
Guido van Rossumb2c65561993-05-12 08:53:36 +00001860... except ZeroDivisionError:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001861... print '*** has no inverse ***'
1862...
18630.3333 3.00030003
18642.5 0.4
18650 *** has no inverse ***
186610 0.1
1867>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001868\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001869%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001870The {\tt try} statement works as follows.
1871\begin{itemize}
1872\item
1873First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001874{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001875(the statement(s) between the {\tt try} and {\tt except} keywords) is
1876executed.
1877\item
1878If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001879{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001880is skipped and execution of the {\tt try} statement is finished.
1881\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001882If an exception occurs during execution of the try clause,
1883the rest of the clause is skipped. Then if
1884its type matches the exception named after the {\tt except} keyword,
1885the rest of the try clause is skipped, the except clause is executed,
1886and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001887\item
1888If an exception occurs which does not match the exception named in the
1889except clause, it is passed on to outer try statements; if no handler is
1890found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001891{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001892and execution stops with a message as shown above.
1893\end{itemize}
1894A {\tt try} statement may have more than one except clause, to specify
1895handlers for different exceptions.
1896At most one handler will be executed.
1897Handlers only handle exceptions that occur in the corresponding try
1898clause, not in other handlers of the same {\tt try} statement.
1899An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001900e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001901
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001902\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001903... except (RuntimeError, TypeError, NameError):
1904... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001905\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001906%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001907The last except clause may omit the exception name(s), to serve as a
1908wildcard.
Guido van Rossumb2c65561993-05-12 08:53:36 +00001909Use this with extreme caution, since it is easy to mask a real
1910programming error in this way!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001911
1912When an exception occurs, it may have an associated value, also known as
1913the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001914{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001915The presence and type of the argument depend on the exception type.
1916For exception types which have an argument, the except clause may
1917specify a variable after the exception name (or list) to receive the
1918argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001919
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001920\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001921>>> try:
Guido van Rossume5f8b601995-01-04 19:12:49 +00001922... spam()
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001923... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001924... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001925...
Guido van Rossume5f8b601995-01-04 19:12:49 +00001926name spam undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001927>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001928\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001929%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001930If an exception has an argument, it is printed as the last part
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001931(`detail') of the message for unhandled exceptions.
1932
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001933Exception handlers don't just handle exceptions if they occur
1934immediately in the try clause, but also if they occur inside functions
1935that are called (even indirectly) in the try clause.
1936For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001937
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001938\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001939>>> def this_fails():
1940... x = 1/0
1941...
1942>>> try:
1943... this_fails()
Guido van Rossumb2c65561993-05-12 08:53:36 +00001944... except ZeroDivisionError, detail:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001945... print 'Handling run-time error:', detail
1946...
Guido van Rossumb2c65561993-05-12 08:53:36 +00001947Handling run-time error: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001948>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001949\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001950
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001951\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001952
1953The {\tt raise} statement allows the programmer to force a specified
1954exception to occur.
1955For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001956
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001957\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001958>>> raise NameError, 'HiThere'
Guido van Rossum6938f061994-08-01 12:22:53 +00001959Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001960 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001961NameError: HiThere
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001962>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001963\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001964%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001965The first argument to {\tt raise} names the exception to be raised.
1966The optional second argument specifies the exception's argument.
1967
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001968\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001969
1970Programs may name their own exceptions by assigning a string to a
1971variable.
1972For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001973
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001974\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001975>>> my_exc = 'my_exc'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001976>>> try:
1977... raise my_exc, 2*2
1978... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001979... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001980...
Guido van Rossum6938f061994-08-01 12:22:53 +00001981My exception occurred, value: 4
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001982>>> raise my_exc, 1
Guido van Rossum6938f061994-08-01 12:22:53 +00001983Traceback (innermost last):
1984 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001985my_exc: 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001986>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001987\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001988%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001989Many standard modules use this to report errors that may occur in
1990functions they define.
1991
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001992\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001993
1994The {\tt try} statement has another optional clause which is intended to
1995define clean-up actions that must be executed under all circumstances.
1996For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001997
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001998\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001999>>> try:
2000... raise KeyboardInterrupt
2001... finally:
2002... print 'Goodbye, world!'
2003...
2004Goodbye, world!
Guido van Rossum6938f061994-08-01 12:22:53 +00002005Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002006 File "<stdin>", line 2
Guido van Rossumb2c65561993-05-12 08:53:36 +00002007KeyboardInterrupt
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002008>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002009\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002010%
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00002011A {\tt finally} clause is executed whether or not an exception has
2012occurred in the {\tt try} clause. When an exception has occurred, it
Guido van Rossum6938f061994-08-01 12:22:53 +00002013is re-raised after the {\tt finally} clause is executed. The
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00002014{\tt finally} clause is also executed ``on the way out'' when the
2015{\tt try} statement is left via a {\tt break} or {\tt return}
2016statement.
2017
2018A {\tt try} statement must either have one or more {\tt except}
2019clauses or one {\tt finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002020
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002021
2022\chapter{Classes}
2023
2024Python's class mechanism adds classes to the language with a minimum
2025of new syntax and semantics. It is a mixture of the class mechanisms
Guido van Rossum16d6e711994-08-08 12:30:22 +00002026found in \Cpp{} and Modula-3. As is true for modules, classes in Python
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002027do not put an absolute barrier between definition and user, but rather
2028rely on the politeness of the user not to ``break into the
2029definition.'' The most important features of classes are retained
2030with full power, however: the class inheritance mechanism allows
2031multiple base classes, a derived class can override any methods of its
2032base class(es), a method can call the method of a base class with the
2033same name. Objects can contain an arbitrary amount of private data.
2034
Guido van Rossum16d6e711994-08-08 12:30:22 +00002035In \Cpp{} terminology, all class members (including the data members) are
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002036{\em public}, and all member functions are {\em virtual}. There are
Guido van Rossum6938f061994-08-01 12:22:53 +00002037no special constructors or destructors. As in Modula-3, there are no
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002038shorthands for referencing the object's members from its methods: the
2039method function is declared with an explicit first argument
2040representing the object, which is provided implicitly by the call. As
2041in Smalltalk, classes themselves are objects, albeit in the wider
2042sense of the word: in Python, all data types are objects. This
Guido van Rossum16d6e711994-08-08 12:30:22 +00002043provides semantics for importing and renaming. But, just like in \Cpp{}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002044or Modula-3, built-in types cannot be used as base classes for
Guido van Rossum16d6e711994-08-08 12:30:22 +00002045extension by the user. Also, like in \Cpp{} but unlike in Modula-3, most
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002046built-in operators with special syntax (arithmetic operators,
Guido van Rossum6938f061994-08-01 12:22:53 +00002047subscripting etc.) can be redefined for class members.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002048
2049
2050\section{A word about terminology}
2051
2052Lacking universally accepted terminology to talk about classes, I'll
Guido van Rossum16d6e711994-08-08 12:30:22 +00002053make occasional use of Smalltalk and \Cpp{} terms. (I'd use Modula-3
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002054terms, since its object-oriented semantics are closer to those of
Guido van Rossum16d6e711994-08-08 12:30:22 +00002055Python than \Cpp{}, but I expect that few readers have heard of it...)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002056
2057I also have to warn you that there's a terminological pitfall for
2058object-oriented readers: the word ``object'' in Python does not
Guido van Rossum16d6e711994-08-08 12:30:22 +00002059necessarily mean a class instance. Like \Cpp{} and Modula-3, and unlike
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002060Smalltalk, not all types in Python are classes: the basic built-in
2061types like integers and lists aren't, and even somewhat more exotic
2062types like files aren't. However, {\em all} Python types share a little
2063bit of common semantics that is best described by using the word
2064object.
2065
2066Objects have individuality, and multiple names (in multiple scopes)
2067can be bound to the same object. This is known as aliasing in other
2068languages. This is usually not appreciated on a first glance at
2069Python, and can be safely ignored when dealing with immutable basic
2070types (numbers, strings, tuples). However, aliasing has an
Guido van Rossum6938f061994-08-01 12:22:53 +00002071(intended!) effect on the semantics of Python code involving mutable
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002072objects such as lists, dictionaries, and most types representing
2073entities outside the program (files, windows, etc.). This is usually
2074used to the benefit of the program, since aliases behave like pointers
2075in some respects. For example, passing an object is cheap since only
2076a pointer is passed by the implementation; and if a function modifies
2077an object passed as an argument, the caller will see the change --- this
2078obviates the need for two different argument passing mechanisms as in
2079Pascal.
2080
2081
2082\section{Python scopes and name spaces}
2083
2084Before introducing classes, I first have to tell you something about
2085Python's scope rules. Class definitions play some neat tricks with
2086name spaces, and you need to know how scopes and name spaces work to
2087fully understand what's going on. Incidentally, knowledge about this
2088subject is useful for any advanced Python programmer.
2089
2090Let's begin with some definitions.
2091
2092A {\em name space} is a mapping from names to objects. Most name
2093spaces are currently implemented as Python dictionaries, but that's
2094normally not noticeable in any way (except for performance), and it
2095may change in the future. Examples of name spaces are: the set of
2096built-in names (functions such as \verb\abs()\, and built-in exception
2097names); the global names in a module; and the local names in a
2098function invocation. In a sense the set of attributes of an object
Guido van Rossum16cd7f91994-10-06 10:29:26 +00002099also form a name space. The important thing to know about name
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002100spaces is that there is absolutely no relation between names in
2101different name spaces; for instance, two different modules may both
2102define a function ``maximize'' without confusion --- users of the
2103modules must prefix it with the module name.
2104
2105By the way, I use the word {\em attribute} for any name following a
2106dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2107an attribute of the object \verb\z\. Strictly speaking, references to
2108names in modules are attribute references: in the expression
2109\verb\modname.funcname\, \verb\modname\ is a module object and
2110\verb\funcname\ is an attribute of it. In this case there happens to
2111be a straightforward mapping between the module's attributes and the
2112global names defined in the module: they share the same name space!%
2113\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00002114 Except for one thing. Module objects have a secret read-only
2115 attribute called {\tt __dict__} which returns the dictionary
2116 used to implement the module's name space; the name
2117 {\tt __dict__} is an attribute but not a global name.
2118 Obviously, using this violates the abstraction of name space
2119 implementation, and should be restricted to things like
2120 post-mortem debuggers...
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002121}
2122
2123Attributes may be read-only or writable. In the latter case,
2124assignment to attributes is possible. Module attributes are writable:
2125you can write \verb\modname.the_answer = 42\. Writable attributes may
2126also be deleted with the del statement, e.g.
2127\verb\del modname.the_answer\.
2128
2129Name spaces are created at different moments and have different
2130lifetimes. The name space containing the built-in names is created
2131when the Python interpreter starts up, and is never deleted. The
2132global name space for a module is created when the module definition
2133is read in; normally, module name spaces also last until the
2134interpreter quits. The statements executed by the top-level
2135invocation of the interpreter, either read from a script file or
2136interactively, are considered part of a module called \verb\__main__\,
2137so they have their own global name space. (The built-in names
Guido van Rossum4bd023f1993-10-27 13:49:20 +00002138actually also live in a module; this is called \verb\__builtin__\.)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002139
2140The local name space for a function is created when the function is
2141called, and deleted when the function returns or raises an exception
2142that is not handled within the function. (Actually, forgetting would
2143be a better way to describe what actually happens.) Of course,
2144recursive invocations each have their own local name space.
2145
2146A {\em scope} is a textual region of a Python program where a name space
2147is directly accessible. ``Directly accessible'' here means that an
2148unqualified reference to a name attempts to find the name in the name
2149space.
2150
2151Although scopes are determined statically, they are used dynamically.
2152At any time during execution, exactly three nested scopes are in use
2153(i.e., exactly three name spaces are directly accessible): the
2154innermost scope, which is searched first, contains the local names,
2155the middle scope, searched next, contains the current module's global
2156names, and the outermost scope (searched last) is the name space
2157containing built-in names.
2158
2159Usually, the local scope references the local names of the (textually)
Guido van Rossum16cd7f91994-10-06 10:29:26 +00002160current function. Outside of functions, the the local scope references
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002161the same name space as the global scope: the module's name space.
2162Class definitions place yet another name space in the local scope.
2163
2164It is important to realize that scopes are determined textually: the
2165global scope of a function defined in a module is that module's name
2166space, no matter from where or by what alias the function is called.
2167On the other hand, the actual search for names is done dynamically, at
2168run time --- however, the the language definition is evolving towards
2169static name resolution, at ``compile'' time, so don't rely on dynamic
2170name resolution! (In fact, local variables are already determined
2171statically.)
2172
2173A special quirk of Python is that assignments always go into the
2174innermost scope. Assignments do not copy data --- they just
2175bind names to objects. The same is true for deletions: the statement
2176\verb\del x\ removes the binding of x from the name space referenced by the
2177local scope. In fact, all operations that introduce new names use the
2178local scope: in particular, import statements and function definitions
2179bind the module or function name in the local scope. (The
2180\verb\global\ statement can be used to indicate that particular
2181variables live in the global scope.)
2182
2183
2184\section{A first look at classes}
2185
2186Classes introduce a little bit of new syntax, three new object types,
2187and some new semantics.
2188
2189
2190\subsection{Class definition syntax}
2191
2192The simplest form of class definition looks like this:
2193
2194\begin{verbatim}
2195 class ClassName:
2196 <statement-1>
2197 .
2198 .
2199 .
2200 <statement-N>
2201\end{verbatim}
2202
2203Class definitions, like function definitions (\verb\def\ statements)
2204must be executed before they have any effect. (You could conceivably
2205place a class definition in a branch of an \verb\if\ statement, or
2206inside a function.)
2207
2208In practice, the statements inside a class definition will usually be
2209function definitions, but other statements are allowed, and sometimes
2210useful --- we'll come back to this later. The function definitions
2211inside a class normally have a peculiar form of argument list,
2212dictated by the calling conventions for methods --- again, this is
2213explained later.
2214
2215When a class definition is entered, a new name space is created, and
2216used as the local scope --- thus, all assignments to local variables
2217go into this new name space. In particular, function definitions bind
2218the name of the new function here.
2219
2220When a class definition is left normally (via the end), a {\em class
2221object} is created. This is basically a wrapper around the contents
2222of the name space created by the class definition; we'll learn more
2223about class objects in the next section. The original local scope
2224(the one in effect just before the class definitions was entered) is
2225reinstated, and the class object is bound here to class name given in
2226the class definition header (ClassName in the example).
2227
2228
2229\subsection{Class objects}
2230
2231Class objects support two kinds of operations: attribute references
2232and instantiation.
2233
2234{\em Attribute references} use the standard syntax used for all
2235attribute references in Python: \verb\obj.name\. Valid attribute
2236names are all the names that were in the class's name space when the
2237class object was created. So, if the class definition looked like
2238this:
2239
2240\begin{verbatim}
2241 class MyClass:
2242 i = 12345
2243 def f(x):
2244 return 'hello world'
2245\end{verbatim}
2246
2247then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
2248references, returning an integer and a function object, respectively.
2249Class attributes can also be assigned to, so you can change the
2250value of \verb\MyClass.i\ by assignment.
2251
2252Class {\em instantiation} uses function notation. Just pretend that
2253the class object is a parameterless function that returns a new
2254instance of the class. For example, (assuming the above class):
2255
2256\begin{verbatim}
2257 x = MyClass()
2258\end{verbatim}
2259
2260creates a new {\em instance} of the class and assigns this object to
2261the local variable \verb\x\.
2262
2263
2264\subsection{Instance objects}
2265
2266Now what can we do with instance objects? The only operations
2267understood by instance objects are attribute references. There are
2268two kinds of valid attribute names.
2269
2270The first I'll call {\em data attributes}. These correspond to
Guido van Rossum16d6e711994-08-08 12:30:22 +00002271``instance variables'' in Smalltalk, and to ``data members'' in \Cpp{}.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002272Data attributes need not be declared; like local variables, they
2273spring into existence when they are first assigned to. For example,
2274if \verb\x\ in the instance of \verb\MyClass\ created above, the
2275following piece of code will print the value 16, without leaving a
2276trace:
2277
2278\begin{verbatim}
2279 x.counter = 1
2280 while x.counter < 10:
2281 x.counter = x.counter * 2
2282 print x.counter
2283 del x.counter
2284\end{verbatim}
2285
2286The second kind of attribute references understood by instance objects
2287are {\em methods}. A method is a function that ``belongs to'' an
2288object. (In Python, the term method is not unique to class instances:
2289other object types can have methods as well, e.g., list objects have
2290methods called append, insert, remove, sort, and so on. However,
2291below, we'll use the term method exclusively to mean methods of class
2292instance objects, unless explicitly stated otherwise.)
2293
2294Valid method names of an instance object depend on its class. By
2295definition, all attributes of a class that are (user-defined) function
2296objects define corresponding methods of its instances. So in our
2297example, \verb\x.f\ is a valid method reference, since
2298\verb\MyClass.f\ is a function, but \verb\x.i\ is not, since
2299\verb\MyClass.i\ is not. But \verb\x.f\ is not the
2300same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a
2301function object.
2302
2303
2304\subsection{Method objects}
2305
2306Usually, a method is called immediately, e.g.:
2307
2308\begin{verbatim}
2309 x.f()
2310\end{verbatim}
2311
2312In our example, this will return the string \verb\'hello world'\.
2313However, it is not necessary to call a method right away: \verb\x.f\
2314is a method object, and can be stored away and called at a later
2315moment, for example:
2316
2317\begin{verbatim}
2318 xf = x.f
2319 while 1:
2320 print xf()
2321\end{verbatim}
2322
2323will continue to print \verb\hello world\ until the end of time.
2324
2325What exactly happens when a method is called? You may have noticed
2326that \verb\x.f()\ was called without an argument above, even though
2327the function definition for \verb\f\ specified an argument. What
2328happened to the argument? Surely Python raises an exception when a
2329function that requires an argument is called without any --- even if
2330the argument isn't actually used...
2331
2332Actually, you may have guessed the answer: the special thing about
2333methods is that the object is passed as the first argument of the
2334function. In our example, the call \verb\x.f()\ is exactly equivalent
2335to \verb\MyClass.f(x)\. In general, calling a method with a list of
2336{\em n} arguments is equivalent to calling the corresponding function
2337with an argument list that is created by inserting the method's object
2338before the first argument.
2339
2340If you still don't understand how methods work, a look at the
2341implementation can perhaps clarify matters. When an instance
2342attribute is referenced that isn't a data attribute, its class is
2343searched. If the name denotes a valid class attribute that is a
2344function object, a method object is created by packing (pointers to)
2345the instance object and the function object just found together in an
2346abstract object: this is the method object. When the method object is
2347called with an argument list, it is unpacked again, a new argument
2348list is constructed from the instance object and the original argument
2349list, and the function object is called with this new argument list.
2350
2351
2352\section{Random remarks}
2353
2354
2355[These should perhaps be placed more carefully...]
2356
2357
2358Data attributes override method attributes with the same name; to
2359avoid accidental name conflicts, which may cause hard-to-find bugs in
2360large programs, it is wise to use some kind of convention that
2361minimizes the chance of conflicts, e.g., capitalize method names,
2362prefix data attribute names with a small unique string (perhaps just
Guido van Rossum6938f061994-08-01 12:22:53 +00002363an underscore), or use verbs for methods and nouns for data attributes.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002364
2365
2366Data attributes may be referenced by methods as well as by ordinary
2367users (``clients'') of an object. In other words, classes are not
2368usable to implement pure abstract data types. In fact, nothing in
2369Python makes it possible to enforce data hiding --- it is all based
2370upon convention. (On the other hand, the Python implementation,
2371written in C, can completely hide implementation details and control
2372access to an object if necessary; this can be used by extensions to
2373Python written in C.)
2374
2375
2376Clients should use data attributes with care --- clients may mess up
2377invariants maintained by the methods by stamping on their data
2378attributes. Note that clients may add data attributes of their own to
2379an instance object without affecting the validity of the methods, as
2380long as name conflicts are avoided --- again, a naming convention can
2381save a lot of headaches here.
2382
2383
2384There is no shorthand for referencing data attributes (or other
2385methods!) from within methods. I find that this actually increases
2386the readability of methods: there is no chance of confusing local
2387variables and instance variables when glancing through a method.
2388
2389
2390Conventionally, the first argument of methods is often called
2391\verb\self\. This is nothing more than a convention: the name
2392\verb\self\ has absolutely no special meaning to Python. (Note,
2393however, that by not following the convention your code may be less
2394readable by other Python programmers, and it is also conceivable that
2395a {\em class browser} program be written which relies upon such a
2396convention.)
2397
2398
2399Any function object that is a class attribute defines a method for
2400instances of that class. It is not necessary that the function
2401definition is textually enclosed in the class definition: assigning a
2402function object to a local variable in the class is also ok. For
2403example:
2404
2405\begin{verbatim}
2406 # Function defined outside the class
2407 def f1(self, x, y):
2408 return min(x, x+y)
2409
2410 class C:
2411 f = f1
2412 def g(self):
2413 return 'hello world'
2414 h = g
2415\end{verbatim}
2416
2417Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
2418\verb\C\ that refer to function objects, and consequently they are all
2419methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
2420to \verb\g\. Note that this practice usually only serves to confuse
2421the reader of a program.
2422
2423
2424Methods may call other methods by using method attributes of the
2425\verb\self\ argument, e.g.:
2426
2427\begin{verbatim}
2428 class Bag:
2429 def empty(self):
2430 self.data = []
2431 def add(self, x):
2432 self.data.append(x)
2433 def addtwice(self, x):
Guido van Rossum084b0b21992-08-14 09:19:56 +00002434 self.add(x)
2435 self.add(x)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002436\end{verbatim}
2437
2438
2439The instantiation operation (``calling'' a class object) creates an
2440empty object. Many classes like to create objects in a known initial
Guido van Rossumca3f6c81994-10-06 14:08:53 +00002441state. Therefore a class may define a special method named
2442\verb\__init__\, like this:
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002443
Guido van Rossum6938f061994-08-01 12:22:53 +00002444\begin{verbatim}
2445 def __init__(self):
2446 self.empty()
2447\end{verbatim}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002448
Guido van Rossum6938f061994-08-01 12:22:53 +00002449When a class defines an \verb\__init__\ method, class instantiation
2450automatically invokes \verb\__init__\ for the newly-created class
2451instance. So in the \verb\Bag\ example, a new and initialized instance
2452can be obtained by:
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002453
Guido van Rossum6938f061994-08-01 12:22:53 +00002454\begin{verbatim}
2455 x = Bag()
2456\end{verbatim}
2457
2458Of course, the \verb\__init__\ method may have arguments for greater
2459flexibility. In that case, arguments given to the class instantiation
2460operator are passed on to \verb\__init__\. For example,
2461
2462\bcode\begin{verbatim}
2463>>> class Complex:
2464... def __init__(self, realpart, imagpart):
2465... self.r = realpart
2466... self.i = imagpart
2467...
2468>>> x = Complex(3.0,-4.5)
2469>>> x.r, x.i
2470(3.0, -4.5)
2471>>>
2472\end{verbatim}\ecode
2473%
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002474Methods may reference global names in the same way as ordinary
2475functions. The global scope associated with a method is the module
2476containing the class definition. (The class itself is never used as a
2477global scope!) While one rarely encounters a good reason for using
2478global data in a method, there are many legitimate uses of the global
2479scope: for one thing, functions and modules imported into the global
2480scope can be used by methods, as well as functions and classes defined
2481in it. Usually, the class containing the method is itself defined in
2482this global scope, and in the next section we'll find some good
2483reasons why a method would want to reference its own class!
2484
2485
2486\section{Inheritance}
2487
2488Of course, a language feature would not be worthy of the name ``class''
2489without supporting inheritance. The syntax for a derived class
2490definition looks as follows:
2491
2492\begin{verbatim}
2493 class DerivedClassName(BaseClassName):
2494 <statement-1>
2495 .
2496 .
2497 .
2498 <statement-N>
2499\end{verbatim}
2500
2501The name \verb\BaseClassName\ must be defined in a scope containing
2502the derived class definition. Instead of a base class name, an
2503expression is also allowed. This is useful when the base class is
2504defined in another module, e.g.,
2505
2506\begin{verbatim}
2507 class DerivedClassName(modname.BaseClassName):
2508\end{verbatim}
2509
2510Execution of a derived class definition proceeds the same as for a
2511base class. When the class object is constructed, the base class is
2512remembered. This is used for resolving attribute references: if a
2513requested attribute is not found in the class, it is searched in the
2514base class. This rule is applied recursively if the base class itself
2515is derived from some other class.
2516
2517There's nothing special about instantiation of derived classes:
2518\verb\DerivedClassName()\ creates a new instance of the class. Method
2519references are resolved as follows: the corresponding class attribute
2520is searched, descending down the chain of base classes if necessary,
2521and the method reference is valid if this yields a function object.
2522
2523Derived classes may override methods of their base classes. Because
2524methods have no special privileges when calling other methods of the
2525same object, a method of a base class that calls another method
2526defined in the same base class, may in fact end up calling a method of
Guido van Rossum16d6e711994-08-08 12:30:22 +00002527a derived class that overrides it. (For \Cpp{} programmers: all methods
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002528in Python are ``virtual functions''.)
2529
2530An overriding method in a derived class may in fact want to extend
2531rather than simply replace the base class method of the same name.
2532There is a simple way to call the base class method directly: just
2533call \verb\BaseClassName.methodname(self, arguments)\. This is
2534occasionally useful to clients as well. (Note that this only works if
2535the base class is defined or imported directly in the global scope.)
2536
2537
2538\subsection{Multiple inheritance}
2539
Guido van Rossum6938f061994-08-01 12:22:53 +00002540Python supports a limited form of multiple inheritance as well. A
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002541class definition with multiple base classes looks as follows:
2542
2543\begin{verbatim}
2544 class DerivedClassName(Base1, Base2, Base3):
2545 <statement-1>
2546 .
2547 .
2548 .
2549 <statement-N>
2550\end{verbatim}
2551
2552The only rule necessary to explain the semantics is the resolution
2553rule used for class attribute references. This is depth-first,
2554left-to-right. Thus, if an attribute is not found in
2555\verb\DerivedClassName\, it is searched in \verb\Base1\, then
2556(recursively) in the base classes of \verb\Base1\, and only if it is
2557not found there, it is searched in \verb\Base2\, and so on.
2558
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002559(To some people breadth first---searching \verb\Base2\ and
2560\verb\Base3\ before the base classes of \verb\Base1\---looks more
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002561natural. However, this would require you to know whether a particular
2562attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
2563one of its base classes before you can figure out the consequences of
2564a name conflict with an attribute of \verb\Base2\. The depth-first
2565rule makes no differences between direct and inherited attributes of
2566\verb\Base1\.)
2567
2568It is clear that indiscriminate use of multiple inheritance is a
2569maintenance nightmare, given the reliance in Python on conventions to
2570avoid accidental name conflicts. A well-known problem with multiple
2571inheritance is a class derived from two classes that happen to have a
2572common base class. While it is easy enough to figure out what happens
2573in this case (the instance will have a single copy of ``instance
2574variables'' or data attributes used by the common base class), it is
2575not clear that these semantics are in any way useful.
2576
2577
2578\section{Odds and ends}
2579
2580Sometimes it is useful to have a data type similar to the Pascal
2581``record'' or C ``struct'', bundling together a couple of named data
2582items. An empty class definition will do nicely, e.g.:
2583
2584\begin{verbatim}
2585 class Employee:
2586 pass
2587
2588 john = Employee() # Create an empty employee record
2589
2590 # Fill the fields of the record
2591 john.name = 'John Doe'
2592 john.dept = 'computer lab'
2593 john.salary = 1000
2594\end{verbatim}
2595
2596
2597A piece of Python code that expects a particular abstract data type
2598can often be passed a class that emulates the methods of that data
2599type instead. For instance, if you have a function that formats some
2600data from a file object, you can define a class with methods
2601\verb\read()\ and \verb\readline()\ that gets the data from a string
2602buffer instead, and pass it as an argument. (Unfortunately, this
2603technique has its limitations: a class can't define operations that
2604are accessed by special syntax such as sequence subscripting or
2605arithmetic operators, and assigning such a ``pseudo-file'' to
2606\verb\sys.stdin\ will not cause the interpreter to read further input
2607from it.)
2608
2609
2610Instance method objects have attributes, too: \verb\m.im_self\ is the
2611object of which the method is an instance, and \verb\m.im_func\ is the
2612function object corresponding to the method.
2613
2614
Guido van Rossum6938f061994-08-01 12:22:53 +00002615\chapter{Recent Additions}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002616
Guido van Rossum6938f061994-08-01 12:22:53 +00002617Python is an evolving language. Since this tutorial was last
2618thoroughly revised, several new features have been added to the
2619language. While ideally I should revise the tutorial to incorporate
2620them in the mainline of the text, lack of time currently requires me
Guido van Rossum16cd7f91994-10-06 10:29:26 +00002621to take a more modest approach. In this chapter I will briefly list the
Guido van Rossum6938f061994-08-01 12:22:53 +00002622most important improvements to the language and how you can use them
2623to your benefit.
2624
2625\section{The Last Printed Expression}
2626
2627In interactive mode, the last printed expression is assigned to the
Guido van Rossum194e57c1995-02-15 15:51:38 +00002628variable \code{_}. This means that when you are using Python as a
Guido van Rossum6938f061994-08-01 12:22:53 +00002629desk calculator, it is somewhat easier to continue calculations, for
2630example:
2631
2632\begin{verbatim}
2633 >>> tax = 17.5 / 100
2634 >>> price = 3.50
2635 >>> price * tax
2636 0.6125
2637 >>> price + _
2638 4.1125
2639 >>> round(_, 2)
2640 4.11
2641 >>>
2642\end{verbatim}
2643
Guido van Rossum194e57c1995-02-15 15:51:38 +00002644For reasons too embarrassing to explain, this variable is implemented
2645as a built-in (living in the module \code{__builtin__}), so it should
2646be treated as read-only by the user. I.e. don't explicitly assign a
2647value to it --- you would create an independent local variable with
2648the same name masking the built-in variable with its magic behavior.
2649
Guido van Rossum6938f061994-08-01 12:22:53 +00002650\section{String Literals}
2651
2652\subsection{Double Quotes}
2653
2654Python can now also use double quotes to surround string literals,
Guido van Rossum194e57c1995-02-15 15:51:38 +00002655e.g. \verb\"this doesn't hurt a bit"\. There is no semantic
2656difference between strings surrounded by single or double quotes.
Guido van Rossum6938f061994-08-01 12:22:53 +00002657
2658\subsection{Continuation Of String Literals}
2659
2660String literals can span multiple lines by escaping newlines with
2661backslashes, e.g.
2662
2663\begin{verbatim}
2664 hello = "This is a rather long string containing\n\
2665 several lines of text just as you would do in C.\n\
2666 Note that whitespace at the beginning of the line is\
2667 significant.\n"
2668 print hello
2669\end{verbatim}
2670
2671which would print the following:
2672\begin{verbatim}
2673 This is a rather long string containing
2674 several lines of text just as you would do in C.
2675 Note that whitespace at the beginning of the line is significant.
2676\end{verbatim}
2677
2678\subsection{Triple-quoted strings}
2679
2680In some cases, when you need to include really long strings (e.g.
2681containing several paragraphs of informational text), it is annoying
2682that you have to terminate each line with \verb@\n\@, especially if
2683you would like to reformat the text occasionally with a powerful text
2684editor like Emacs. For such situations, ``triple-quoted'' strings can
2685be used, e.g.
2686
2687\begin{verbatim}
2688 hello = """
2689
2690 This string is bounded by triple double quotes (3 times ").
Guido van Rossum194e57c1995-02-15 15:51:38 +00002691 Unescaped newlines in the string are retained, though \
Guido van Rossum6938f061994-08-01 12:22:53 +00002692 it is still possible\nto use all normal escape sequences.
2693
2694 Whitespace at the beginning of a line is
2695 significant. If you need to include three opening quotes
2696 you have to escape at least one of them, e.g. \""".
2697
2698 This string ends in a newline.
2699 """
2700\end{verbatim}
2701
Guido van Rossum194e57c1995-02-15 15:51:38 +00002702Triple-quoted strings can be surrounded by three single quotes as
2703well, again without semantic difference.
Guido van Rossum6938f061994-08-01 12:22:53 +00002704
2705\subsection{String Literal Juxtaposition}
2706
2707One final twist: you can juxtapose multiple string literals. Two or
2708more adjacent string literals (but not arbitrary expressions!)
2709separated only by whitespace will be concatenated (without intervening
2710whitespace) into a single string object at compile time. This makes
2711it possible to continue a long string on the next line without
2712sacrificing indentation or performance, unlike the use of the string
2713concatenation operator \verb\+\ or the continuation of the literal
2714itself on the next line (since leading whitespace is significant
2715inside all types of string literals). Note that this feature, like
2716all string features except triple-quoted strings, is borrowed from
2717Standard C.
2718
2719\section{The Formatting Operator}
2720
2721\subsection{Basic Usage}
2722
2723The chapter on output formatting is really out of date: there is now
2724an almost complete interface to C-style printf formats. This is done
2725by overloading the modulo operator (\verb\%\) for a left operand
2726which is a string, e.g.
2727
2728\begin{verbatim}
2729 >>> import math
2730 >>> print 'The value of PI is approximately %5.3f.' % math.pi
2731 The value of PI is approximately 3.142.
2732 >>>
2733\end{verbatim}
2734
2735If there is more than one format in the string you pass a tuple as
2736right operand, e.g.
2737
2738\begin{verbatim}
2739 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
2740 >>> for name, phone in table.items():
2741 ... print '%-10s ==> %10d' % (name, phone)
2742 ...
2743 Jack ==> 4098
2744 Dcab ==> 8637678
2745 Sjoerd ==> 4127
2746 >>>
2747\end{verbatim}
2748
2749Most formats work exactly as in C and require that you pass the proper
2750type (however, if you don't you get an exception, not a core dump).
2751The \verb\%s\ format is more relaxed: if the corresponding argument is
2752not a string object, it is converted to string using the \verb\str()\
2753built-in function. Using \verb\*\ to pass the width or precision in
2754as a separate (integer) argument is supported. The C formats
2755\verb\%n\ and \verb\%p\ are not supported.
2756
2757\subsection{Referencing Variables By Name}
2758
2759If you have a really long format string that you don't want to split
2760up, it would be nice if you could reference the variables to be
2761formatted by name instead of by position. This can be done by using
2762an extension of C formats using the form \verb\%(name)format\, e.g.
2763
2764\begin{verbatim}
2765 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
2766 >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table
2767 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
2768 >>>
2769\end{verbatim}
2770
2771This is particularly useful in combination with the new built-in
2772\verb\vars()\ function, which returns a dictionary containing all
2773local variables.
2774
2775\section{Optional Function Arguments}
2776
2777It is now possible to define functions with a variable number of
2778arguments. There are two forms, which can be combined.
2779
2780\subsection{Default Argument Values}
2781
2782The most useful form is to specify a default value for one or more
2783arguments. This creates a function that can be called with fewer
2784arguments than it is defined, e.g.
2785
2786\begin{verbatim}
2787 def ask_ok(prompt, retries = 4, complaint = 'Yes or no, please!'):
2788 while 1:
2789 ok = raw_input(prompt)
2790 if ok in ('y', 'ye', 'yes'): return 1
2791 if ok in ('n', 'no', 'nop', 'nope'): return 0
2792 retries = retries - 1
2793 if retries < 0: raise IOError, 'refusenik user'
2794 print complaint
2795\end{verbatim}
2796
2797This function can be called either like this:
2798\verb\ask_ok('Do you really want to quit?')\ or like this:
2799\verb\ask_ok('OK to overwrite the file?', 2)\.
2800
2801The default values are evaluated at the point of function definition
2802in the {\em defining} scope, so that e.g.
2803
2804\begin{verbatim}
2805 i = 5
2806 def f(arg = i): print arg
2807 i = 6
2808 f()
2809\end{verbatim}
2810
2811will print \verb\5\.
2812
2813\subsection{Arbitrary Argument Lists}
2814
2815It is also possible to specify that a function can be called with an
2816arbitrary number of arguments. These arguments will be wrapped up in
2817a tuple. Before the variable number of arguments, zero or more normal
2818arguments may occur, e.g.
2819
2820\begin{verbatim}
2821 def fprintf(file, format, *args):
2822 file.write(format % args)
2823\end{verbatim}
2824
2825This feature may be combined with the previous, e.g.
2826
2827\begin{verbatim}
2828 def but_is_it_useful(required, optional = None, *remains):
2829 print "I don't know"
2830\end{verbatim}
2831
2832\section{Lambda And Functional Programming Tools}
2833
2834\subsection{Lambda Forms}
2835
Guido van Rossum16d6e711994-08-08 12:30:22 +00002836By popular demand, a few features commonly found in functional
Guido van Rossum6938f061994-08-01 12:22:53 +00002837programming languages and Lisp have been added to Python. With the
2838\verb\lambda\ keyword, small anonymous functions can be created.
2839Here's a function that returns the sum of its two arguments:
2840\verb\lambda a, b: a+b\. Lambda forms can be used wherever function
2841objects are required. They are syntactically restricted to a single
2842expression. Semantically, they are just syntactic sugar for a normal
2843function definition. Like nested function definitions, lambda forms
2844cannot reference variables from the containing scope, but this can be
2845overcome through the judicious use of default argument values, e.g.
2846
2847\begin{verbatim}
2848 def make_incrementor(n):
Guido van Rossumc1be9d51994-08-30 12:08:58 +00002849 return lambda x, incr=n: x+incr
Guido van Rossum6938f061994-08-01 12:22:53 +00002850\end{verbatim}
2851
2852\subsection{Map, Reduce and Filter}
2853
2854Three new built-in functions on sequences are good candidate to pass
2855lambda forms.
2856
2857\subsubsection{Map.}
2858
2859\verb\map(function, sequence)\ calls \verb\function(item)\ for each of
2860the sequence's items and returns a list of the return values. For
2861example, to compute some cubes:
2862
2863\begin{verbatim}
2864 >>> map(lambda x: x*x*x, range(1, 11))
2865 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
2866 >>>
2867\end{verbatim}
2868
2869More than one sequence may be passed; the function must then have as
2870many arguments as there are sequences and is called with the
2871corresponding item from each sequence (or \verb\None\ if some sequence
2872is shorter than another). If \verb\None\ is passed for the function,
2873a function returning its argument(s) is substituted.
2874
2875Combining these two special cases, we see that
2876\verb\map(None, list1, list2)\ is a convenient way of turning a pair
2877of lists into a list of pairs. For example:
2878
2879\begin{verbatim}
2880 >>> seq = range(8)
2881 >>> map(None, seq, map(lambda x: x*x, seq))
2882 [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
2883 >>>
2884\end{verbatim}
2885
2886\subsubsection{Filter.}
2887
2888\verb\filter(function, sequence)\ returns a sequence (of the same
2889type, if possible) consisting of those items from the sequence for
2890which \verb\function(item)\ is true. For example, to compute some
2891primes:
2892
2893\begin{verbatim}
2894 >>> filter(lambda x: x%2 != 0 and x%3 != 0, range(2, 25))
2895 [5, 7, 11, 13, 17, 19, 23]
2896 >>>
2897\end{verbatim}
2898
2899\subsubsection{Reduce.}
2900
2901\verb\reduce(function, sequence)\ returns a single value constructed
2902by calling the (binary) function on the first two items of the
2903sequence, then on the result and the next item, and so on. For
2904example, to compute the sum of the numbers 1 through 10:
2905
2906\begin{verbatim}
2907 >>> reduce(lambda x, y: x+y, range(1, 11))
2908 55
2909 >>>
2910\end{verbatim}
2911
2912If there's only one item in the sequence, its value is returned; if
2913the sequence is empty, an exception is raised.
2914
2915A third argument can be passed to indicate the starting value. In this
2916case the starting value is returned for an empty sequence, and the
2917function is first applied to the starting value and the first sequence
2918item, then to the result and the next item, and so on. For example,
2919
2920\begin{verbatim}
2921 >>> def sum(seq):
2922 ... return reduce(lambda x, y: x+y, seq, 0)
2923 ...
2924 >>> sum(range(1, 11))
2925 55
2926 >>> sum([])
2927 0
2928 >>>
2929\end{verbatim}
2930
2931\section{Continuation Lines Without Backslashes}
2932
2933While the general mechanism for continuation of a source line on the
2934next physical line remains to place a backslash on the end of the
2935line, expressions inside matched parentheses (or square brackets, or
2936curly braces) can now also be continued without using a backslash.
2937This is particularly useful for calls to functions with many
2938arguments, and for initializations of large tables.
2939
2940For example:
2941
2942\begin{verbatim}
2943 month_names = ['Januari', 'Februari', 'Maart',
2944 'April', 'Mei', 'Juni',
2945 'Juli', 'Augustus', 'September',
2946 'Oktober', 'November', 'December']
2947\end{verbatim}
2948
2949and
2950
2951\begin{verbatim}
2952 CopyInternalHyperLinks(self.context.hyperlinks,
2953 copy.context.hyperlinks,
2954 uidremap)
2955\end{verbatim}
2956
2957\section{Regular Expressions}
2958
2959While C's printf-style output formats, transformed into Python, are
2960adequate for most output formatting jobs, C's scanf-style input
2961formats are not very powerful. Instead of scanf-style input, Python
2962offers Emacs-style regular expressions as a powerful input and
2963scanning mechanism. Read the corresponding section in the Library
2964Reference for a full description.
2965
2966\section{Generalized Dictionaries}
2967
2968The keys of dictionaries are no longer restricted to strings -- they
Guido van Rossum194e57c1995-02-15 15:51:38 +00002969can be any immutable basic type including strings, numbers, tuples, or
2970(certain) class instances. (Lists and dictionaries are not acceptable
2971as dictionary keys, in order to avoid problems when the object used as
2972a key is modified.)
Guido van Rossum6938f061994-08-01 12:22:53 +00002973
2974Dictionaries have two new methods: \verb\d.values()\ returns a list of
2975the dictionary's values, and \verb\d.items()\ returns a list of the
2976dictionary's (key, value) pairs. Like \verb\d.keys()\, these
2977operations are slow for large dictionaries. Examples:
2978
2979\begin{verbatim}
2980 >>> d = {100: 'honderd', 1000: 'duizend', 10: 'tien'}
2981 >>> d.keys()
2982 [100, 10, 1000]
2983 >>> d.values()
2984 ['honderd', 'tien', 'duizend']
2985 >>> d.items()
2986 [(100, 'honderd'), (10, 'tien'), (1000, 'duizend')]
2987 >>>
2988\end{verbatim}
2989
2990\section{Miscellaneous New Built-in Functions}
2991
2992The function \verb\vars()\ returns a dictionary containing the current
Guido van Rossum16cd7f91994-10-06 10:29:26 +00002993local variables. With a module argument, it returns that module's
Guido van Rossum6938f061994-08-01 12:22:53 +00002994global variables. The old function \verb\dir(x)\ returns
2995\verb\vars(x).keys()\.
2996
2997The function \verb\round(x)\ returns a floating point number rounded
2998to the nearest integer (but still expressed as a floating point
2999number). E.g. \verb\round(3.4) == 3.0\ and \verb\round(3.5) == 4.0\.
3000With a second argument it rounds to the specified number of digits,
3001e.g. \verb\round(math.pi, 4) == 3.1416\ or even
3002\verb\round(123.4, -2) == 100.0\.
3003
3004The function \verb\hash(x)\ returns a hash value for an object.
3005All object types acceptable as dictionary keys have a hash value (and
3006it is this hash value that the dictionary implementation uses).
3007
3008The function \verb\id(x)\ return a unique identifier for an object.
3009For two objects x and y, \verb\id(x) == id(y)\ if and only if
3010\verb\x is y\. (In fact the object's address is used.)
3011
3012The function \verb\hasattr(x, name)\ returns whether an object has an
3013attribute with the given name (a string value). The function
3014\verb\getattr(x, name)\ returns the object's attribute with the given
3015name. The function \verb\setattr(x, name, value)\ assigns a value to
3016an object's attribute with the given name. These three functions are
3017useful if the attribute names are not known beforehand. Note that
Guido van Rossume5f8b601995-01-04 19:12:49 +00003018\verb\getattr(x, 'spam')\ is equivalent to \verb\x.spam\, and
3019\verb\setattr(x, 'spam', y)\ is equivalent to \verb\x.spam = y\. By
Guido van Rossum6938f061994-08-01 12:22:53 +00003020definition, \verb\hasattr(x, name)\ returns true if and only if
3021\verb\getattr(x, name)\ returns without raising an exception.
3022
3023\section{Else Clause For Try Statement}
3024
3025The \verb\try...except\ statement now has an optional \verb\else\
3026clause, which must follow all \verb\except\ clauses. It is useful to
3027place code that must be executed if the \verb\try\ clause does not
3028raise an exception. For example:
3029
3030\begin{verbatim}
3031 for arg in sys.argv:
3032 try:
3033 f = open(arg, 'r')
3034 except IOError:
3035 print 'cannot open', arg
3036 else:
3037 print arg, 'has', len(f.readlines()), 'lines'
3038 f.close()
3039\end{verbatim}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003040
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003041
3042\section{New Class Features in Release 1.1}
3043
Guido van Rossumcfb45e41994-11-10 23:04:43 +00003044Some changes have been made to classes: the operator overloading
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003045mechanism is more flexible, providing more support for non-numeric use
Guido van Rossum29766b21994-10-06 15:33:25 +00003046of operators (including calling an object as if it were a function),
3047and it is possible to trap attribute accesses.
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003048
3049\subsection{New Operator Overloading}
3050
3051It is no longer necessary to coerce both sides of an operator to the
3052same class or type. A class may still provide a \code{__coerce__}
3053method, but this method may return objects of different types or
3054classes if it feels like it. If no \code{__coerce__} is defined, any
3055argument type or class is acceptable.
3056
3057In order to make it possible to implement binary operators where the
3058right-hand side is a class instance but the left-hand side is not,
3059without using coercions, right-hand versions of all binary operators
3060may be defined. These have an `r' prepended to their name,
3061e.g. \code{__radd__}.
3062
3063For example, here's a very simple class for representing times. Times
3064are initialized from a number of seconds (like time.time()). Times
3065are printed like this: \code{Thu Oct 6 14:20:06 1994}. Subtracting
3066two Times gives their difference in seconds. Adding or subtracting a
3067Time and a number gives a new Time. You can't add two times, nor can
3068you subtract a Time from a number.
3069
3070\begin{verbatim}
3071import time
3072
3073class Time:
3074 def __init__(self, seconds):
3075 self.seconds = seconds
3076 def __repr__(self):
3077 return time.ctime(self.seconds)
3078 def __add__(self, x):
3079 return Time(self.seconds + x)
3080 __radd__ = __add__ # support for x+t
3081 def __sub__(self, x):
3082 if hasattr(x, 'seconds'): # test if x could be a Time
3083 return self.seconds - x.seconds
3084 else:
3085 return self.seconds - x
3086
3087now = Time(time.time())
3088tomorrow = 24*3600 + now
3089yesterday = now - today
3090print tomorrow - yesterday # prints 172800
3091\end{verbatim}
3092
3093\subsection{Trapping Attribute Access}
3094
3095You can define three new ``magic'' methods in a class now:
3096\code{__getattr__(self, name)}, \code{__setattr__(self, name, value)}
3097and \code{__delattr__(self, name)}.
3098
3099The \code{__getattr__} method is called when an attribute access fails,
3100i.e. when an attribute access would otherwise raise AttributeError --
3101this is {\em after} the instance's dictionary and its class hierarchy
3102have been searched for the named attribute. Note that if this method
3103attempts to access any undefined instance attribute it will be called
3104recursively!
3105
3106The \code{__setattr__} and \code{__delattr__} methods are called when
3107assignment to, respectively deletion of an attribute are attempted.
3108They are called {\em instead} of the normal action (which is to insert
3109or delete the attribute in the instance dictionary). If either of
3110these methods most set or delete any attribute, they can only do so by
3111using the instance dictionary directly -- \code{self.__dict__} -- else
3112they would be called recursively.
3113
3114For example, here's a near-universal ``Wrapper'' class that passes all
3115its attribute accesses to another object. Note how the
3116\code{__init__} method inserts the wrapped object in
3117\code{self.__dict__} in order to avoid endless recursion
3118(\code{__setattr__} would call \code{__getattr__} which would call
3119itself recursively).
3120
3121\begin{verbatim}
3122class Wrapper:
3123 def __init__(self, wrapped):
3124 self.__dict__['wrapped'] = wrapped
3125 def __getattr__(self, name):
3126 return getattr(self.wrapped, name)
3127 def __setattr__(self, name, value):
Guido van Rossumcfb45e41994-11-10 23:04:43 +00003128 setattr(self.wrapped, name, value)
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003129 def __delattr__(self, name):
3130 delattr(self.wrapped, name)
3131
3132import sys
3133f = Wrapper(sys.stdout)
3134f.write('hello world\n') # prints 'hello world'
3135\end{verbatim}
3136
Guido van Rossum29766b21994-10-06 15:33:25 +00003137A simpler example of \code{__getattr__} is an attribute that is
3138computed each time (or the first time) it it accessed. For instance:
3139
3140\begin{verbatim}
3141from math import pi
3142
3143class Circle:
3144 def __init__(self, radius):
3145 self.radius = radius
3146 def __getattr__(self, name):
3147 if name == 'circumference':
3148 return 2 * pi * self.radius
3149 if name == 'diameter':
3150 return 2 * self.radius
3151 if name == 'area':
3152 return pi * pow(self.radius, 2)
3153 raise AttributeError, name
3154\end{verbatim}
3155
3156\subsection{Calling a Class Instance}
3157
3158If a class defines a method \code{__call__} it is possible to call its
3159instances as if they were functions. For example:
3160
3161\begin{verbatim}
3162class PresetSomeArguments:
3163 def __init__(self, func, *args):
3164 self.func, self.args = func, args
3165 def __call__(self, *args):
3166 return apply(self.func, self.args + args)
3167
3168f = PresetSomeArguments(pow, 2) # f(i) computes powers of 2
3169for i in range(10): print f(i), # prints 1 2 4 8 16 32 64 128 256 512
3170print # append newline
3171\end{verbatim}
3172
Guido van Rossum194e57c1995-02-15 15:51:38 +00003173
3174\chapter{New in Release 1.2}
3175
3176
3177This chapter describes even more recent additions to the Python
3178language and library.
3179
3180
3181\section{New Class Features}
3182
3183The semantics of \code{__coerce__} have been changed to be more
3184reasonable. As an example, the new standard module \code{Complex}
3185implements fairly complete complex numbers using this. Additional
3186examples of classes with and without \code{__coerce__} methods can be
3187found in the \code{Demo/classes} subdirectory, modules \code{Rat} and
3188\code{Dates}.
3189
3190If a class defines no \code{__coerce__} method, this is equivalent to
3191the following definition:
3192
3193\begin{verbatim}
3194def __coerce__(self, other): return self, other
3195\end{verbatim}
3196
3197If \code{__coerce__} coerces itself to an object of a different type,
3198the operation is carried out using that type --- in release 1.1, this
3199would cause an error.
3200
3201Comparisons involving class instances now invoke \code{__coerce__}
3202exactly as if \code{cmp(x, y)} were a binary operator like \code{+}
3203(except if \code{x} and \code{y} are the same object).
3204
3205\section{Unix Signal Handling}
3206
3207On Unix, Python now supports signal handling. The module
3208\code{signal} exports functions \code{signal}, \code{pause} and
3209\code{alarm}, which act similar to their Unix counterparts. The
3210module also exports the conventional names for the various signal
3211classes (also usable with \code{os.kill()}) and \code{SIG_IGN} and
3212\code{SIG_DFL}. See the section on \code{signal} in the Library
3213Reference Manual for more information.
3214
3215\section{Exceptions Can Be Classes}
3216
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
3273
3274\section{Object Persistency and Object Copying}
3275
3276Two new modules, \code{pickle} and \code{shelve}, support storage and
3277retrieval of (almost) arbitrary Python objects on disk, using the
3278\code{dbm} package. A third module, \code{copy}, provides flexible
3279object copying operations.
3280
3281\subsection{Persistent Objects}
3282
3283The module \code{pickle} provides a general framework for objects to
3284disassemble themselves into a stream of bytes and to reassemble such a
3285stream back into an object. It copes with reference sharing,
3286recursive objects and instances of user-defined classes, but not
3287(directly) with objects that have ``magical'' links into the operating
3288system such as open files, sockets or windows.
3289
3290The \code{pickle} module defines a simple protocol whereby
3291user-defined classes can control how they are disassembled and
3292assembled. The method \code{__getinitargs__()}, if defined, returns
3293the argument list for the constructor to be used at assembly time (by
3294default the constructor is called without arguments). The methods
3295\code{__getstate__()} and \code{__setstate__()} are used to pass
3296additional state from disassembly to assembly; by default the
3297instance's \code{__dict__} is passed and restored.
3298
3299Note that \code{pickle} does not open or close any files --- it can be
3300used equally well for moving objects around on a network or store them
3301in a database. For ease of debugging, and the inevitable occasional
3302manual patch-up, the constructed byte streams consist of printable
3303ASCII characters only (though it's not designed to be pretty).
3304
3305The module \code{shelve} provides a simple model for storing objects
3306on files. The operation \code{shelve.open(filename)} returns a
3307``shelf'', which is a simple persistent database with a
3308dictionary-like interface. Database keys are strings, objects stored
3309in the database can be anything that \code{pickle} will handle.
3310
3311More information on these modules can be glanced from their
3312documentation strings (see below).
3313
3314\subsection{Copying Objects}
3315
3316The module \code{copy} exports two functions: \code{copy()} and
3317\code{deepcopy()}. The \code{copy()} function returns a ``shallow''
3318copy of an object; \code{deepcopy()} returns a ``deep'' copy. The
3319difference between shallow and deep copying is only relevant for
3320compound objects (objects that contain other objects, like lists or
3321class instances):
3322
3323\begin{itemize}
3324
3325\item
3326A shallow copy constructs a new compound object and then (to the
3327extent possible) inserts {\em the same objects} into in that the
3328original contains.
3329
3330\item
3331A deep copy constructs a new compound object and then, recursively,
3332inserts {\em copies} into it of the objects found in the original.
3333
3334\end{itemize}
3335
3336Both functions have the same restrictions and use the same protocols
3337as \code{pickle} --- user-defined classes can control how they are
3338copied by providing methods named \code{__getinitargs__()},
3339\code{__getstate__()} and \code{__setstate__()}.
3340
3341More info in the module's documentation string.
3342
3343
3344\section{Documentation Strings}
3345
3346A variety of objects now have a new attribute, \code{__doc__}, which
3347is supposed to contain a documentation string (if no documentation is
3348present, the attribute is \code{None}). New syntax, compatible with
3349the old interpreter, allows for convenient initialization of the
3350\code{__doc__} attribute of modules, classes and functions by placing
3351a string literal by itself as the first statement in the suite. It
3352must be a literal --- an expression yielding a string object is not
3353accepted as a documentation string, since future tools may need to
3354derive documentation from source by parsing.
3355
3356Here is a hypothetical, amply documented module called \code{Spam}:
3357
3358\begin{verbatim}
3359"""Spam operations.
3360
3361This module exports two classes, a function and an exception:
3362
3363class Spam: full Spam functionality --- three can sizes
3364class SpamLight: limited Spam functionality --- only one can size
3365
3366def open(filename): open a file and return a corresponding Spam or
3367SpamLight object
3368
3369GoneOff: exception raised for errors; should never happen
3370
3371Note that it is always possible to convert a SpamLight object to a
3372Spam object by a simple method call, but that the reverse operation is
3373generally costly and may fail for a number of reasons.
3374"""
3375
3376class SpamLight:
3377 """Limited spam functionality.
3378
3379 Supports a single can size, no flavor, and only hard disks.
3380 """
3381
3382 def __init__(self, size=12):
3383 """Construct a new SpamLight instance.
3384
3385 Argument is the can size.
3386 """
3387 # etc.
3388
3389 # etc.
3390
3391class Spam(SpamLight):
3392 """Full spam functionality.
3393
3394 Supports three can sizes, two flavor varieties, and all floppy
3395 disk formats still supported by current hardware.
3396 """
3397
3398 def __init__(self, size1=8, size2=12, size3=20):
3399 """Construct a new Spam instance.
3400
3401 Arguments are up to three can sizes.
3402 """
3403 # etc.
3404
3405 # etc.
3406
3407def open(filename = "/dev/null"):
3408 """Open a can of Spam.
3409
3410 Argument must be an existing file.
3411 """
3412 # etc.
3413
3414class GoneOff:
3415 """Class used for Spam exceptions.
3416
3417 There shouldn't be any.
3418 """
3419 pass
3420\end{verbatim}
3421
3422After executing ``\code{import Spam}'', the following expressions
3423return the various documentation strings from the module:
3424
3425\begin{verbatim}
3426Spam.__doc__
3427Spam.SpamLight.__doc__
3428Spam.SpamLight.__init__.__doc__
3429Spam.Spam.__doc__
3430Spam.Spam.__init__.__doc__
3431Spam.open.__doc__
3432Spam.GoneOff.__doc__
3433\end{verbatim}
3434
3435There are emerging conventions about the content and formatting of
3436documentation strings.
3437
3438The first line should always be a short, concise summary of the
3439object's purpose. For brevity, it should not explicitly state the
3440object's name or type, since these are available by other means
3441(except if the name happens to be a verb describing a function's
3442operation). This line should begin with a capital letter and end with
3443a period.
3444
3445If there are more lines in the documentation string, the second line
3446should be blank, visually separating the summary from the rest of the
3447description. The following lines should be one of more of paragraphs
3448describing the objects calling conventions, its side effects, etc.
3449
3450Some people like to copy the Emacs convention of using UPPER CASE for
3451function parameters --- this often saves a few words or lines.
3452
3453The Python parser does not strip indentation from multi-line string
3454literals in Python, so tools that process documentation have to strip
3455indentation. This is done using the following convention. The first
3456non-blank line {\em after} the first line of the string determines the
3457amount of indentation for the entire documentation string. (We can't
3458use the first line since it is generally adjacent to the string's
3459opening quotes so its indentation is not apparent in the string
3460literal.) Whitespace ``equivalent'' to this indentation is then
3461stripped from the start of all lines of the string. Lines that are
3462indented less should not occur, but if they occur all their leading
3463whitespace should be stripped. Equivalence of whitespace should be
3464tested after expansion of tabs (to 8 spaces, normally).
3465
3466In this release, few of the built-in or standard functions and modules
3467have documentation strings.
3468
3469
3470\section{Customizing Import and Built-Ins}
3471
3472In preparation for a ``restricted execution mode'' which will be
3473usable to run code received from an untrusted source (such as a WWW
3474server or client), the mechanism by which modules are imported has
3475been redesigned. It is now possible to provide your own function
3476\code{__import__} which is called whenever an \code{import} statement
3477is executed. There's a built-in function \code{__import__} which
3478provides the default implementation, but more interesting, the various
3479steps it takes are available separately from the new built-in module
3480\code{imp}. (See the section on \code{imp} in the Library Reference
3481Manual for more information on this module.)
3482
3483When you do \code{dir()} in a fresh interactive interpreter you will
3484see another ``secret'' object that's present in every module:
3485\code{__builtins__}. This is either a dictionary or a module
3486containing the set of built-in objects used by functions defined in
3487current module. Although normally all modules are initialized with a
3488reference to the same dictionary, it is now possible to use a
3489different set of built-ins on a per-module basis. Together with the
3490fact that the \code{import} statement uses the \code{__import__}
3491function it finds in the importing modules' dictionary of built-ins,
3492this forms the basis for a future restricted execution mode.
3493
3494
3495\section{Python and the World-Wide Web}
3496
3497There is a growing number of modules available for writing WWW tools.
3498The previous release already sported modules \code{gopherlib},
3499\code{ftplib}, \code{httplib} and \code{urllib} (unifying the previous
3500three) for accessing data through the commonest WWW protocols. This
3501release also provides \code{cgi}, to ease the writing of server-side
3502scripts that use the Common Gateway Interface protocol, supported by
3503most WWW servers. The module \code{urlparse} provides precise parsing
3504of a URL string into its components (address scheme, network location,
3505path, parameters, query, and fragment identifier).
3506
3507There is no complete parser for HTML files yet, although the
3508\code{Demo/www} directory in the distribution contains some old code
3509that should be a start if you wanted to contribute one.
3510Unfortunately Python seems to be too slow for real-time parsing and
3511formatting of HTML such as required by interactive WWW browsers --- but
3512it's ideal for writing a ``robot'' (an automated WWW browser that
3513searches the web for information).
3514
3515
3516\section{Miscellaneous}
3517
3518\begin{itemize}
3519
3520\item
3521The \code{socket} module now exports all the needed constants used for
3522socket operations, such as \code{SO_BROADCAST}.
3523
3524\item
3525The functions \code{popen()} and \code{fdopen()} in the \code{os}
3526module now follow the pattern of the built-in function \code{open()}:
3527the default mode argument is \code{'r'} and the optional third
3528argument specifies the buffer size, where \code{0} means unbuffered,
3529\code{1} means line-buffered, and any larger number means the size of
3530the buffer in bytes.
3531
3532\end{itemize}
3533
3534
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00003535\end{document}