blob: 97ebeb77da32c4725816e2f9e063df90d8fc95c4 [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
Guido van Rossuma67dee31995-09-13 17:34:25 +0000206>>>}); for continuation lines it prompts with the
207{\em secondary\ prompt},
208by default three dots ({\tt ...}). Typing an EOF (Control-D)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000209at the primary prompt causes the interpreter to exit with a zero exit
210status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000211
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000212The interpreter prints a welcome message stating its version number
213and a copyright notice before printing the first prompt, e.g.:
214
215\bcode\begin{verbatim}
216python
Guido van Rossum288527a1995-10-09 21:02:17 +0000217Python 1.3 (Oct 13 1995)
Guido van Rossum1133aec1995-03-15 11:34:18 +0000218Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000219>>>
220\end{verbatim}\ecode
221
222\section{The Interpreter and its Environment}
223
224\subsection{Error Handling}
225
226When an error occurs, the interpreter prints an error
227message and a stack trace. In interactive mode, it then returns to
228the primary prompt; when input came from a file, it exits with a
229nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000230the stack trace. (Exceptions handled by an {\tt except} clause in a
231{\tt try} statement are not errors in this context.) Some errors are
232unconditionally fatal and cause an exit with a nonzero exit; this
233applies to internal inconsistencies and some cases of running out of
234memory. All error messages are written to the standard error stream;
235normal output from the executed commands is written to standard
236output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000237
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000238Typing the interrupt character (usually Control-C or DEL) to the
239primary or secondary prompt cancels the input and returns to the
240primary prompt.%
241\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +0000242 A problem with the GNU Readline package may prevent this.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000243}
244Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000245KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000246statement.
247
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000248\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000249
Guido van Rossume5f8b601995-01-04 19:12:49 +0000250When a module named {\tt spam} is imported, the interpreter searches
251for a file named {\tt spam.py} in the list of directories specified by
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000252the environment variable {\tt PYTHONPATH}. It has the same syntax as
253the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000254directory names. When {\tt PYTHONPATH} is not set, or when the file
255is not found there, the search continues in an installation-dependent
256default path, usually {\tt .:/usr/local/lib/python}.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000257
258Actually, modules are searched in the list of directories given by the
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000259variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} and
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000260the installation-dependent default. This allows Python programs that
261know what they're doing to modify or replace the module search path.
262See the section on Standard Modules later.
263
264\subsection{``Compiled'' Python files}
265
266As an important speed-up of the start-up time for short programs that
Guido van Rossume5f8b601995-01-04 19:12:49 +0000267use a lot of standard modules, if a file called {\tt spam.pyc} exists
268in the directory where {\tt spam.py} is found, this is assumed to
269contain an already-``compiled'' version of the module {\tt spam}. The
270modification time of the version of {\tt spam.py} used to create {\tt
271spam.pyc} is recorded in {\tt spam.pyc}, and the file is ignored if
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000272these don't match.
273
Guido van Rossume5f8b601995-01-04 19:12:49 +0000274Whenever {\tt spam.py} is successfully compiled, an attempt is made to
275write the compiled version to {\tt spam.pyc}. It is not an error if
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000276this attempt fails; if for any reason the file is not written
Guido van Rossume5f8b601995-01-04 19:12:49 +0000277completely, the resulting {\tt spam.pyc} file will be recognized as
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000278invalid and thus ignored later.
279
280\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000281
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000282On BSD'ish {\UNIX} systems, Python scripts can be made directly
283executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000284
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000285\bcode\begin{verbatim}
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000286#! /usr/local/bin/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000287\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000288%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000289(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000290script and giving the file an executable mode. The {\tt \#!} must be
291the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000292
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000293\subsection{The Interactive Startup File}
294
295When you use Python interactively, it is frequently handy to have some
296standard commands executed every time the interpreter is started. You
297can do this by setting an environment variable named {\tt
298PYTHONSTARTUP} to the name of a file containing your start-up
Guido van Rossum6938f061994-08-01 12:22:53 +0000299commands. This is similar to the {\tt .profile} feature of the UNIX
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000300shells.
301
302This file is only read in interactive sessions, not when Python reads
303commands from a script, and not when {\tt /dev/tty} is given as the
304explicit source of commands (which otherwise behaves like an
305interactive session). It is executed in the same name space where
306interactive commands are executed, so that objects that it defines or
307imports can be used without qualification in the interactive session.
Guido van Rossum7b3c8a11992-09-08 09:20:13 +0000308You can also change the prompts {\tt sys.ps1} and {\tt sys.ps2} in
309this file.
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000310
311If you want to read an additional start-up file from the current
312directory, you can program this in the global start-up file, e.g.
313\verb\execfile('.pythonrc')\. If you want to use the startup file
314in a script, you must write this explicitly in the script, e.g.
315\verb\import os;\ \verb\execfile(os.environ['PYTHONSTARTUP'])\.
316
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000317\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000318
Guido van Rossum4410c751991-06-04 20:22:18 +0000319Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000320input line and history substitution, similar to facilities found in
321the Korn shell and the GNU Bash shell. This is implemented using the
322{\em GNU\ Readline} library, which supports Emacs-style and vi-style
323editing. This library has its own documentation which I won't
324duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000325
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000326Perhaps the quickest check to see whether command line editing is
327supported is typing Control-P to the first Python prompt you get. If
328it beeps, you have command line editing. If nothing appears to
329happen, or if \verb/^P/ is echoed, you can skip the rest of this
330section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000331
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000332\subsection{Line Editing}
333
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000334If supported, input line editing is active whenever the interpreter
335prints a primary or secondary prompt. The current line can be edited
336using the conventional Emacs control characters. The most important
337of these are: C-A (Control-A) moves the cursor to the beginning of the
338line, C-E to the end, C-B moves it one position to the left, C-F to
339the right. Backspace erases the character to the left of the cursor,
340C-D the character to its right. C-K kills (erases) the rest of the
341line to the right of the cursor, C-Y yanks back the last killed
342string. C-underscore undoes the last change you made; it can be
343repeated for cumulative effect.
344
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000345\subsection{History Substitution}
346
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000347History substitution works as follows. All non-empty input lines
348issued are saved in a history buffer, and when a new prompt is given
349you are positioned on a new line at the bottom of this buffer. C-P
350moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000351Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000352front of the prompt to mark a line as modified. Pressing the Return
353key passes the current line to the interpreter. C-R starts an
354incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000355
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000356\subsection{Key Bindings}
357
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000358The key bindings and some other parameters of the Readline library can
359be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000360{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000361
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000362\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000363key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000364\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000365%
366or
367
368\bcode\begin{verbatim}
369"string": function-name
370\end{verbatim}\ecode
371%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000372and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000373
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000374\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000375set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000376\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000377%
378For example:
379
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000380\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000381# I prefer vi-style editing:
382set editing-mode vi
383# Edit using a single line:
384set horizontal-scroll-mode On
385# Rebind some keys:
386Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000387"\C-u": universal-argument
388"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000389\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000390%
Guido van Rossum4410c751991-06-04 20:22:18 +0000391Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000392instead of Readline's default filename completion function. If you
393insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000394
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000395\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000396TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000397\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000398%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000399in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000400indented continuation lines...)
401
402\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000403
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000404This facility is an enormous step forward compared to previous
405versions of the interpreter; however, some wishes are left: It would
406be nice if the proper indentation were suggested on continuation lines
407(the parser knows if an indent token is required next). The
408completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000409command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000410would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000411
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000412
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000413\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000414
415In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000416presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
417the example, you must type everything after the prompt, when the
418prompt appears; lines that do not begin with a prompt are output from
419the interpreter.%
420\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +0000421 I'd prefer to use different fonts to distinguish input
422 from output, but the amount of LaTeX hacking that would require
423 is currently beyond my ability.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000424}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000425Note that a secondary prompt on a line by itself in an example means
426you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000427
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000428\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000429
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000430Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000431for the primary prompt, {\tt >>>}. (It shouldn't take long.)
432
433\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000434
435The interpreter acts as a simple calculator: you can type an
436expression at it and it will write the value. Expression syntax is
437straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000438work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000439can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000440
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000441\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000442>>> 2+2
4434
Guido van Rossum6938f061994-08-01 12:22:53 +0000444>>> # This is a comment
445... 2+2
4464
447>>> 2+2 # and a comment on the same line as code
4484
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000449>>> (50-5*6)/4
4505
Guido van Rossum6938f061994-08-01 12:22:53 +0000451>>> # Integer division returns the floor:
452... 7/3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004532
Guido van Rossum6938f061994-08-01 12:22:53 +0000454>>> 7/-3
455-3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000456>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000457\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000458%
459Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000460variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000461
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000462\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000463>>> width = 20
464>>> height = 5*9
465>>> width * height
466900
467>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000468\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000469%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000470A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000471
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000472\bcode\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000473>>> x = y = z = 0 # Zero x, y and z
474>>> x
4750
476>>> y
4770
478>>> z
4790
480>>>
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000481\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000482%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000483There is full support for floating point; operators with mixed type
484operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000485
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000486\bcode\begin{verbatim}
487>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004883.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000489>>> 7.0 / 2
4903.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000491>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000492\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000493
494\subsection{Strings}
495
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000496Besides numbers, Python can also manipulate strings, enclosed in
Guido van Rossum6938f061994-08-01 12:22:53 +0000497single quotes or double quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000498
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000499\bcode\begin{verbatim}
Guido van Rossume5f8b601995-01-04 19:12:49 +0000500>>> 'spam eggs'
501'spam eggs'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000502>>> 'doesn\'t'
Guido van Rossum6938f061994-08-01 12:22:53 +0000503"doesn't"
504>>> "doesn't"
505"doesn't"
506>>> '"Yes," he said.'
507'"Yes," he said.'
508>>> "\"Yes,\" he said."
509'"Yes," he said.'
510>>> '"Isn\'t," she said.'
511'"Isn\'t," she said.'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000512>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000513\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000514%
515Strings are written the same way as they are typed for input: inside
Guido van Rossum6938f061994-08-01 12:22:53 +0000516quotes and with quotes and other funny characters escaped by backslashes,
517to show the precise value. The string is enclosed in double quotes if
518the string contains a single quote and no double quotes, else it's
519enclosed in single quotes. (The {\tt print} statement, described later,
520can be used to write strings without quotes or escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000521
522Strings can be concatenated (glued together) with the {\tt +}
523operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000524
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000525\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000526>>> word = 'Help' + 'A'
527>>> word
528'HelpA'
529>>> '<' + word*5 + '>'
530'<HelpAHelpAHelpAHelpAHelpA>'
531>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000532\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000533%
534Strings can be subscripted (indexed); like in C, the first character of
535a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000536
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000537There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000538size one. Like in Icon, substrings can be specified with the {\em
539slice} notation: two indices separated by a colon.
540
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000541\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000542>>> word[4]
543'A'
544>>> word[0:2]
545'He'
546>>> word[2:4]
547'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000548>>>
549\end{verbatim}\ecode
550%
551Slice indices have useful defaults; an omitted first index defaults to
552zero, an omitted second index defaults to the size of the string being
553sliced.
554
555\bcode\begin{verbatim}
556>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000557'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000558>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000559'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000560>>>
561\end{verbatim}\ecode
562%
563Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
564equals \verb\s\.
565
566\bcode\begin{verbatim}
567>>> word[:2] + word[2:]
568'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000569>>> word[:3] + word[3:]
570'HelpA'
571>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000572\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000573%
574Degenerate slice indices are handled gracefully: an index that is too
575large is replaced by the string size, an upper bound smaller than the
576lower bound returns an empty string.
577
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000578\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000579>>> word[1:100]
580'elpA'
581>>> word[10:]
582''
583>>> word[2:1]
584''
585>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000586\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000587%
588Indices may be negative numbers, to start counting from the right.
589For example:
590
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000591\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000592>>> word[-1] # The last character
593'A'
594>>> word[-2] # The last-but-one character
595'p'
596>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000597'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000598>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000599'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000600>>>
601\end{verbatim}\ecode
602%
603But note that -0 is really the same as 0, so it does not count from
604the right!
605
606\bcode\begin{verbatim}
607>>> word[-0] # (since -0 equals 0)
608'H'
609>>>
610\end{verbatim}\ecode
611%
612Out-of-range negative slice indices are truncated, but don't try this
613for single-element (non-slice) indices:
614
615\bcode\begin{verbatim}
616>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000617'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000618>>> word[-10] # error
Guido van Rossum6938f061994-08-01 12:22:53 +0000619Traceback (innermost last):
620 File "<stdin>", line 1
621IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000622>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000623\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000624%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000625The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000626pointing {\em between} characters, with the left edge of the first
627character numbered 0. Then the right edge of the last character of a
628string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000629
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000630\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000631 +---+---+---+---+---+
632 | H | e | l | p | A |
633 +---+---+---+---+---+
634 0 1 2 3 4 5
635-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000636\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000637%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000638The first row of numbers gives the position of the indices 0...5 in
639the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000640The slice from \verb\i\ to \verb\j\ consists of all characters between
641the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000642
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000643For nonnegative indices, the length of a slice is the difference of
644the indices, if both are within bounds, e.g., the length of
645\verb\word[1:3]\ is 2.
646
647The built-in function {\tt len()} returns the length of a string:
648
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000649\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000650>>> s = 'supercalifragilisticexpialidocious'
651>>> len(s)
65234
653>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000654\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000655
656\subsection{Lists}
657
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000658Python knows a number of {\em compound} data types, used to group
659together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000660can be written as a list of comma-separated values (items) between
661square brackets. List items need not all have the same type.
662
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000663\bcode\begin{verbatim}
Guido van Rossume5f8b601995-01-04 19:12:49 +0000664>>> a = ['spam', 'eggs', 100, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000665>>> a
Guido van Rossume5f8b601995-01-04 19:12:49 +0000666['spam', 'eggs', 100, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000667>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000668\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000669%
670Like string indices, list indices start at 0, and lists can be sliced,
671concatenated and so on:
672
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000673\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000674>>> a[0]
Guido van Rossume5f8b601995-01-04 19:12:49 +0000675'spam'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000676>>> a[3]
6771234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000678>>> a[-2]
679100
680>>> a[1:-1]
Guido van Rossume5f8b601995-01-04 19:12:49 +0000681['eggs', 100]
682>>> a[:2] + ['bacon', 2*2]
683['spam', 'eggs', 'bacon', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000684>>> 3*a[:3] + ['Boe!']
Guido van Rossume5f8b601995-01-04 19:12:49 +0000685['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000686>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000687\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000688%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000689Unlike strings, which are {\em immutable}, it is possible to change
690individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000691
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000692\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000693>>> a
Guido van Rossume5f8b601995-01-04 19:12:49 +0000694['spam', 'eggs', 100, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000695>>> a[2] = a[2] + 23
696>>> a
Guido van Rossume5f8b601995-01-04 19:12:49 +0000697['spam', 'eggs', 123, 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000698>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000699\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000700%
701Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000702of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000703
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000704\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000705>>> # Replace some items:
Guido van Rossum6938f061994-08-01 12:22:53 +0000706... a[0:2] = [1, 12]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000707>>> a
708[1, 12, 123, 1234]
709>>> # Remove some:
Guido van Rossum6938f061994-08-01 12:22:53 +0000710... a[0:2] = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000711>>> a
712[123, 1234]
713>>> # Insert some:
Guido van Rossum6938f061994-08-01 12:22:53 +0000714... a[1:1] = ['bletch', 'xyzzy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000715>>> a
716[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000717>>> a[:0] = a # Insert (a copy of) itself at the beginning
718>>> a
719[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000720>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000721\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000722%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000723The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000724
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000725\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000726>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00007278
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000728>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000729\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000730%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000731It is possible to nest lists (create lists containing other lists),
732for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000733
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000734\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000735>>> q = [2, 3]
736>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000737>>> len(p)
7383
739>>> p[1]
740[2, 3]
741>>> p[1][0]
7422
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000743>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000744>>> p
745[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000746>>> q
747[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000748>>>
749\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000750%
751Note that in the last example, {\tt p[1]} and {\tt q} really refer to
752the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000753
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000754\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000755
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000756Of course, we can use Python for more complicated tasks than adding
757two and two together. For instance, we can write an initial
758subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000759
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000760\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000761>>> # Fibonacci series:
Guido van Rossum6938f061994-08-01 12:22:53 +0000762... # the sum of two elements defines the next
763... a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000764>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000765... print b
766... a, b = b, a+b
767...
7681
7691
7702
7713
7725
7738
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000774>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000775\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000776%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000777This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000778
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000779\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000780
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000781\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000782The first line contains a {\em multiple assignment}: the variables
783{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
784last line this is used again, demonstrating that the expressions on
785the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000786assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000787
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000788\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000789The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossum16cd7f91994-10-06 10:29:26 +000079010}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000791true; zero is false. The condition may also be a string or list value,
792in fact any sequence; anything with a non-zero length is true, empty
793sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000794comparison. The standard comparison operators are written the same as
795in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000796
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000797\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000798The {\em body} of the loop is {\em indented}: indentation is Python's
799way of grouping statements. Python does not (yet!) provide an
800intelligent input line editing facility, so you have to type a tab or
801space(s) for each indented line. In practice you will prepare more
802complicated input for Python with a text editor; most text editors have
803an auto-indent facility. When a compound statement is entered
804interactively, it must be followed by a blank line to indicate
805completion (since the parser cannot guess when you have typed the last
806line).
807
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000808\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000809The {\tt print} statement writes the value of the expression(s) it is
810given. It differs from just writing the expression you want to write
811(as we did earlier in the calculator examples) in the way it handles
Guido van Rossum16cd7f91994-10-06 10:29:26 +0000812multiple expressions and strings. Strings are printed without quotes,
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000813and a space is inserted between items, so you can format things nicely,
814like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000815
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000816\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000817>>> i = 256*256
818>>> print 'The value of i is', i
819The value of i is 65536
820>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000821\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000822%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000823A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000824
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000825\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000826>>> a, b = 0, 1
827>>> while b < 1000:
828... print b,
829... a, b = b, a+b
830...
8311 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
832>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000833\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000834%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000835Note that the interpreter inserts a newline before it prints the next
836prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000837
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000838\end{itemize}
839
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000840
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000841\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000842
Guido van Rossum4410c751991-06-04 20:22:18 +0000843Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000844usual control flow statements known from other languages, with some
845twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000846
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000847\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000848
849Perhaps the most well-known statement type is the {\tt if} statement.
850For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000851
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000852\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000853>>> if x < 0:
854... x = 0
855... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000856... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000857... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000858... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000859... print 'Single'
860... else:
861... print 'More'
862...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000863\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000864%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000865There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000866optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
867useful to avoid excessive indentation. An {\tt if...elif...elif...}
868sequence is a substitute for the {\em switch} or {\em case} statements
869found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000870
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000871\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000872
Guido van Rossum4410c751991-06-04 20:22:18 +0000873The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000874used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000875arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000876completely free in the iteration test and step (as C), Python's {\tt
877for} statement iterates over the items of any sequence (e.g., a list
878or a string), in the order that they appear in the sequence. For
879example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000880
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000881\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000882>>> # Measure some strings:
Guido van Rossum6938f061994-08-01 12:22:53 +0000883... a = ['cat', 'window', 'defenestrate']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000884>>> for x in a:
885... print x, len(x)
886...
887cat 3
888window 6
889defenestrate 12
890>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000891\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000892%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000893It is not safe to modify the sequence being iterated over in the loop
894(this can only happen for mutable sequence types, i.e., lists). If
895you need to modify the list you are iterating over, e.g., duplicate
896selected items, you must iterate over a copy. The slice notation
897makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000898
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000899\bcode\begin{verbatim}
900>>> for x in a[:]: # make a slice copy of the entire list
901... if len(x) > 6: a.insert(0, x)
902...
903>>> a
904['defenestrate', 'cat', 'window', 'defenestrate']
905>>>
906\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000907
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000908\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000909
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000910If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000911function {\tt range()} comes in handy. It generates lists containing
912arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000913
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000914\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000915>>> range(10)
916[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
917>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000918\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000919%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000920The given end point is never part of the generated list; {\tt range(10)}
921generates a list of 10 values, exactly the legal indices for items of a
922sequence of length 10. It is possible to let the range start at another
923number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000924
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000925\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000926>>> range(5, 10)
927[5, 6, 7, 8, 9]
928>>> range(0, 10, 3)
929[0, 3, 6, 9]
930>>> range(-10, -100, -30)
931[-10, -40, -70]
932>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000933\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000934%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000935To iterate over the indices of a sequence, combine {\tt range()} and
936{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000937
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000938\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000939>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000940>>> for i in range(len(a)):
941... print i, a[i]
942...
9430 Mary
9441 had
9452 a
9463 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009474 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000948>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000949\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000950
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000951\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000952
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000953The {\tt break} statement, like in C, breaks out of the smallest
954enclosing {\tt for} or {\tt while} loop.
955
956The {\tt continue} statement, also borrowed from C, continues with the
957next iteration of the loop.
958
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000959Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000960loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000961the condition becomes false (with {\tt while}), but not when the loop is
962terminated by a {\tt break} statement. This is exemplified by the
Guido van Rossumcfb45e41994-11-10 23:04:43 +0000963following loop, which searches for prime numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000964
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000965\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000966>>> for n in range(2, 10):
967... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000968... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000969... print n, 'equals', x, '*', n/x
970... break
971... else:
972... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000973...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009742 is a prime number
9753 is a prime number
9764 equals 2 * 2
9775 is a prime number
9786 equals 2 * 3
9797 is a prime number
9808 equals 2 * 4
9819 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000982>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000983\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000984
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000985\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000986
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000987The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000988It can be used when a statement is required syntactically but the
989program requires no action.
990For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000991
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000992\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000993>>> while 1:
994... pass # Busy-wait for keyboard interrupt
995...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000996\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000997
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000998\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000999
1000We can create a function that writes the Fibonacci series to an
1001arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001002
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001003\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001004>>> def fib(n): # write Fibonacci series up to n
1005... a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001006... while b < n:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001007... print b,
1008... a, b = b, a+b
1009...
1010>>> # Now call the function we just defined:
Guido van Rossum6938f061994-08-01 12:22:53 +00001011... fib(2000)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000010121 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
1013>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001014\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001015%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001016The keyword {\tt def} introduces a function {\em definition}. It must
1017be followed by the function name and the parenthesized list of formal
1018parameters. The statements that form the body of the function starts at
1019the next line, indented by a tab stop.
1020
1021The {\em execution} of a function introduces a new symbol table used
1022for the local variables of the function. More precisely, all variable
1023assignments in a function store the value in the local symbol table;
1024whereas
Guido van Rossum6938f061994-08-01 12:22:53 +00001025variable references first look in the local symbol table, then
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001026in the global symbol table, and then in the table of built-in names.
1027Thus,
Guido van Rossumcfb45e41994-11-10 23:04:43 +00001028global variables cannot be directly assigned a value within a
Guido van Rossum6938f061994-08-01 12:22:53 +00001029function (unless named in a {\tt global} statement), although
1030they may be referenced.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001031
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001032The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001033the local symbol table of the called function when it is called; thus,
1034arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001035\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001036 Actually, {\em call by object reference} would be a better
1037 description, since if a mutable object is passed, the caller
1038 will see any changes the callee makes to it (e.g., items
1039 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001040}
1041When a function calls another function, a new local symbol table is
1042created for that call.
1043
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001044A function definition introduces the function name in the
1045current
1046symbol table. The value
1047of the function name
1048has a type that is recognized by the interpreter as a user-defined
1049function. This value can be assigned to another name which can then
1050also be used as a function. This serves as a general renaming
1051mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001052
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001053\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001054>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001055<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001056>>> f = fib
1057>>> f(100)
10581 1 2 3 5 8 13 21 34 55 89
1059>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001060\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001061%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001062You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001063Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001064value. In fact, technically speaking, procedures do return a value,
1065albeit a rather boring one. This value is called {\tt None} (it's a
1066built-in name). Writing the value {\tt None} is normally suppressed by
1067the interpreter if it would be the only value written. You can see it
1068if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001069
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001070\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001071>>> print fib(0)
1072None
1073>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001074\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001075%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001076It is simple to write a function that returns a list of the numbers of
1077the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001078
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001079\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001080>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001081... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001082... a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001083... while b < n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001084... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001085... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001086... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001087...
1088>>> f100 = fib2(100) # call it
1089>>> f100 # write the result
1090[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1091>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001092\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001093%
Guido van Rossum4410c751991-06-04 20:22:18 +00001094This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001095
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001096\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001097
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001098\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001099The {\tt return} statement returns with a value from a function. {\tt
1100return} without an expression argument is used to return from the middle
Guido van Rossum6938f061994-08-01 12:22:53 +00001101of a procedure (falling off the end also returns from a procedure), in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001102which case the {\tt None} value is returned.
1103
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001104\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001105The statement {\tt result.append(b)} calls a {\em method} of the list
1106object {\tt result}. A method is a function that `belongs' to an
1107object and is named {\tt obj.methodname}, where {\tt obj} is some
1108object (this may be an expression), and {\tt methodname} is the name
1109of a method that is defined by the object's type. Different types
1110define different methods. Methods of different types may have the
1111same name without causing ambiguity. (It is possible to define your
Guido van Rossum6938f061994-08-01 12:22:53 +00001112own object types and methods, using {\em classes}, as discussed later
1113in this tutorial.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001114The method {\tt append} shown in the example, is defined for
1115list objects; it adds a new element at the end of the list. In this
1116example
1117it is equivalent to {\tt result = result + [b]}, but more efficient.
1118
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001119\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001120
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001121
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001122\chapter{Odds and Ends}
1123
1124This chapter describes some things you've learned about already in
1125more detail, and adds some new things as well.
1126
1127\section{More on Lists}
1128
1129The list data type has some more methods. Here are all of the methods
1130of lists objects:
1131
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001132\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001133
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001134\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001135Insert an item at a given position. The first argument is the index of
1136the element before which to insert, so {\tt a.insert(0, x)} inserts at
1137the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1138{\tt a.append(x)}.
1139
1140\item[{\tt append(x)}]
1141Equivalent to {\tt a.insert(len(a), x)}.
1142
1143\item[{\tt index(x)}]
1144Return the index in the list of the first item whose value is {\tt x}.
1145It is an error if there is no such item.
1146
1147\item[{\tt remove(x)}]
1148Remove the first item from the list whose value is {\tt x}.
1149It is an error if there is no such item.
1150
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001151\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001152Sort the items of the list, in place.
1153
1154\item[{\tt reverse()}]
1155Reverse the elements of the list, in place.
1156
Guido van Rossum6938f061994-08-01 12:22:53 +00001157\item[{\tt count(x)}]
1158Return the number of times {\tt x} appears in the list.
1159
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001160\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001161
1162An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001163
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001164\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001165>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossum6938f061994-08-01 12:22:53 +00001166>>> print a.count(333), a.count(66.6), a.count('x')
11672 1 0
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001168>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001169>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001170>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001171[66.6, 333, -1, 333, 1, 1234.5, 333]
1172>>> a.index(333)
11731
1174>>> a.remove(333)
1175>>> a
1176[66.6, -1, 333, 1, 1234.5, 333]
1177>>> a.reverse()
1178>>> a
1179[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001180>>> a.sort()
1181>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001182[-1, 1, 66.6, 333, 333, 1234.5]
1183>>>
1184\end{verbatim}\ecode
1185
1186\section{The {\tt del} statement}
1187
1188There is a way to remove an item from a list given its index instead
1189of its value: the {\tt del} statement. This can also be used to
1190remove slices from a list (which we did earlier by assignment of an
1191empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001192
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001193\bcode\begin{verbatim}
1194>>> a
1195[-1, 1, 66.6, 333, 333, 1234.5]
1196>>> del a[0]
1197>>> a
1198[1, 66.6, 333, 333, 1234.5]
1199>>> del a[2:4]
1200>>> a
1201[1, 66.6, 1234.5]
1202>>>
1203\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001204%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001205{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001206
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001207\bcode\begin{verbatim}
1208>>> del a
1209>>>
1210\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001211%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001212Referencing the name {\tt a} hereafter is an error (at least until
1213another value is assigned to it). We'll find other uses for {\tt del}
1214later.
1215
1216\section{Tuples and Sequences}
1217
1218We saw that lists and strings have many common properties, e.g.,
Guido van Rossum6938f061994-08-01 12:22:53 +00001219indexing and slicing operations. They are two examples of {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001220sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001221sequence data types may be added. There is also another standard
1222sequence data type: the {\em tuple}.
1223
1224A tuple consists of a number of values separated by commas, for
1225instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001226
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001227\bcode\begin{verbatim}
1228>>> t = 12345, 54321, 'hello!'
1229>>> t[0]
123012345
1231>>> t
1232(12345, 54321, 'hello!')
1233>>> # Tuples may be nested:
Guido van Rossum6938f061994-08-01 12:22:53 +00001234... u = t, (1, 2, 3, 4, 5)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001235>>> u
1236((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1237>>>
1238\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001239%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001240As you see, on output tuples are alway enclosed in parentheses, so
1241that nested tuples are interpreted correctly; they may be input with
1242or without surrounding parentheses, although often parentheses are
1243necessary anyway (if the tuple is part of a larger expression).
1244
1245Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1246from a database, etc. Tuples, like strings, are immutable: it is not
1247possible to assign to the individual items of a tuple (you can
1248simulate much of the same effect with slicing and concatenation,
1249though).
1250
1251A special problem is the construction of tuples containing 0 or 1
Guido van Rossum6938f061994-08-01 12:22:53 +00001252items: the syntax has some extra quirks to accommodate these. Empty
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001253tuples are constructed by an empty pair of parentheses; a tuple with
1254one item is constructed by following a value with a comma
1255(it is not sufficient to enclose a single value in parentheses).
1256Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001257
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001258\bcode\begin{verbatim}
1259>>> empty = ()
1260>>> singleton = 'hello', # <-- note trailing comma
1261>>> len(empty)
12620
1263>>> len(singleton)
12641
1265>>> singleton
1266('hello',)
1267>>>
1268\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001269%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001270The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1271tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1272are packed together in a tuple. The reverse operation is also
1273possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001274
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001275\bcode\begin{verbatim}
1276>>> x, y, z = t
1277>>>
1278\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001279%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001280This is called, appropriately enough, {\em tuple unpacking}. Tuple
1281unpacking requires that the list of variables on the left has the same
1282number of elements as the length of the tuple. Note that multiple
1283assignment is really just a combination of tuple packing and tuple
1284unpacking!
1285
1286Occasionally, the corresponding operation on lists is useful: {\em list
1287unpacking}. This is supported by enclosing the list of variables in
1288square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001289
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001290\bcode\begin{verbatim}
Guido van Rossume5f8b601995-01-04 19:12:49 +00001291>>> a = ['spam', 'eggs', 100, 1234]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001292>>> [a1, a2, a3, a4] = a
1293>>>
1294\end{verbatim}\ecode
1295
1296\section{Dictionaries}
1297
1298Another useful data type built into Python is the {\em dictionary}.
1299Dictionaries are sometimes found in other languages as ``associative
1300memories'' or ``associative arrays''. Unlike sequences, which are
1301indexed by a range of numbers, dictionaries are indexed by {\em keys},
Guido van Rossum6938f061994-08-01 12:22:53 +00001302which are strings (the use of non-string values as keys
1303is supported, but beyond the scope of this tutorial).
1304It is best to think of a dictionary as an unordered set of
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001305{\em key:value} pairs, with the requirement that the keys are unique
1306(within one dictionary).
1307A pair of braces creates an empty dictionary: \verb/{}/.
1308Placing a comma-separated list of key:value pairs within the
1309braces adds initial key:value pairs to the dictionary; this is also the
1310way dictionaries are written on output.
1311
1312The main operations on a dictionary are storing a value with some key
1313and extracting the value given the key. It is also possible to delete
1314a key:value pair
1315with {\tt del}.
1316If you store using a key that is already in use, the old value
1317associated with that key is forgotten. It is an error to extract a
Guido van Rossum6938f061994-08-01 12:22:53 +00001318value using a non-existent key.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001319
1320The {\tt keys()} method of a dictionary object returns a list of all the
1321keys used in the dictionary, in random order (if you want it sorted,
1322just apply the {\tt sort()} method to the list of keys). To check
1323whether a single key is in the dictionary, use the \verb/has_key()/
1324method of the dictionary.
1325
1326Here is a small example using a dictionary:
1327
1328\bcode\begin{verbatim}
1329>>> tel = {'jack': 4098, 'sape': 4139}
1330>>> tel['guido'] = 4127
1331>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001332{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001333>>> tel['jack']
13344098
1335>>> del tel['sape']
1336>>> tel['irv'] = 4127
1337>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001338{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001339>>> tel.keys()
1340['guido', 'irv', 'jack']
1341>>> tel.has_key('guido')
13421
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001343>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001344\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001345
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001346\section{More on Conditions}
1347
1348The conditions used in {\tt while} and {\tt if} statements above can
1349contain other operators besides comparisons.
1350
1351The comparison operators {\tt in} and {\tt not in} check whether a value
1352occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1353is not} compare whether two objects are really the same object; this
1354only matters for mutable objects like lists. All comparison operators
1355have the same priority, which is lower than that of all numerical
1356operators.
1357
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001358Comparisons can be chained: e.g., {\tt a < b == c} tests whether {\tt a}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001359is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1360
1361Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1362or}, and the outcome of a comparison (or of any other Boolean
1363expression) may be negated with {\tt not}. These all have lower
1364priorities than comparison operators again; between them, {\tt not} has
1365the highest priority, and {\tt or} the lowest, so that
1366{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1367course, parentheses can be used to express the desired composition.
1368
1369The Boolean operators {\tt and} and {\tt or} are so-called {\em
1370shortcut} operators: their arguments are evaluated from left to right,
1371and evaluation stops as soon as the outcome is determined. E.g., if
1372{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1373does not evaluate the expression C. In general, the return value of a
1374shortcut operator, when used as a general value and not as a Boolean, is
1375the last evaluated argument.
1376
1377It is possible to assign the result of a comparison or other Boolean
Guido van Rossum6938f061994-08-01 12:22:53 +00001378expression to a variable. For example,
1379
1380\bcode\begin{verbatim}
1381>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
1382>>> non_null = string1 or string2 or string3
1383>>> non_null
1384'Trondheim'
1385>>>
1386\end{verbatim}\ecode
1387%
1388Note that in Python, unlike C, assignment cannot occur inside expressions.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001389
1390\section{Comparing Sequences and Other Types}
1391
1392Sequence objects may be compared to other objects with the same
1393sequence type. The comparison uses {\em lexicographical} ordering:
1394first the first two items are compared, and if they differ this
1395determines the outcome of the comparison; if they are equal, the next
1396two items are compared, and so on, until either sequence is exhausted.
1397If two items to be compared are themselves sequences of the same type,
Guido van Rossum6938f061994-08-01 12:22:53 +00001398the lexicographical comparison is carried out recursively. If all
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001399items of two sequences compare equal, the sequences are considered
1400equal. If one sequence is an initial subsequence of the other, the
1401shorted sequence is the smaller one. Lexicographical ordering for
Guido van Rossum47b4c0f1995-03-15 11:25:32 +00001402strings uses the \ASCII{} ordering for individual characters. Some
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001403examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001404
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001405\bcode\begin{verbatim}
1406(1, 2, 3) < (1, 2, 4)
1407[1, 2, 3] < [1, 2, 4]
1408'ABC' < 'C' < 'Pascal' < 'Python'
1409(1, 2, 3, 4) < (1, 2, 4)
1410(1, 2) < (1, 2, -1)
1411(1, 2, 3) = (1.0, 2.0, 3.0)
1412(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1413\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001414%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001415Note that comparing objects of different types is legal. The outcome
1416is deterministic but arbitrary: the types are ordered by their name.
1417Thus, a list is always smaller than a string, a string is always
1418smaller than a tuple, etc. Mixed numeric types are compared according
1419to their numeric value, so 0 equals 0.0, etc.%
1420\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001421 The rules for comparing objects of different types should
1422 not be relied upon; they may change in a future version of
1423 the language.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001424}
1425
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001426
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001427\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001428
Guido van Rossum4410c751991-06-04 20:22:18 +00001429If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001430definitions you have made (functions and variables) are lost.
1431Therefore, if you want to write a somewhat longer program, you are
1432better off using a text editor to prepare the input for the interpreter
Guido van Rossum16d6e711994-08-08 12:30:22 +00001433and running it with that file as input instead. This is known as creating a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001434{\em script}. As your program gets longer, you may want to split it
1435into several files for easier maintenance. You may also want to use a
1436handy function that you've written in several programs without copying
1437its definition into each program.
1438
Guido van Rossum4410c751991-06-04 20:22:18 +00001439To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001440them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001441Such a file is called a {\em module}; definitions from a module can be
1442{\em imported} into other modules or into the {\em main} module (the
1443collection of variables that you have access to in a script
1444executed at the top level
1445and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001446
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001447A module is a file containing Python definitions and statements. The
Guido van Rossum6938f061994-08-01 12:22:53 +00001448file name is the module name with the suffix {\tt .py} appended. Within
1449a module, the module's name (as a string) is available as the value of
1450the global variable {\tt __name__}. For instance, use your favorite text
1451editor to create a file called {\tt fibo.py} in the current directory
1452with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001453
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001454\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001455# Fibonacci numbers module
1456
1457def fib(n): # write Fibonacci series up to n
1458 a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001459 while b < n:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001460 print b,
1461 a, b = b, a+b
1462
1463def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001464 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001465 a, b = 0, 1
Guido van Rossum16cd7f91994-10-06 10:29:26 +00001466 while b < n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001467 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001468 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001469 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001470\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001471%
Guido van Rossum4410c751991-06-04 20:22:18 +00001472Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001473following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001474
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001475\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001476>>> import fibo
1477>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001478\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001479%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001480This does not enter the names of the functions defined in
1481{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001482directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001483{\tt fibo}
1484there.
1485Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001486
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001487\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001488>>> fibo.fib(1000)
14891 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1490>>> fibo.fib2(100)
1491[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Guido van Rossum6938f061994-08-01 12:22:53 +00001492>>> fibo.__name__
1493'fibo'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001494>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001495\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001496%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001497If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001498
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001499\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001500>>> fib = fibo.fib
1501>>> fib(500)
15021 1 2 3 5 8 13 21 34 55 89 144 233 377
1503>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001504\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001505
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001506\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001507
1508A module can contain executable statements as well as function
1509definitions.
1510These statements are intended to initialize the module.
1511They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001512{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001513time the module is imported somewhere.%
1514\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001515 In fact function definitions are also `statements' that are
1516 `executed'; the execution enters the function name in the
1517 module's global symbol table.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001518}
1519
1520Each module has its own private symbol table, which is used as the
1521global symbol table by all functions defined in the module.
1522Thus, the author of a module can use global variables in the module
1523without worrying about accidental clashes with a user's global
1524variables.
1525On the other hand, if you know what you are doing you can touch a
1526module's global variables with the same notation used to refer to its
1527functions,
1528{\tt modname.itemname}.
1529
1530Modules can import other modules.
1531It is customary but not required to place all
1532{\tt import}
1533statements at the beginning of a module (or script, for that matter).
1534The imported module names are placed in the importing module's global
1535symbol table.
1536
1537There is a variant of the
1538{\tt import}
1539statement that imports names from a module directly into the importing
1540module's symbol table.
1541For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001542
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001543\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001544>>> from fibo import fib, fib2
1545>>> fib(500)
15461 1 2 3 5 8 13 21 34 55 89 144 233 377
1547>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001548\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001549%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001550This does not introduce the module name from which the imports are taken
1551in the local symbol table (so in the example, {\tt fibo} is not
1552defined).
1553
1554There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001555
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001556\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001557>>> from fibo import *
1558>>> fib(500)
15591 1 2 3 5 8 13 21 34 55 89 144 233 377
1560>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001561\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001562%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001563This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001564({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001565
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001566\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001567
Guido van Rossum4410c751991-06-04 20:22:18 +00001568Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001569document (Python Library Reference). Some modules are built into the
1570interpreter; these provide access to operations that are not part of the
1571core of the language but are nevertheless built in, either for
1572efficiency or to provide access to operating system primitives such as
1573system calls. The set of such modules is a configuration option; e.g.,
1574the {\tt amoeba} module is only provided on systems that somehow support
1575Amoeba primitives. One particular module deserves some attention: {\tt
1576sys}, which is built into every Python interpreter. The variables {\tt
1577sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1578secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001579
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001580\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001581>>> import sys
1582>>> sys.ps1
1583'>>> '
1584>>> sys.ps2
1585'... '
1586>>> sys.ps1 = 'C> '
1587C> print 'Yuck!'
1588Yuck!
1589C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001590\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001591%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001592These two variables are only defined if the interpreter is in
1593interactive mode.
1594
1595The variable
1596{\tt sys.path}
1597is a list of strings that determine the interpreter's search path for
1598modules.
1599It is initialized to a default path taken from the environment variable
1600{\tt PYTHONPATH},
1601or from a built-in default if
1602{\tt PYTHONPATH}
1603is not set.
1604You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001605
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001606\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001607>>> import sys
1608>>> sys.path.append('/ufs/guido/lib/python')
1609>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001610\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001611
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001612\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001613
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001614The built-in function {\tt dir} is used to find out which names a module
1615defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001616
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001617\bcode\begin{verbatim}
1618>>> import fibo, sys
1619>>> dir(fibo)
Guido van Rossum6938f061994-08-01 12:22:53 +00001620['__name__', 'fib', 'fib2']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001621>>> dir(sys)
Guido van Rossum6938f061994-08-01 12:22:53 +00001622['__name__', 'argv', 'builtin_module_names', 'copyright', 'exit',
1623'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',
1624'stderr', 'stdin', 'stdout', 'version']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001625>>>
1626\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001627%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001628Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001629
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001630\bcode\begin{verbatim}
1631>>> a = [1, 2, 3, 4, 5]
1632>>> import fibo, sys
1633>>> fib = fibo.fib
1634>>> dir()
Guido van Rossum6938f061994-08-01 12:22:53 +00001635['__name__', 'a', 'fib', 'fibo', 'sys']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001636>>>
1637\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001638%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001639Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001640
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001641{\tt dir()} does not list the names of built-in functions and variables.
1642If you want a list of those, they are defined in the standard module
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001643{\tt __builtin__}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001644
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001645\bcode\begin{verbatim}
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001646>>> import __builtin__
1647>>> dir(__builtin__)
Guido van Rossum6938f061994-08-01 12:22:53 +00001648['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError',
1649'ImportError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
1650'MemoryError', 'NameError', 'None', 'OverflowError', 'RuntimeError',
1651'SyntaxError', 'SystemError', 'SystemExit', 'TypeError', 'ValueError',
1652'ZeroDivisionError', '__name__', 'abs', 'apply', 'chr', 'cmp', 'coerce',
1653'compile', 'dir', 'divmod', 'eval', 'execfile', 'filter', 'float',
1654'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'len', 'long',
1655'map', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input',
1656'reduce', 'reload', 'repr', 'round', 'setattr', 'str', 'type', 'xrange']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001657>>>
1658\end{verbatim}\ecode
1659
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001660
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001661\chapter{Output Formatting}
1662
1663So far we've encountered two ways of writing values: {\em expression
1664statements} and the {\tt print} statement. (A third way is using the
1665{\tt write} method of file objects; the standard output file can be
1666referenced as {\tt sys.stdout}. See the Library Reference for more
1667information on this.)
1668
1669Often you'll want more control over the formatting of your output than
1670simply printing space-separated values. The key to nice formatting in
1671Python is to do all the string handling yourself; using string slicing
1672and concatenation operations you can create any lay-out you can imagine.
1673The standard module {\tt string} contains some useful operations for
1674padding strings to a given column width; these will be discussed shortly.
Guido van Rossum6938f061994-08-01 12:22:53 +00001675Finally, the \code{\%} operator (modulo) with a string left argument
1676interprets this string as a C sprintf format string to be applied to the
1677right argument, and returns the string resulting from this formatting
1678operation.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001679
1680One question remains, of course: how do you convert values to strings?
1681Luckily, Python has a way to convert any value to a string: just write
1682the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001683
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001684\bcode\begin{verbatim}
1685>>> x = 10 * 3.14
1686>>> y = 200*200
1687>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1688>>> print s
1689The value of x is 31.4, and y is 40000...
1690>>> # Reverse quotes work on other types besides numbers:
Guido van Rossum6938f061994-08-01 12:22:53 +00001691... p = [x, y]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001692>>> ps = `p`
1693>>> ps
1694'[31.4, 40000]'
1695>>> # Converting a string adds string quotes and backslashes:
Guido van Rossum6938f061994-08-01 12:22:53 +00001696... hello = 'hello, world\n'
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001697>>> hellos = `hello`
1698>>> print hellos
1699'hello, world\012'
1700>>> # The argument of reverse quotes may be a tuple:
Guido van Rossume5f8b601995-01-04 19:12:49 +00001701... `x, y, ('spam', 'eggs')`
1702"(31.4, 40000, ('spam', 'eggs'))"
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001703>>>
1704\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001705%
Guido van Rossum6938f061994-08-01 12:22:53 +00001706Here are two ways to write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001707
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001708\bcode\begin{verbatim}
1709>>> import string
1710>>> for x in range(1, 11):
1711... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1712... # Note trailing comma on previous line
1713... print string.rjust(`x*x*x`, 4)
1714...
1715 1 1 1
1716 2 4 8
1717 3 9 27
1718 4 16 64
1719 5 25 125
1720 6 36 216
1721 7 49 343
1722 8 64 512
1723 9 81 729
172410 100 1000
Guido van Rossum6938f061994-08-01 12:22:53 +00001725>>> for x in range(1,11):
1726... print '%2d %3d %4d' % (x, x*x, x*x*x)
1727...
1728 1 1 1
1729 2 4 8
1730 3 9 27
1731 4 16 64
1732 5 25 125
1733 6 36 216
1734 7 49 343
1735 8 64 512
1736 9 81 729
173710 100 1000
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001738>>>
1739\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001740%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001741(Note that one space between each column was added by the way {\tt print}
1742works: it always adds spaces between its arguments.)
1743
1744This example demonstrates the function {\tt string.rjust()}, which
1745right-justifies a string in a field of a given width by padding it with
1746spaces on the left. There are similar functions {\tt string.ljust()}
1747and {\tt string.center()}. These functions do not write anything, they
1748just return a new string. If the input string is too long, they don't
1749truncate it, but return it unchanged; this will mess up your column
1750lay-out but that's usually better than the alternative, which would be
1751lying about a value. (If you really want truncation you can always add
1752a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1753
1754There is another function, {\tt string.zfill}, which pads a numeric
1755string on the left with zeros. It understands about plus and minus
Guido van Rossum6938f061994-08-01 12:22:53 +00001756signs:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001757
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001758\bcode\begin{verbatim}
1759>>> string.zfill('12', 5)
1760'00012'
1761>>> string.zfill('-3.14', 7)
1762'-003.14'
1763>>> string.zfill('3.14159265359', 5)
1764'3.14159265359'
1765>>>
1766\end{verbatim}\ecode
1767
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001768
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001769\chapter{Errors and Exceptions}
1770
1771Until now error messages haven't been more than mentioned, but if you
1772have tried out the examples you have probably seen some. There are
1773(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1774and {\em exceptions}.
1775
1776\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001777
1778Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001779kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001780
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001781\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001782>>> while 1 print 'Hello world'
Guido van Rossum6938f061994-08-01 12:22:53 +00001783 File "<stdin>", line 1
1784 while 1 print 'Hello world'
1785 ^
1786SyntaxError: invalid syntax
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001787>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001788\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001789%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001790The parser repeats the offending line and displays a little `arrow'
1791pointing at the earliest point in the line where the error was detected.
1792The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001793{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001794the arrow: in the example, the error is detected at the keyword
1795{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001796File name and line number are printed so you know where to look in case
1797the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001798
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001799\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001800
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001801Even if a statement or expression is syntactically correct, it may
1802cause an error when an attempt is made to execute it.
1803Errors detected during execution are called {\em exceptions} and are
1804not unconditionally fatal: you will soon learn how to handle them in
1805Python programs. Most exceptions are not handled by programs,
1806however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001807
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001808\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001809>>> 10 * (1/0)
Guido van Rossum3cbc16d1993-12-17 12:13:53 +00001810Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001811 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001812ZeroDivisionError: integer division or modulo
Guido van Rossume5f8b601995-01-04 19:12:49 +00001813>>> 4 + spam*3
Guido van Rossum6938f061994-08-01 12:22:53 +00001814Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001815 File "<stdin>", line 1
Guido van Rossume5f8b601995-01-04 19:12:49 +00001816NameError: spam
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001817>>> '2' + 2
Guido van Rossum6938f061994-08-01 12:22:53 +00001818Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001819 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001820TypeError: illegal argument type for built-in operation
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001821>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001822\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001823%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001824The last line of the error message indicates what happened.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001825Exceptions come in different types, and the type is printed as part of
1826the message: the types in the example are
Guido van Rossumb2c65561993-05-12 08:53:36 +00001827{\tt ZeroDivisionError},
1828{\tt NameError}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001829and
Guido van Rossumb2c65561993-05-12 08:53:36 +00001830{\tt TypeError}.
1831The string printed as the exception type is the name of the built-in
1832name for the exception that occurred. This is true for all built-in
1833exceptions, but need not be true for user-defined exceptions (although
1834it is a useful convention).
1835Standard exception names are built-in identifiers (not reserved
1836keywords).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001837
Guido van Rossumb2c65561993-05-12 08:53:36 +00001838The rest of the line is a detail whose interpretation depends on the
1839exception type; its meaning is dependent on the exception type.
1840
1841The preceding part of the error message shows the context where the
1842exception happened, in the form of a stack backtrace.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001843In general it contains a stack backtrace listing source lines; however,
1844it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001845
Guido van Rossumb2c65561993-05-12 08:53:36 +00001846The Python library reference manual lists the built-in exceptions and
1847their meanings.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001848
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001849\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001850
1851It is possible to write programs that handle selected exceptions.
1852Look at the following example, which prints a table of inverses of
1853some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001854
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001855\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001856>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001857>>> for x in numbers:
1858... print x,
1859... try:
1860... print 1.0 / x
Guido van Rossumb2c65561993-05-12 08:53:36 +00001861... except ZeroDivisionError:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001862... print '*** has no inverse ***'
1863...
18640.3333 3.00030003
18652.5 0.4
18660 *** has no inverse ***
186710 0.1
1868>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001869\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001870%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001871The {\tt try} statement works as follows.
1872\begin{itemize}
1873\item
1874First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001875{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001876(the statement(s) between the {\tt try} and {\tt except} keywords) is
1877executed.
1878\item
1879If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001880{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001881is skipped and execution of the {\tt try} statement is finished.
1882\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001883If an exception occurs during execution of the try clause,
1884the rest of the clause is skipped. Then if
1885its type matches the exception named after the {\tt except} keyword,
1886the rest of the try clause is skipped, the except clause is executed,
1887and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001888\item
1889If an exception occurs which does not match the exception named in the
1890except clause, it is passed on to outer try statements; if no handler is
1891found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001892{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001893and execution stops with a message as shown above.
1894\end{itemize}
1895A {\tt try} statement may have more than one except clause, to specify
1896handlers for different exceptions.
1897At most one handler will be executed.
1898Handlers only handle exceptions that occur in the corresponding try
1899clause, not in other handlers of the same {\tt try} statement.
1900An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001901e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001902
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001903\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001904... except (RuntimeError, TypeError, NameError):
1905... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001906\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001907%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001908The last except clause may omit the exception name(s), to serve as a
1909wildcard.
Guido van Rossumb2c65561993-05-12 08:53:36 +00001910Use this with extreme caution, since it is easy to mask a real
1911programming error in this way!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001912
1913When an exception occurs, it may have an associated value, also known as
1914the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001915{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001916The presence and type of the argument depend on the exception type.
1917For exception types which have an argument, the except clause may
1918specify a variable after the exception name (or list) to receive the
1919argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001920
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001921\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001922>>> try:
Guido van Rossume5f8b601995-01-04 19:12:49 +00001923... spam()
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001924... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001925... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001926...
Guido van Rossume5f8b601995-01-04 19:12:49 +00001927name spam undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001928>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001929\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001930%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001931If an exception has an argument, it is printed as the last part
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001932(`detail') of the message for unhandled exceptions.
1933
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001934Exception handlers don't just handle exceptions if they occur
1935immediately in the try clause, but also if they occur inside functions
1936that are called (even indirectly) in the try clause.
1937For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001938
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001939\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001940>>> def this_fails():
1941... x = 1/0
1942...
1943>>> try:
1944... this_fails()
Guido van Rossumb2c65561993-05-12 08:53:36 +00001945... except ZeroDivisionError, detail:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001946... print 'Handling run-time error:', detail
1947...
Guido van Rossumb2c65561993-05-12 08:53:36 +00001948Handling run-time error: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001949>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001950\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001951
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001952\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001953
1954The {\tt raise} statement allows the programmer to force a specified
1955exception to occur.
1956For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001957
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001958\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001959>>> raise NameError, 'HiThere'
Guido van Rossum6938f061994-08-01 12:22:53 +00001960Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001961 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001962NameError: HiThere
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001963>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001964\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001965%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001966The first argument to {\tt raise} names the exception to be raised.
1967The optional second argument specifies the exception's argument.
1968
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001969\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001970
1971Programs may name their own exceptions by assigning a string to a
1972variable.
1973For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001974
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001975\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001976>>> my_exc = 'my_exc'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001977>>> try:
1978... raise my_exc, 2*2
1979... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001980... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001981...
Guido van Rossum6938f061994-08-01 12:22:53 +00001982My exception occurred, value: 4
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001983>>> raise my_exc, 1
Guido van Rossum6938f061994-08-01 12:22:53 +00001984Traceback (innermost last):
1985 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001986my_exc: 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001987>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001988\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001989%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001990Many standard modules use this to report errors that may occur in
1991functions they define.
1992
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001993\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001994
1995The {\tt try} statement has another optional clause which is intended to
1996define clean-up actions that must be executed under all circumstances.
1997For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001998
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001999\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002000>>> try:
2001... raise KeyboardInterrupt
2002... finally:
2003... print 'Goodbye, world!'
2004...
2005Goodbye, world!
Guido van Rossum6938f061994-08-01 12:22:53 +00002006Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002007 File "<stdin>", line 2
Guido van Rossumb2c65561993-05-12 08:53:36 +00002008KeyboardInterrupt
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002009>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002010\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002011%
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00002012A {\tt finally} clause is executed whether or not an exception has
2013occurred in the {\tt try} clause. When an exception has occurred, it
Guido van Rossum6938f061994-08-01 12:22:53 +00002014is re-raised after the {\tt finally} clause is executed. The
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00002015{\tt finally} clause is also executed ``on the way out'' when the
2016{\tt try} statement is left via a {\tt break} or {\tt return}
2017statement.
2018
2019A {\tt try} statement must either have one or more {\tt except}
2020clauses or one {\tt finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002021
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002022
2023\chapter{Classes}
2024
2025Python's class mechanism adds classes to the language with a minimum
2026of new syntax and semantics. It is a mixture of the class mechanisms
Guido van Rossum16d6e711994-08-08 12:30:22 +00002027found in \Cpp{} and Modula-3. As is true for modules, classes in Python
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002028do not put an absolute barrier between definition and user, but rather
2029rely on the politeness of the user not to ``break into the
2030definition.'' The most important features of classes are retained
2031with full power, however: the class inheritance mechanism allows
2032multiple base classes, a derived class can override any methods of its
2033base class(es), a method can call the method of a base class with the
2034same name. Objects can contain an arbitrary amount of private data.
2035
Guido van Rossum16d6e711994-08-08 12:30:22 +00002036In \Cpp{} terminology, all class members (including the data members) are
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002037{\em public}, and all member functions are {\em virtual}. There are
Guido van Rossum6938f061994-08-01 12:22:53 +00002038no special constructors or destructors. As in Modula-3, there are no
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002039shorthands for referencing the object's members from its methods: the
2040method function is declared with an explicit first argument
2041representing the object, which is provided implicitly by the call. As
2042in Smalltalk, classes themselves are objects, albeit in the wider
2043sense of the word: in Python, all data types are objects. This
Guido van Rossum16d6e711994-08-08 12:30:22 +00002044provides semantics for importing and renaming. But, just like in \Cpp{}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002045or Modula-3, built-in types cannot be used as base classes for
Guido van Rossum16d6e711994-08-08 12:30:22 +00002046extension by the user. Also, like in \Cpp{} but unlike in Modula-3, most
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002047built-in operators with special syntax (arithmetic operators,
Guido van Rossum6938f061994-08-01 12:22:53 +00002048subscripting etc.) can be redefined for class members.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002049
2050
2051\section{A word about terminology}
2052
2053Lacking universally accepted terminology to talk about classes, I'll
Guido van Rossum16d6e711994-08-08 12:30:22 +00002054make occasional use of Smalltalk and \Cpp{} terms. (I'd use Modula-3
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002055terms, since its object-oriented semantics are closer to those of
Guido van Rossum16d6e711994-08-08 12:30:22 +00002056Python than \Cpp{}, but I expect that few readers have heard of it...)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002057
2058I also have to warn you that there's a terminological pitfall for
2059object-oriented readers: the word ``object'' in Python does not
Guido van Rossum16d6e711994-08-08 12:30:22 +00002060necessarily mean a class instance. Like \Cpp{} and Modula-3, and unlike
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002061Smalltalk, not all types in Python are classes: the basic built-in
2062types like integers and lists aren't, and even somewhat more exotic
2063types like files aren't. However, {\em all} Python types share a little
2064bit of common semantics that is best described by using the word
2065object.
2066
2067Objects have individuality, and multiple names (in multiple scopes)
2068can be bound to the same object. This is known as aliasing in other
2069languages. This is usually not appreciated on a first glance at
2070Python, and can be safely ignored when dealing with immutable basic
2071types (numbers, strings, tuples). However, aliasing has an
Guido van Rossum6938f061994-08-01 12:22:53 +00002072(intended!) effect on the semantics of Python code involving mutable
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002073objects such as lists, dictionaries, and most types representing
2074entities outside the program (files, windows, etc.). This is usually
2075used to the benefit of the program, since aliases behave like pointers
2076in some respects. For example, passing an object is cheap since only
2077a pointer is passed by the implementation; and if a function modifies
2078an object passed as an argument, the caller will see the change --- this
2079obviates the need for two different argument passing mechanisms as in
2080Pascal.
2081
2082
2083\section{Python scopes and name spaces}
2084
2085Before introducing classes, I first have to tell you something about
2086Python's scope rules. Class definitions play some neat tricks with
2087name spaces, and you need to know how scopes and name spaces work to
2088fully understand what's going on. Incidentally, knowledge about this
2089subject is useful for any advanced Python programmer.
2090
2091Let's begin with some definitions.
2092
2093A {\em name space} is a mapping from names to objects. Most name
2094spaces are currently implemented as Python dictionaries, but that's
2095normally not noticeable in any way (except for performance), and it
2096may change in the future. Examples of name spaces are: the set of
2097built-in names (functions such as \verb\abs()\, and built-in exception
2098names); the global names in a module; and the local names in a
2099function invocation. In a sense the set of attributes of an object
Guido van Rossum16cd7f91994-10-06 10:29:26 +00002100also form a name space. The important thing to know about name
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002101spaces is that there is absolutely no relation between names in
2102different name spaces; for instance, two different modules may both
2103define a function ``maximize'' without confusion --- users of the
2104modules must prefix it with the module name.
2105
2106By the way, I use the word {\em attribute} for any name following a
2107dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2108an attribute of the object \verb\z\. Strictly speaking, references to
2109names in modules are attribute references: in the expression
2110\verb\modname.funcname\, \verb\modname\ is a module object and
2111\verb\funcname\ is an attribute of it. In this case there happens to
2112be a straightforward mapping between the module's attributes and the
2113global names defined in the module: they share the same name space!%
2114\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00002115 Except for one thing. Module objects have a secret read-only
2116 attribute called {\tt __dict__} which returns the dictionary
2117 used to implement the module's name space; the name
2118 {\tt __dict__} is an attribute but not a global name.
2119 Obviously, using this violates the abstraction of name space
2120 implementation, and should be restricted to things like
2121 post-mortem debuggers...
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002122}
2123
2124Attributes may be read-only or writable. In the latter case,
2125assignment to attributes is possible. Module attributes are writable:
2126you can write \verb\modname.the_answer = 42\. Writable attributes may
2127also be deleted with the del statement, e.g.
2128\verb\del modname.the_answer\.
2129
2130Name spaces are created at different moments and have different
2131lifetimes. The name space containing the built-in names is created
2132when the Python interpreter starts up, and is never deleted. The
2133global name space for a module is created when the module definition
2134is read in; normally, module name spaces also last until the
2135interpreter quits. The statements executed by the top-level
2136invocation of the interpreter, either read from a script file or
2137interactively, are considered part of a module called \verb\__main__\,
2138so they have their own global name space. (The built-in names
Guido van Rossum4bd023f1993-10-27 13:49:20 +00002139actually also live in a module; this is called \verb\__builtin__\.)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002140
2141The local name space for a function is created when the function is
2142called, and deleted when the function returns or raises an exception
2143that is not handled within the function. (Actually, forgetting would
2144be a better way to describe what actually happens.) Of course,
2145recursive invocations each have their own local name space.
2146
2147A {\em scope} is a textual region of a Python program where a name space
2148is directly accessible. ``Directly accessible'' here means that an
2149unqualified reference to a name attempts to find the name in the name
2150space.
2151
2152Although scopes are determined statically, they are used dynamically.
2153At any time during execution, exactly three nested scopes are in use
2154(i.e., exactly three name spaces are directly accessible): the
2155innermost scope, which is searched first, contains the local names,
2156the middle scope, searched next, contains the current module's global
2157names, and the outermost scope (searched last) is the name space
2158containing built-in names.
2159
2160Usually, the local scope references the local names of the (textually)
Guido van Rossum96628a91995-04-10 11:34:00 +00002161current function. Outside of functions, the local scope references
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002162the same name space as the global scope: the module's name space.
2163Class definitions place yet another name space in the local scope.
2164
2165It is important to realize that scopes are determined textually: the
2166global scope of a function defined in a module is that module's name
2167space, no matter from where or by what alias the function is called.
2168On the other hand, the actual search for names is done dynamically, at
Guido van Rossum96628a91995-04-10 11:34:00 +00002169run time --- however, the language definition is evolving towards
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002170static name resolution, at ``compile'' time, so don't rely on dynamic
2171name resolution! (In fact, local variables are already determined
2172statically.)
2173
2174A special quirk of Python is that assignments always go into the
2175innermost scope. Assignments do not copy data --- they just
2176bind names to objects. The same is true for deletions: the statement
2177\verb\del x\ removes the binding of x from the name space referenced by the
2178local scope. In fact, all operations that introduce new names use the
2179local scope: in particular, import statements and function definitions
2180bind the module or function name in the local scope. (The
2181\verb\global\ statement can be used to indicate that particular
2182variables live in the global scope.)
2183
2184
2185\section{A first look at classes}
2186
2187Classes introduce a little bit of new syntax, three new object types,
2188and some new semantics.
2189
2190
2191\subsection{Class definition syntax}
2192
2193The simplest form of class definition looks like this:
2194
2195\begin{verbatim}
2196 class ClassName:
2197 <statement-1>
2198 .
2199 .
2200 .
2201 <statement-N>
2202\end{verbatim}
2203
2204Class definitions, like function definitions (\verb\def\ statements)
2205must be executed before they have any effect. (You could conceivably
2206place a class definition in a branch of an \verb\if\ statement, or
2207inside a function.)
2208
2209In practice, the statements inside a class definition will usually be
2210function definitions, but other statements are allowed, and sometimes
2211useful --- we'll come back to this later. The function definitions
2212inside a class normally have a peculiar form of argument list,
2213dictated by the calling conventions for methods --- again, this is
2214explained later.
2215
2216When a class definition is entered, a new name space is created, and
2217used as the local scope --- thus, all assignments to local variables
2218go into this new name space. In particular, function definitions bind
2219the name of the new function here.
2220
2221When a class definition is left normally (via the end), a {\em class
2222object} is created. This is basically a wrapper around the contents
2223of the name space created by the class definition; we'll learn more
2224about class objects in the next section. The original local scope
2225(the one in effect just before the class definitions was entered) is
2226reinstated, and the class object is bound here to class name given in
2227the class definition header (ClassName in the example).
2228
2229
2230\subsection{Class objects}
2231
2232Class objects support two kinds of operations: attribute references
2233and instantiation.
2234
2235{\em Attribute references} use the standard syntax used for all
2236attribute references in Python: \verb\obj.name\. Valid attribute
2237names are all the names that were in the class's name space when the
2238class object was created. So, if the class definition looked like
2239this:
2240
2241\begin{verbatim}
2242 class MyClass:
2243 i = 12345
2244 def f(x):
2245 return 'hello world'
2246\end{verbatim}
2247
2248then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
2249references, returning an integer and a function object, respectively.
2250Class attributes can also be assigned to, so you can change the
2251value of \verb\MyClass.i\ by assignment.
2252
2253Class {\em instantiation} uses function notation. Just pretend that
2254the class object is a parameterless function that returns a new
2255instance of the class. For example, (assuming the above class):
2256
2257\begin{verbatim}
2258 x = MyClass()
2259\end{verbatim}
2260
2261creates a new {\em instance} of the class and assigns this object to
2262the local variable \verb\x\.
2263
2264
2265\subsection{Instance objects}
2266
2267Now what can we do with instance objects? The only operations
2268understood by instance objects are attribute references. There are
2269two kinds of valid attribute names.
2270
2271The first I'll call {\em data attributes}. These correspond to
Guido van Rossum16d6e711994-08-08 12:30:22 +00002272``instance variables'' in Smalltalk, and to ``data members'' in \Cpp{}.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002273Data attributes need not be declared; like local variables, they
2274spring into existence when they are first assigned to. For example,
2275if \verb\x\ in the instance of \verb\MyClass\ created above, the
2276following piece of code will print the value 16, without leaving a
2277trace:
2278
2279\begin{verbatim}
2280 x.counter = 1
2281 while x.counter < 10:
2282 x.counter = x.counter * 2
2283 print x.counter
2284 del x.counter
2285\end{verbatim}
2286
2287The second kind of attribute references understood by instance objects
2288are {\em methods}. A method is a function that ``belongs to'' an
2289object. (In Python, the term method is not unique to class instances:
2290other object types can have methods as well, e.g., list objects have
2291methods called append, insert, remove, sort, and so on. However,
2292below, we'll use the term method exclusively to mean methods of class
2293instance objects, unless explicitly stated otherwise.)
2294
2295Valid method names of an instance object depend on its class. By
2296definition, all attributes of a class that are (user-defined) function
2297objects define corresponding methods of its instances. So in our
2298example, \verb\x.f\ is a valid method reference, since
2299\verb\MyClass.f\ is a function, but \verb\x.i\ is not, since
2300\verb\MyClass.i\ is not. But \verb\x.f\ is not the
2301same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a
2302function object.
2303
2304
2305\subsection{Method objects}
2306
2307Usually, a method is called immediately, e.g.:
2308
2309\begin{verbatim}
2310 x.f()
2311\end{verbatim}
2312
2313In our example, this will return the string \verb\'hello world'\.
2314However, it is not necessary to call a method right away: \verb\x.f\
2315is a method object, and can be stored away and called at a later
2316moment, for example:
2317
2318\begin{verbatim}
2319 xf = x.f
2320 while 1:
2321 print xf()
2322\end{verbatim}
2323
2324will continue to print \verb\hello world\ until the end of time.
2325
2326What exactly happens when a method is called? You may have noticed
2327that \verb\x.f()\ was called without an argument above, even though
2328the function definition for \verb\f\ specified an argument. What
2329happened to the argument? Surely Python raises an exception when a
2330function that requires an argument is called without any --- even if
2331the argument isn't actually used...
2332
2333Actually, you may have guessed the answer: the special thing about
2334methods is that the object is passed as the first argument of the
2335function. In our example, the call \verb\x.f()\ is exactly equivalent
2336to \verb\MyClass.f(x)\. In general, calling a method with a list of
2337{\em n} arguments is equivalent to calling the corresponding function
2338with an argument list that is created by inserting the method's object
2339before the first argument.
2340
2341If you still don't understand how methods work, a look at the
2342implementation can perhaps clarify matters. When an instance
2343attribute is referenced that isn't a data attribute, its class is
2344searched. If the name denotes a valid class attribute that is a
2345function object, a method object is created by packing (pointers to)
2346the instance object and the function object just found together in an
2347abstract object: this is the method object. When the method object is
2348called with an argument list, it is unpacked again, a new argument
2349list is constructed from the instance object and the original argument
2350list, and the function object is called with this new argument list.
2351
2352
2353\section{Random remarks}
2354
2355
2356[These should perhaps be placed more carefully...]
2357
2358
2359Data attributes override method attributes with the same name; to
2360avoid accidental name conflicts, which may cause hard-to-find bugs in
2361large programs, it is wise to use some kind of convention that
2362minimizes the chance of conflicts, e.g., capitalize method names,
2363prefix data attribute names with a small unique string (perhaps just
Guido van Rossum6938f061994-08-01 12:22:53 +00002364an underscore), or use verbs for methods and nouns for data attributes.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002365
2366
2367Data attributes may be referenced by methods as well as by ordinary
2368users (``clients'') of an object. In other words, classes are not
2369usable to implement pure abstract data types. In fact, nothing in
2370Python makes it possible to enforce data hiding --- it is all based
2371upon convention. (On the other hand, the Python implementation,
2372written in C, can completely hide implementation details and control
2373access to an object if necessary; this can be used by extensions to
2374Python written in C.)
2375
2376
2377Clients should use data attributes with care --- clients may mess up
2378invariants maintained by the methods by stamping on their data
2379attributes. Note that clients may add data attributes of their own to
2380an instance object without affecting the validity of the methods, as
2381long as name conflicts are avoided --- again, a naming convention can
2382save a lot of headaches here.
2383
2384
2385There is no shorthand for referencing data attributes (or other
2386methods!) from within methods. I find that this actually increases
2387the readability of methods: there is no chance of confusing local
2388variables and instance variables when glancing through a method.
2389
2390
2391Conventionally, the first argument of methods is often called
2392\verb\self\. This is nothing more than a convention: the name
2393\verb\self\ has absolutely no special meaning to Python. (Note,
2394however, that by not following the convention your code may be less
2395readable by other Python programmers, and it is also conceivable that
2396a {\em class browser} program be written which relies upon such a
2397convention.)
2398
2399
2400Any function object that is a class attribute defines a method for
2401instances of that class. It is not necessary that the function
2402definition is textually enclosed in the class definition: assigning a
2403function object to a local variable in the class is also ok. For
2404example:
2405
2406\begin{verbatim}
2407 # Function defined outside the class
2408 def f1(self, x, y):
2409 return min(x, x+y)
2410
2411 class C:
2412 f = f1
2413 def g(self):
2414 return 'hello world'
2415 h = g
2416\end{verbatim}
2417
2418Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
2419\verb\C\ that refer to function objects, and consequently they are all
2420methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
2421to \verb\g\. Note that this practice usually only serves to confuse
2422the reader of a program.
2423
2424
2425Methods may call other methods by using method attributes of the
2426\verb\self\ argument, e.g.:
2427
2428\begin{verbatim}
2429 class Bag:
2430 def empty(self):
2431 self.data = []
2432 def add(self, x):
2433 self.data.append(x)
2434 def addtwice(self, x):
Guido van Rossum084b0b21992-08-14 09:19:56 +00002435 self.add(x)
2436 self.add(x)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002437\end{verbatim}
2438
2439
2440The instantiation operation (``calling'' a class object) creates an
2441empty object. Many classes like to create objects in a known initial
Guido van Rossumca3f6c81994-10-06 14:08:53 +00002442state. Therefore a class may define a special method named
2443\verb\__init__\, like this:
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002444
Guido van Rossum6938f061994-08-01 12:22:53 +00002445\begin{verbatim}
2446 def __init__(self):
2447 self.empty()
2448\end{verbatim}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002449
Guido van Rossum6938f061994-08-01 12:22:53 +00002450When a class defines an \verb\__init__\ method, class instantiation
2451automatically invokes \verb\__init__\ for the newly-created class
2452instance. So in the \verb\Bag\ example, a new and initialized instance
2453can be obtained by:
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002454
Guido van Rossum6938f061994-08-01 12:22:53 +00002455\begin{verbatim}
2456 x = Bag()
2457\end{verbatim}
2458
2459Of course, the \verb\__init__\ method may have arguments for greater
2460flexibility. In that case, arguments given to the class instantiation
2461operator are passed on to \verb\__init__\. For example,
2462
2463\bcode\begin{verbatim}
2464>>> class Complex:
2465... def __init__(self, realpart, imagpart):
2466... self.r = realpart
2467... self.i = imagpart
2468...
2469>>> x = Complex(3.0,-4.5)
2470>>> x.r, x.i
2471(3.0, -4.5)
2472>>>
2473\end{verbatim}\ecode
2474%
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002475Methods may reference global names in the same way as ordinary
2476functions. The global scope associated with a method is the module
2477containing the class definition. (The class itself is never used as a
2478global scope!) While one rarely encounters a good reason for using
2479global data in a method, there are many legitimate uses of the global
2480scope: for one thing, functions and modules imported into the global
2481scope can be used by methods, as well as functions and classes defined
2482in it. Usually, the class containing the method is itself defined in
2483this global scope, and in the next section we'll find some good
2484reasons why a method would want to reference its own class!
2485
2486
2487\section{Inheritance}
2488
2489Of course, a language feature would not be worthy of the name ``class''
2490without supporting inheritance. The syntax for a derived class
2491definition looks as follows:
2492
2493\begin{verbatim}
2494 class DerivedClassName(BaseClassName):
2495 <statement-1>
2496 .
2497 .
2498 .
2499 <statement-N>
2500\end{verbatim}
2501
2502The name \verb\BaseClassName\ must be defined in a scope containing
2503the derived class definition. Instead of a base class name, an
2504expression is also allowed. This is useful when the base class is
2505defined in another module, e.g.,
2506
2507\begin{verbatim}
2508 class DerivedClassName(modname.BaseClassName):
2509\end{verbatim}
2510
2511Execution of a derived class definition proceeds the same as for a
2512base class. When the class object is constructed, the base class is
2513remembered. This is used for resolving attribute references: if a
2514requested attribute is not found in the class, it is searched in the
2515base class. This rule is applied recursively if the base class itself
2516is derived from some other class.
2517
2518There's nothing special about instantiation of derived classes:
2519\verb\DerivedClassName()\ creates a new instance of the class. Method
2520references are resolved as follows: the corresponding class attribute
2521is searched, descending down the chain of base classes if necessary,
2522and the method reference is valid if this yields a function object.
2523
2524Derived classes may override methods of their base classes. Because
2525methods have no special privileges when calling other methods of the
2526same object, a method of a base class that calls another method
2527defined in the same base class, may in fact end up calling a method of
Guido van Rossum16d6e711994-08-08 12:30:22 +00002528a derived class that overrides it. (For \Cpp{} programmers: all methods
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002529in Python are ``virtual functions''.)
2530
2531An overriding method in a derived class may in fact want to extend
2532rather than simply replace the base class method of the same name.
2533There is a simple way to call the base class method directly: just
2534call \verb\BaseClassName.methodname(self, arguments)\. This is
2535occasionally useful to clients as well. (Note that this only works if
2536the base class is defined or imported directly in the global scope.)
2537
2538
2539\subsection{Multiple inheritance}
2540
Guido van Rossum6938f061994-08-01 12:22:53 +00002541Python supports a limited form of multiple inheritance as well. A
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002542class definition with multiple base classes looks as follows:
2543
2544\begin{verbatim}
2545 class DerivedClassName(Base1, Base2, Base3):
2546 <statement-1>
2547 .
2548 .
2549 .
2550 <statement-N>
2551\end{verbatim}
2552
2553The only rule necessary to explain the semantics is the resolution
2554rule used for class attribute references. This is depth-first,
2555left-to-right. Thus, if an attribute is not found in
2556\verb\DerivedClassName\, it is searched in \verb\Base1\, then
2557(recursively) in the base classes of \verb\Base1\, and only if it is
2558not found there, it is searched in \verb\Base2\, and so on.
2559
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002560(To some people breadth first---searching \verb\Base2\ and
2561\verb\Base3\ before the base classes of \verb\Base1\---looks more
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002562natural. However, this would require you to know whether a particular
2563attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
2564one of its base classes before you can figure out the consequences of
2565a name conflict with an attribute of \verb\Base2\. The depth-first
2566rule makes no differences between direct and inherited attributes of
2567\verb\Base1\.)
2568
2569It is clear that indiscriminate use of multiple inheritance is a
2570maintenance nightmare, given the reliance in Python on conventions to
2571avoid accidental name conflicts. A well-known problem with multiple
2572inheritance is a class derived from two classes that happen to have a
2573common base class. While it is easy enough to figure out what happens
2574in this case (the instance will have a single copy of ``instance
2575variables'' or data attributes used by the common base class), it is
2576not clear that these semantics are in any way useful.
2577
2578
2579\section{Odds and ends}
2580
2581Sometimes it is useful to have a data type similar to the Pascal
2582``record'' or C ``struct'', bundling together a couple of named data
2583items. An empty class definition will do nicely, e.g.:
2584
2585\begin{verbatim}
2586 class Employee:
2587 pass
2588
2589 john = Employee() # Create an empty employee record
2590
2591 # Fill the fields of the record
2592 john.name = 'John Doe'
2593 john.dept = 'computer lab'
2594 john.salary = 1000
2595\end{verbatim}
2596
2597
2598A piece of Python code that expects a particular abstract data type
2599can often be passed a class that emulates the methods of that data
2600type instead. For instance, if you have a function that formats some
2601data from a file object, you can define a class with methods
2602\verb\read()\ and \verb\readline()\ that gets the data from a string
2603buffer instead, and pass it as an argument. (Unfortunately, this
2604technique has its limitations: a class can't define operations that
2605are accessed by special syntax such as sequence subscripting or
2606arithmetic operators, and assigning such a ``pseudo-file'' to
2607\verb\sys.stdin\ will not cause the interpreter to read further input
2608from it.)
2609
2610
2611Instance method objects have attributes, too: \verb\m.im_self\ is the
2612object of which the method is an instance, and \verb\m.im_func\ is the
2613function object corresponding to the method.
2614
2615
Guido van Rossum6938f061994-08-01 12:22:53 +00002616\chapter{Recent Additions}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002617
Guido van Rossum6938f061994-08-01 12:22:53 +00002618Python is an evolving language. Since this tutorial was last
2619thoroughly revised, several new features have been added to the
2620language. While ideally I should revise the tutorial to incorporate
2621them in the mainline of the text, lack of time currently requires me
Guido van Rossum16cd7f91994-10-06 10:29:26 +00002622to take a more modest approach. In this chapter I will briefly list the
Guido van Rossum6938f061994-08-01 12:22:53 +00002623most important improvements to the language and how you can use them
2624to your benefit.
2625
2626\section{The Last Printed Expression}
2627
2628In interactive mode, the last printed expression is assigned to the
Guido van Rossum194e57c1995-02-15 15:51:38 +00002629variable \code{_}. This means that when you are using Python as a
Guido van Rossum6938f061994-08-01 12:22:53 +00002630desk calculator, it is somewhat easier to continue calculations, for
2631example:
2632
2633\begin{verbatim}
2634 >>> tax = 17.5 / 100
2635 >>> price = 3.50
2636 >>> price * tax
2637 0.6125
2638 >>> price + _
2639 4.1125
2640 >>> round(_, 2)
2641 4.11
2642 >>>
2643\end{verbatim}
2644
Guido van Rossum194e57c1995-02-15 15:51:38 +00002645For reasons too embarrassing to explain, this variable is implemented
2646as a built-in (living in the module \code{__builtin__}), so it should
2647be treated as read-only by the user. I.e. don't explicitly assign a
2648value to it --- you would create an independent local variable with
2649the same name masking the built-in variable with its magic behavior.
2650
Guido van Rossum6938f061994-08-01 12:22:53 +00002651\section{String Literals}
2652
2653\subsection{Double Quotes}
2654
2655Python can now also use double quotes to surround string literals,
Guido van Rossum194e57c1995-02-15 15:51:38 +00002656e.g. \verb\"this doesn't hurt a bit"\. There is no semantic
2657difference between strings surrounded by single or double quotes.
Guido van Rossum6938f061994-08-01 12:22:53 +00002658
2659\subsection{Continuation Of String Literals}
2660
2661String literals can span multiple lines by escaping newlines with
2662backslashes, e.g.
2663
2664\begin{verbatim}
2665 hello = "This is a rather long string containing\n\
2666 several lines of text just as you would do in C.\n\
2667 Note that whitespace at the beginning of the line is\
2668 significant.\n"
2669 print hello
2670\end{verbatim}
2671
2672which would print the following:
2673\begin{verbatim}
2674 This is a rather long string containing
2675 several lines of text just as you would do in C.
2676 Note that whitespace at the beginning of the line is significant.
2677\end{verbatim}
2678
2679\subsection{Triple-quoted strings}
2680
2681In some cases, when you need to include really long strings (e.g.
2682containing several paragraphs of informational text), it is annoying
2683that you have to terminate each line with \verb@\n\@, especially if
2684you would like to reformat the text occasionally with a powerful text
2685editor like Emacs. For such situations, ``triple-quoted'' strings can
2686be used, e.g.
2687
2688\begin{verbatim}
2689 hello = """
2690
2691 This string is bounded by triple double quotes (3 times ").
Guido van Rossum194e57c1995-02-15 15:51:38 +00002692 Unescaped newlines in the string are retained, though \
Guido van Rossum6938f061994-08-01 12:22:53 +00002693 it is still possible\nto use all normal escape sequences.
2694
2695 Whitespace at the beginning of a line is
2696 significant. If you need to include three opening quotes
2697 you have to escape at least one of them, e.g. \""".
2698
2699 This string ends in a newline.
2700 """
2701\end{verbatim}
2702
Guido van Rossum194e57c1995-02-15 15:51:38 +00002703Triple-quoted strings can be surrounded by three single quotes as
2704well, again without semantic difference.
Guido van Rossum6938f061994-08-01 12:22:53 +00002705
2706\subsection{String Literal Juxtaposition}
2707
2708One final twist: you can juxtapose multiple string literals. Two or
2709more adjacent string literals (but not arbitrary expressions!)
2710separated only by whitespace will be concatenated (without intervening
2711whitespace) into a single string object at compile time. This makes
2712it possible to continue a long string on the next line without
2713sacrificing indentation or performance, unlike the use of the string
2714concatenation operator \verb\+\ or the continuation of the literal
2715itself on the next line (since leading whitespace is significant
2716inside all types of string literals). Note that this feature, like
2717all string features except triple-quoted strings, is borrowed from
2718Standard C.
2719
2720\section{The Formatting Operator}
2721
2722\subsection{Basic Usage}
2723
2724The chapter on output formatting is really out of date: there is now
2725an almost complete interface to C-style printf formats. This is done
2726by overloading the modulo operator (\verb\%\) for a left operand
2727which is a string, e.g.
2728
2729\begin{verbatim}
2730 >>> import math
2731 >>> print 'The value of PI is approximately %5.3f.' % math.pi
2732 The value of PI is approximately 3.142.
2733 >>>
2734\end{verbatim}
2735
2736If there is more than one format in the string you pass a tuple as
2737right operand, e.g.
2738
2739\begin{verbatim}
2740 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
2741 >>> for name, phone in table.items():
2742 ... print '%-10s ==> %10d' % (name, phone)
2743 ...
2744 Jack ==> 4098
2745 Dcab ==> 8637678
2746 Sjoerd ==> 4127
2747 >>>
2748\end{verbatim}
2749
2750Most formats work exactly as in C and require that you pass the proper
2751type (however, if you don't you get an exception, not a core dump).
2752The \verb\%s\ format is more relaxed: if the corresponding argument is
2753not a string object, it is converted to string using the \verb\str()\
2754built-in function. Using \verb\*\ to pass the width or precision in
2755as a separate (integer) argument is supported. The C formats
2756\verb\%n\ and \verb\%p\ are not supported.
2757
2758\subsection{Referencing Variables By Name}
2759
2760If you have a really long format string that you don't want to split
2761up, it would be nice if you could reference the variables to be
2762formatted by name instead of by position. This can be done by using
2763an extension of C formats using the form \verb\%(name)format\, e.g.
2764
2765\begin{verbatim}
2766 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
2767 >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table
2768 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
2769 >>>
2770\end{verbatim}
2771
2772This is particularly useful in combination with the new built-in
2773\verb\vars()\ function, which returns a dictionary containing all
2774local variables.
2775
2776\section{Optional Function Arguments}
2777
2778It is now possible to define functions with a variable number of
2779arguments. There are two forms, which can be combined.
2780
2781\subsection{Default Argument Values}
2782
2783The most useful form is to specify a default value for one or more
2784arguments. This creates a function that can be called with fewer
2785arguments than it is defined, e.g.
2786
2787\begin{verbatim}
2788 def ask_ok(prompt, retries = 4, complaint = 'Yes or no, please!'):
2789 while 1:
2790 ok = raw_input(prompt)
2791 if ok in ('y', 'ye', 'yes'): return 1
2792 if ok in ('n', 'no', 'nop', 'nope'): return 0
2793 retries = retries - 1
2794 if retries < 0: raise IOError, 'refusenik user'
2795 print complaint
2796\end{verbatim}
2797
2798This function can be called either like this:
2799\verb\ask_ok('Do you really want to quit?')\ or like this:
2800\verb\ask_ok('OK to overwrite the file?', 2)\.
2801
2802The default values are evaluated at the point of function definition
2803in the {\em defining} scope, so that e.g.
2804
2805\begin{verbatim}
2806 i = 5
2807 def f(arg = i): print arg
2808 i = 6
2809 f()
2810\end{verbatim}
2811
2812will print \verb\5\.
2813
2814\subsection{Arbitrary Argument Lists}
2815
2816It is also possible to specify that a function can be called with an
2817arbitrary number of arguments. These arguments will be wrapped up in
2818a tuple. Before the variable number of arguments, zero or more normal
2819arguments may occur, e.g.
2820
2821\begin{verbatim}
2822 def fprintf(file, format, *args):
2823 file.write(format % args)
2824\end{verbatim}
2825
2826This feature may be combined with the previous, e.g.
2827
2828\begin{verbatim}
2829 def but_is_it_useful(required, optional = None, *remains):
2830 print "I don't know"
2831\end{verbatim}
2832
2833\section{Lambda And Functional Programming Tools}
2834
2835\subsection{Lambda Forms}
2836
Guido van Rossum16d6e711994-08-08 12:30:22 +00002837By popular demand, a few features commonly found in functional
Guido van Rossum6938f061994-08-01 12:22:53 +00002838programming languages and Lisp have been added to Python. With the
2839\verb\lambda\ keyword, small anonymous functions can be created.
2840Here's a function that returns the sum of its two arguments:
2841\verb\lambda a, b: a+b\. Lambda forms can be used wherever function
2842objects are required. They are syntactically restricted to a single
2843expression. Semantically, they are just syntactic sugar for a normal
2844function definition. Like nested function definitions, lambda forms
2845cannot reference variables from the containing scope, but this can be
2846overcome through the judicious use of default argument values, e.g.
2847
2848\begin{verbatim}
2849 def make_incrementor(n):
Guido van Rossumc1be9d51994-08-30 12:08:58 +00002850 return lambda x, incr=n: x+incr
Guido van Rossum6938f061994-08-01 12:22:53 +00002851\end{verbatim}
2852
2853\subsection{Map, Reduce and Filter}
2854
2855Three new built-in functions on sequences are good candidate to pass
2856lambda forms.
2857
2858\subsubsection{Map.}
2859
2860\verb\map(function, sequence)\ calls \verb\function(item)\ for each of
2861the sequence's items and returns a list of the return values. For
2862example, to compute some cubes:
2863
2864\begin{verbatim}
2865 >>> map(lambda x: x*x*x, range(1, 11))
2866 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
2867 >>>
2868\end{verbatim}
2869
2870More than one sequence may be passed; the function must then have as
2871many arguments as there are sequences and is called with the
2872corresponding item from each sequence (or \verb\None\ if some sequence
2873is shorter than another). If \verb\None\ is passed for the function,
2874a function returning its argument(s) is substituted.
2875
2876Combining these two special cases, we see that
2877\verb\map(None, list1, list2)\ is a convenient way of turning a pair
2878of lists into a list of pairs. For example:
2879
2880\begin{verbatim}
2881 >>> seq = range(8)
2882 >>> map(None, seq, map(lambda x: x*x, seq))
2883 [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
2884 >>>
2885\end{verbatim}
2886
2887\subsubsection{Filter.}
2888
2889\verb\filter(function, sequence)\ returns a sequence (of the same
2890type, if possible) consisting of those items from the sequence for
2891which \verb\function(item)\ is true. For example, to compute some
2892primes:
2893
2894\begin{verbatim}
2895 >>> filter(lambda x: x%2 != 0 and x%3 != 0, range(2, 25))
2896 [5, 7, 11, 13, 17, 19, 23]
2897 >>>
2898\end{verbatim}
2899
2900\subsubsection{Reduce.}
2901
2902\verb\reduce(function, sequence)\ returns a single value constructed
2903by calling the (binary) function on the first two items of the
2904sequence, then on the result and the next item, and so on. For
2905example, to compute the sum of the numbers 1 through 10:
2906
2907\begin{verbatim}
2908 >>> reduce(lambda x, y: x+y, range(1, 11))
2909 55
2910 >>>
2911\end{verbatim}
2912
2913If there's only one item in the sequence, its value is returned; if
2914the sequence is empty, an exception is raised.
2915
2916A third argument can be passed to indicate the starting value. In this
2917case the starting value is returned for an empty sequence, and the
2918function is first applied to the starting value and the first sequence
2919item, then to the result and the next item, and so on. For example,
2920
2921\begin{verbatim}
2922 >>> def sum(seq):
2923 ... return reduce(lambda x, y: x+y, seq, 0)
2924 ...
2925 >>> sum(range(1, 11))
2926 55
2927 >>> sum([])
2928 0
2929 >>>
2930\end{verbatim}
2931
2932\section{Continuation Lines Without Backslashes}
2933
2934While the general mechanism for continuation of a source line on the
2935next physical line remains to place a backslash on the end of the
2936line, expressions inside matched parentheses (or square brackets, or
2937curly braces) can now also be continued without using a backslash.
2938This is particularly useful for calls to functions with many
2939arguments, and for initializations of large tables.
2940
2941For example:
2942
2943\begin{verbatim}
2944 month_names = ['Januari', 'Februari', 'Maart',
2945 'April', 'Mei', 'Juni',
2946 'Juli', 'Augustus', 'September',
2947 'Oktober', 'November', 'December']
2948\end{verbatim}
2949
2950and
2951
2952\begin{verbatim}
2953 CopyInternalHyperLinks(self.context.hyperlinks,
2954 copy.context.hyperlinks,
2955 uidremap)
2956\end{verbatim}
2957
2958\section{Regular Expressions}
2959
2960While C's printf-style output formats, transformed into Python, are
2961adequate for most output formatting jobs, C's scanf-style input
2962formats are not very powerful. Instead of scanf-style input, Python
2963offers Emacs-style regular expressions as a powerful input and
2964scanning mechanism. Read the corresponding section in the Library
2965Reference for a full description.
2966
2967\section{Generalized Dictionaries}
2968
Guido van Rossum86751151995-02-28 17:14:32 +00002969The keys of dictionaries are no longer restricted to strings --- they
Guido van Rossum194e57c1995-02-15 15:51:38 +00002970can be any immutable basic type including strings, numbers, tuples, or
2971(certain) class instances. (Lists and dictionaries are not acceptable
2972as dictionary keys, in order to avoid problems when the object used as
2973a key is modified.)
Guido van Rossum6938f061994-08-01 12:22:53 +00002974
2975Dictionaries have two new methods: \verb\d.values()\ returns a list of
2976the dictionary's values, and \verb\d.items()\ returns a list of the
2977dictionary's (key, value) pairs. Like \verb\d.keys()\, these
2978operations are slow for large dictionaries. Examples:
2979
2980\begin{verbatim}
2981 >>> d = {100: 'honderd', 1000: 'duizend', 10: 'tien'}
2982 >>> d.keys()
2983 [100, 10, 1000]
2984 >>> d.values()
2985 ['honderd', 'tien', 'duizend']
2986 >>> d.items()
2987 [(100, 'honderd'), (10, 'tien'), (1000, 'duizend')]
2988 >>>
2989\end{verbatim}
2990
2991\section{Miscellaneous New Built-in Functions}
2992
2993The function \verb\vars()\ returns a dictionary containing the current
Guido van Rossum16cd7f91994-10-06 10:29:26 +00002994local variables. With a module argument, it returns that module's
Guido van Rossum6938f061994-08-01 12:22:53 +00002995global variables. The old function \verb\dir(x)\ returns
2996\verb\vars(x).keys()\.
2997
2998The function \verb\round(x)\ returns a floating point number rounded
2999to the nearest integer (but still expressed as a floating point
3000number). E.g. \verb\round(3.4) == 3.0\ and \verb\round(3.5) == 4.0\.
3001With a second argument it rounds to the specified number of digits,
3002e.g. \verb\round(math.pi, 4) == 3.1416\ or even
3003\verb\round(123.4, -2) == 100.0\.
3004
3005The function \verb\hash(x)\ returns a hash value for an object.
3006All object types acceptable as dictionary keys have a hash value (and
3007it is this hash value that the dictionary implementation uses).
3008
3009The function \verb\id(x)\ return a unique identifier for an object.
3010For two objects x and y, \verb\id(x) == id(y)\ if and only if
3011\verb\x is y\. (In fact the object's address is used.)
3012
3013The function \verb\hasattr(x, name)\ returns whether an object has an
3014attribute with the given name (a string value). The function
3015\verb\getattr(x, name)\ returns the object's attribute with the given
3016name. The function \verb\setattr(x, name, value)\ assigns a value to
3017an object's attribute with the given name. These three functions are
3018useful if the attribute names are not known beforehand. Note that
Guido van Rossume5f8b601995-01-04 19:12:49 +00003019\verb\getattr(x, 'spam')\ is equivalent to \verb\x.spam\, and
3020\verb\setattr(x, 'spam', y)\ is equivalent to \verb\x.spam = y\. By
Guido van Rossum6938f061994-08-01 12:22:53 +00003021definition, \verb\hasattr(x, name)\ returns true if and only if
3022\verb\getattr(x, name)\ returns without raising an exception.
3023
3024\section{Else Clause For Try Statement}
3025
3026The \verb\try...except\ statement now has an optional \verb\else\
3027clause, which must follow all \verb\except\ clauses. It is useful to
3028place code that must be executed if the \verb\try\ clause does not
3029raise an exception. For example:
3030
3031\begin{verbatim}
3032 for arg in sys.argv:
3033 try:
3034 f = open(arg, 'r')
3035 except IOError:
3036 print 'cannot open', arg
3037 else:
3038 print arg, 'has', len(f.readlines()), 'lines'
3039 f.close()
3040\end{verbatim}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003041
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003042
3043\section{New Class Features in Release 1.1}
3044
Guido van Rossumcfb45e41994-11-10 23:04:43 +00003045Some changes have been made to classes: the operator overloading
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003046mechanism is more flexible, providing more support for non-numeric use
Guido van Rossum29766b21994-10-06 15:33:25 +00003047of operators (including calling an object as if it were a function),
3048and it is possible to trap attribute accesses.
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003049
3050\subsection{New Operator Overloading}
3051
3052It is no longer necessary to coerce both sides of an operator to the
3053same class or type. A class may still provide a \code{__coerce__}
3054method, but this method may return objects of different types or
3055classes if it feels like it. If no \code{__coerce__} is defined, any
3056argument type or class is acceptable.
3057
3058In order to make it possible to implement binary operators where the
3059right-hand side is a class instance but the left-hand side is not,
3060without using coercions, right-hand versions of all binary operators
3061may be defined. These have an `r' prepended to their name,
3062e.g. \code{__radd__}.
3063
3064For example, here's a very simple class for representing times. Times
3065are initialized from a number of seconds (like time.time()). Times
Guido van Rossum1133aec1995-03-15 11:34:18 +00003066are printed like this: \code{Wed Mar 15 12:28:48 1995}. Subtracting
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003067two Times gives their difference in seconds. Adding or subtracting a
3068Time and a number gives a new Time. You can't add two times, nor can
3069you subtract a Time from a number.
3070
3071\begin{verbatim}
3072import time
3073
3074class Time:
3075 def __init__(self, seconds):
3076 self.seconds = seconds
3077 def __repr__(self):
3078 return time.ctime(self.seconds)
3079 def __add__(self, x):
3080 return Time(self.seconds + x)
3081 __radd__ = __add__ # support for x+t
3082 def __sub__(self, x):
3083 if hasattr(x, 'seconds'): # test if x could be a Time
3084 return self.seconds - x.seconds
3085 else:
3086 return self.seconds - x
3087
3088now = Time(time.time())
3089tomorrow = 24*3600 + now
3090yesterday = now - today
3091print tomorrow - yesterday # prints 172800
3092\end{verbatim}
3093
3094\subsection{Trapping Attribute Access}
3095
3096You can define three new ``magic'' methods in a class now:
3097\code{__getattr__(self, name)}, \code{__setattr__(self, name, value)}
3098and \code{__delattr__(self, name)}.
3099
3100The \code{__getattr__} method is called when an attribute access fails,
Guido van Rossum86751151995-02-28 17:14:32 +00003101i.e. when an attribute access would otherwise raise AttributeError ---
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003102this is {\em after} the instance's dictionary and its class hierarchy
3103have been searched for the named attribute. Note that if this method
3104attempts to access any undefined instance attribute it will be called
3105recursively!
3106
3107The \code{__setattr__} and \code{__delattr__} methods are called when
3108assignment to, respectively deletion of an attribute are attempted.
3109They are called {\em instead} of the normal action (which is to insert
3110or delete the attribute in the instance dictionary). If either of
3111these methods most set or delete any attribute, they can only do so by
Guido van Rossum86751151995-02-28 17:14:32 +00003112using the instance dictionary directly --- \code{self.__dict__} --- else
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003113they would be called recursively.
3114
3115For example, here's a near-universal ``Wrapper'' class that passes all
3116its attribute accesses to another object. Note how the
3117\code{__init__} method inserts the wrapped object in
3118\code{self.__dict__} in order to avoid endless recursion
3119(\code{__setattr__} would call \code{__getattr__} which would call
3120itself recursively).
3121
3122\begin{verbatim}
3123class Wrapper:
3124 def __init__(self, wrapped):
3125 self.__dict__['wrapped'] = wrapped
3126 def __getattr__(self, name):
3127 return getattr(self.wrapped, name)
3128 def __setattr__(self, name, value):
Guido van Rossumcfb45e41994-11-10 23:04:43 +00003129 setattr(self.wrapped, name, value)
Guido van Rossumca3f6c81994-10-06 14:08:53 +00003130 def __delattr__(self, name):
3131 delattr(self.wrapped, name)
3132
3133import sys
3134f = Wrapper(sys.stdout)
3135f.write('hello world\n') # prints 'hello world'
3136\end{verbatim}
3137
Guido van Rossum29766b21994-10-06 15:33:25 +00003138A simpler example of \code{__getattr__} is an attribute that is
3139computed each time (or the first time) it it accessed. For instance:
3140
3141\begin{verbatim}
3142from math import pi
3143
3144class Circle:
3145 def __init__(self, radius):
3146 self.radius = radius
3147 def __getattr__(self, name):
3148 if name == 'circumference':
3149 return 2 * pi * self.radius
3150 if name == 'diameter':
3151 return 2 * self.radius
3152 if name == 'area':
3153 return pi * pow(self.radius, 2)
3154 raise AttributeError, name
3155\end{verbatim}
3156
3157\subsection{Calling a Class Instance}
3158
3159If a class defines a method \code{__call__} it is possible to call its
3160instances as if they were functions. For example:
3161
3162\begin{verbatim}
3163class PresetSomeArguments:
3164 def __init__(self, func, *args):
3165 self.func, self.args = func, args
3166 def __call__(self, *args):
3167 return apply(self.func, self.args + args)
3168
3169f = PresetSomeArguments(pow, 2) # f(i) computes powers of 2
3170for i in range(10): print f(i), # prints 1 2 4 8 16 32 64 128 256 512
3171print # append newline
3172\end{verbatim}
3173
Guido van Rossum194e57c1995-02-15 15:51:38 +00003174
3175\chapter{New in Release 1.2}
3176
3177
3178This chapter describes even more recent additions to the Python
3179language and library.
3180
3181
3182\section{New Class Features}
3183
3184The semantics of \code{__coerce__} have been changed to be more
3185reasonable. As an example, the new standard module \code{Complex}
3186implements fairly complete complex numbers using this. Additional
3187examples of classes with and without \code{__coerce__} methods can be
3188found in the \code{Demo/classes} subdirectory, modules \code{Rat} and
3189\code{Dates}.
3190
3191If a class defines no \code{__coerce__} method, this is equivalent to
3192the following definition:
3193
3194\begin{verbatim}
3195def __coerce__(self, other): return self, other
3196\end{verbatim}
3197
3198If \code{__coerce__} coerces itself to an object of a different type,
3199the operation is carried out using that type --- in release 1.1, this
3200would cause an error.
3201
3202Comparisons involving class instances now invoke \code{__coerce__}
3203exactly as if \code{cmp(x, y)} were a binary operator like \code{+}
3204(except if \code{x} and \code{y} are the same object).
3205
3206\section{Unix Signal Handling}
3207
3208On Unix, Python now supports signal handling. The module
3209\code{signal} exports functions \code{signal}, \code{pause} and
3210\code{alarm}, which act similar to their Unix counterparts. The
3211module also exports the conventional names for the various signal
3212classes (also usable with \code{os.kill()}) and \code{SIG_IGN} and
3213\code{SIG_DFL}. See the section on \code{signal} in the Library
3214Reference Manual for more information.
3215
3216\section{Exceptions Can Be Classes}
3217
3218User-defined exceptions are no longer limited to being string objects
3219--- they can be identified by classes as well. Using this mechanism it
3220is possible to create extensible hierarchies of exceptions.
3221
3222There are two new valid (semantic) forms for the raise statement:
3223
3224\begin{verbatim}
3225raise Class, instance
3226
3227raise instance
3228\end{verbatim}
3229
3230In the first form, \code{instance} must be an instance of \code{Class}
3231or of a class derived from it. The second form is a shorthand for
3232
3233\begin{verbatim}
3234raise instance.__class__, instance
3235\end{verbatim}
3236
3237An except clause may list classes as well as string objects. A class
3238in an except clause is compatible with an exception if it is the same
3239class or a base class thereof (but not the other way around --- an
3240except clause listing a derived class is not compatible with a base
3241class). For example, the following code will print B, C, D in that
3242order:
3243
3244\begin{verbatim}
3245class B:
3246 pass
3247class C(B):
3248 pass
3249class D(C):
3250 pass
3251
3252for c in [B, C, D]:
3253 try:
3254 raise c()
3255 except D:
3256 print "D"
3257 except C:
3258 print "C"
3259 except B:
3260 print "B"
3261\end{verbatim}
3262
3263Note that if the except clauses were reversed (with ``\code{except B}''
3264first), it would have printed B, B, B --- the first matching except
3265clause is triggered.
3266
3267When an error message is printed for an unhandled exception which is a
3268class, the class name is printed, then a colon and a space, and
3269finally the instance converted to a string using the built-in function
3270\code{str()}.
3271
3272In this release, the built-in exceptions are still strings.
3273
3274
3275\section{Object Persistency and Object Copying}
3276
3277Two new modules, \code{pickle} and \code{shelve}, support storage and
3278retrieval of (almost) arbitrary Python objects on disk, using the
3279\code{dbm} package. A third module, \code{copy}, provides flexible
Guido van Rossumbcc95821995-02-16 16:28:48 +00003280object copying operations. More information on these modules is
3281provided in the Library Reference Manual.
Guido van Rossum194e57c1995-02-15 15:51:38 +00003282
3283\subsection{Persistent Objects}
3284
3285The module \code{pickle} provides a general framework for objects to
3286disassemble themselves into a stream of bytes and to reassemble such a
3287stream back into an object. It copes with reference sharing,
3288recursive objects and instances of user-defined classes, but not
3289(directly) with objects that have ``magical'' links into the operating
3290system such as open files, sockets or windows.
3291
3292The \code{pickle} module defines a simple protocol whereby
3293user-defined classes can control how they are disassembled and
3294assembled. The method \code{__getinitargs__()}, if defined, returns
3295the argument list for the constructor to be used at assembly time (by
3296default the constructor is called without arguments). The methods
3297\code{__getstate__()} and \code{__setstate__()} are used to pass
3298additional state from disassembly to assembly; by default the
3299instance's \code{__dict__} is passed and restored.
3300
3301Note that \code{pickle} does not open or close any files --- it can be
3302used equally well for moving objects around on a network or store them
3303in a database. For ease of debugging, and the inevitable occasional
3304manual patch-up, the constructed byte streams consist of printable
Guido van Rossum47b4c0f1995-03-15 11:25:32 +00003305\ASCII{} characters only (though it's not designed to be pretty).
Guido van Rossum194e57c1995-02-15 15:51:38 +00003306
3307The module \code{shelve} provides a simple model for storing objects
3308on files. The operation \code{shelve.open(filename)} returns a
3309``shelf'', which is a simple persistent database with a
3310dictionary-like interface. Database keys are strings, objects stored
3311in the database can be anything that \code{pickle} will handle.
3312
Guido van Rossum194e57c1995-02-15 15:51:38 +00003313\subsection{Copying Objects}
3314
3315The module \code{copy} exports two functions: \code{copy()} and
3316\code{deepcopy()}. The \code{copy()} function returns a ``shallow''
3317copy of an object; \code{deepcopy()} returns a ``deep'' copy. The
3318difference between shallow and deep copying is only relevant for
3319compound objects (objects that contain other objects, like lists or
3320class instances):
3321
3322\begin{itemize}
3323
3324\item
3325A shallow copy constructs a new compound object and then (to the
3326extent possible) inserts {\em the same objects} into in that the
3327original contains.
3328
3329\item
3330A deep copy constructs a new compound object and then, recursively,
3331inserts {\em copies} into it of the objects found in the original.
3332
3333\end{itemize}
3334
3335Both functions have the same restrictions and use the same protocols
3336as \code{pickle} --- user-defined classes can control how they are
3337copied by providing methods named \code{__getinitargs__()},
3338\code{__getstate__()} and \code{__setstate__()}.
3339
Guido van Rossum194e57c1995-02-15 15:51:38 +00003340
3341\section{Documentation Strings}
3342
3343A variety of objects now have a new attribute, \code{__doc__}, which
3344is supposed to contain a documentation string (if no documentation is
3345present, the attribute is \code{None}). New syntax, compatible with
3346the old interpreter, allows for convenient initialization of the
3347\code{__doc__} attribute of modules, classes and functions by placing
3348a string literal by itself as the first statement in the suite. It
3349must be a literal --- an expression yielding a string object is not
3350accepted as a documentation string, since future tools may need to
3351derive documentation from source by parsing.
3352
3353Here is a hypothetical, amply documented module called \code{Spam}:
3354
3355\begin{verbatim}
3356"""Spam operations.
3357
3358This module exports two classes, a function and an exception:
3359
3360class Spam: full Spam functionality --- three can sizes
3361class SpamLight: limited Spam functionality --- only one can size
3362
3363def open(filename): open a file and return a corresponding Spam or
3364SpamLight object
3365
3366GoneOff: exception raised for errors; should never happen
3367
3368Note that it is always possible to convert a SpamLight object to a
3369Spam object by a simple method call, but that the reverse operation is
3370generally costly and may fail for a number of reasons.
3371"""
3372
3373class SpamLight:
3374 """Limited spam functionality.
3375
3376 Supports a single can size, no flavor, and only hard disks.
3377 """
3378
3379 def __init__(self, size=12):
3380 """Construct a new SpamLight instance.
3381
3382 Argument is the can size.
3383 """
3384 # etc.
3385
3386 # etc.
3387
3388class Spam(SpamLight):
3389 """Full spam functionality.
3390
3391 Supports three can sizes, two flavor varieties, and all floppy
3392 disk formats still supported by current hardware.
3393 """
3394
3395 def __init__(self, size1=8, size2=12, size3=20):
3396 """Construct a new Spam instance.
3397
3398 Arguments are up to three can sizes.
3399 """
3400 # etc.
3401
3402 # etc.
3403
3404def open(filename = "/dev/null"):
3405 """Open a can of Spam.
3406
3407 Argument must be an existing file.
3408 """
3409 # etc.
3410
3411class GoneOff:
3412 """Class used for Spam exceptions.
3413
3414 There shouldn't be any.
3415 """
3416 pass
3417\end{verbatim}
3418
3419After executing ``\code{import Spam}'', the following expressions
3420return the various documentation strings from the module:
3421
3422\begin{verbatim}
3423Spam.__doc__
3424Spam.SpamLight.__doc__
3425Spam.SpamLight.__init__.__doc__
3426Spam.Spam.__doc__
3427Spam.Spam.__init__.__doc__
3428Spam.open.__doc__
3429Spam.GoneOff.__doc__
3430\end{verbatim}
3431
3432There are emerging conventions about the content and formatting of
3433documentation strings.
3434
3435The first line should always be a short, concise summary of the
3436object's purpose. For brevity, it should not explicitly state the
3437object's name or type, since these are available by other means
3438(except if the name happens to be a verb describing a function's
3439operation). This line should begin with a capital letter and end with
3440a period.
3441
3442If there are more lines in the documentation string, the second line
3443should be blank, visually separating the summary from the rest of the
3444description. The following lines should be one of more of paragraphs
3445describing the objects calling conventions, its side effects, etc.
3446
3447Some people like to copy the Emacs convention of using UPPER CASE for
3448function parameters --- this often saves a few words or lines.
3449
3450The Python parser does not strip indentation from multi-line string
3451literals in Python, so tools that process documentation have to strip
3452indentation. This is done using the following convention. The first
3453non-blank line {\em after} the first line of the string determines the
3454amount of indentation for the entire documentation string. (We can't
3455use the first line since it is generally adjacent to the string's
3456opening quotes so its indentation is not apparent in the string
3457literal.) Whitespace ``equivalent'' to this indentation is then
3458stripped from the start of all lines of the string. Lines that are
3459indented less should not occur, but if they occur all their leading
3460whitespace should be stripped. Equivalence of whitespace should be
3461tested after expansion of tabs (to 8 spaces, normally).
3462
3463In this release, few of the built-in or standard functions and modules
3464have documentation strings.
3465
3466
3467\section{Customizing Import and Built-Ins}
3468
3469In preparation for a ``restricted execution mode'' which will be
3470usable to run code received from an untrusted source (such as a WWW
3471server or client), the mechanism by which modules are imported has
3472been redesigned. It is now possible to provide your own function
3473\code{__import__} which is called whenever an \code{import} statement
3474is executed. There's a built-in function \code{__import__} which
3475provides the default implementation, but more interesting, the various
3476steps it takes are available separately from the new built-in module
3477\code{imp}. (See the section on \code{imp} in the Library Reference
Guido van Rossumabfa2ca1995-07-07 22:57:02 +00003478Manual for more information on this module -- it also contains a
3479complete example of how to write your own \code{__import__} function.)
Guido van Rossum194e57c1995-02-15 15:51:38 +00003480
3481When you do \code{dir()} in a fresh interactive interpreter you will
3482see another ``secret'' object that's present in every module:
3483\code{__builtins__}. This is either a dictionary or a module
3484containing the set of built-in objects used by functions defined in
3485current module. Although normally all modules are initialized with a
3486reference to the same dictionary, it is now possible to use a
3487different set of built-ins on a per-module basis. Together with the
3488fact that the \code{import} statement uses the \code{__import__}
3489function it finds in the importing modules' dictionary of built-ins,
3490this forms the basis for a future restricted execution mode.
3491
3492
3493\section{Python and the World-Wide Web}
3494
3495There is a growing number of modules available for writing WWW tools.
3496The previous release already sported modules \code{gopherlib},
Guido van Rossumbcc95821995-02-16 16:28:48 +00003497\code{ftplib}, \code{httplib} and \code{urllib} (which unifies the
3498other three) for accessing data through the commonest WWW protocols.
3499This release also provides \code{cgi}, to ease the writing of
3500server-side scripts that use the Common Gateway Interface protocol,
3501supported by most WWW servers. The module \code{urlparse} provides
3502precise parsing of a URL string into its components (address scheme,
3503network location, path, parameters, query, and fragment identifier).
Guido van Rossum194e57c1995-02-15 15:51:38 +00003504
Guido van Rossumbcc95821995-02-16 16:28:48 +00003505A rudimentary, parser for HTML files is available in the module
3506\code{htmllib}. It currently supports a subset of HTML 1.0 (if you
3507bring it up to date, I'd love to receive your fixes!). Unfortunately
3508Python seems to be too slow for real-time parsing and formatting of
Guido van Rossum86751151995-02-28 17:14:32 +00003509HTML such as required by interactive WWW browsers --- but it's good
3510enough to write a ``robot'' (an automated WWW browser that searches
3511the web for information).
Guido van Rossum194e57c1995-02-15 15:51:38 +00003512
3513
3514\section{Miscellaneous}
3515
3516\begin{itemize}
3517
3518\item
3519The \code{socket} module now exports all the needed constants used for
3520socket operations, such as \code{SO_BROADCAST}.
3521
3522\item
3523The functions \code{popen()} and \code{fdopen()} in the \code{os}
3524module now follow the pattern of the built-in function \code{open()}:
3525the default mode argument is \code{'r'} and the optional third
3526argument specifies the buffer size, where \code{0} means unbuffered,
3527\code{1} means line-buffered, and any larger number means the size of
3528the buffer in bytes.
3529
3530\end{itemize}
3531
3532
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003533\chapter{New in Release 1.3}
3534
3535
3536This chapter describes yet more recent additions to the Python
3537language and library.
3538
3539
Guido van Rossum9beefa21995-10-08 00:38:51 +00003540\section{Keyword Arguments}
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003541
3542Functions and methods written in Python can now be called using
3543keyword arguments of the form \code{\var{keyword} = \var{value}}. For
3544instance, the following function:
3545
3546\begin{verbatim}
3547def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
3548 print "-- This parrot wouldn't", action,
3549 print "if you put", voltage, "Volts through it."
3550 print "-- Lovely plumage, the", type
3551 print "-- It's", state, "!"
3552\end{verbatim}
3553
3554could be called in any of the following ways:
3555
3556\begin{verbatim}
3557parrot(1000)
3558parrot(action = 'VOOOOOM', voltage = 1000000)
3559parrot('a thousand', state = 'pushing up the daisies')
3560parrot('a million', 'bereft of life', 'jump')
3561\end{verbatim}
3562
3563but the following calls would all be invalid:
3564
3565\begin{verbatim}
3566parrot() # required argument missing
3567parrot(voltage=5.0, 'dead') # non-keyword argument following keyword
3568parrot(110, voltage=220) # duplicate value for argument
3569parrot(actor='John Cleese') # unknown keyword
3570\end{verbatim}
3571
3572In general, an argument list must have the form: zero or more
3573positional arguments followed by zero or more keyword arguments, where
3574the keywords must be chosen from the formal parameter names. It's not
3575important whether a formal parameter has a default value or not. No
3576argument must receive a value more than once -- formal parameter names
3577corresponding to positional arguments cannot be used as keywords in
3578the same calls.
3579
3580Note that no special syntax is required to allow a function to be
3581called with keyword arguments. The additional costs incurred by
3582keyword arguments are only present when a call uses them.
3583
3584(As far as I know, these rules are exactly the same as used by
3585Modula-3, even if they are enforced by totally different means. This
3586is intentional.)
3587
3588When a final formal parameter of the form \code{**\var{name}} is
3589present, it receives a dictionary containing all keyword arguments
3590whose keyword doesn't correspond to a formal parameter. This may be
3591combined with a formal parameter of the form \code{*\var{name}} which
3592receives a tuple containing the positional arguments beyond the formal
3593parameter list. (\code{*\var{name}} must occur before
3594\code{**\var{name}}.) For example, if we define a function like this:
3595
3596\begin{verbatim}
3597def cheeseshop(kind, *arguments, **keywords):
3598 print "-- Do you have any", kind, '?'
3599 print "-- I'm sorry, we're all out of", kind
3600 for arg in arguments: print arg
3601 print '-'*40
3602 for kw in keywords.keys(): print kw, ':', keywords[kw]
3603\end{verbatim}
3604
3605It could be called like this:
3606
3607\begin{verbatim}
3608cheeseshop('Limburger', "It's very runny, sir.",
3609 "It's really very, VERY runny, sir.",
3610 client='John Cleese',
3611 shopkeeper='Michael Palin',
3612 sketch='Cheese Shop Sketch')
3613\end{verbatim}
3614
3615and of course it would print:
3616
3617\begin{verbatim}
3618-- Do you have any Limburger ?
3619-- I'm sorry, we're all out of Limburger
3620It's very runny, sir.
3621It's really very, VERY runny, sir.
3622----------------------------------------
3623client : John Cleese
3624shopkeeper : Michael Palin
3625sketch : Cheese Shop Sketch
3626\end{verbatim}
3627
Guido van Rossum691d4ec1995-10-08 01:14:57 +00003628Consequences of this change include:
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003629
3630\begin{itemize}
3631
3632\item
Guido van Rossum691d4ec1995-10-08 01:14:57 +00003633The built-in function \code{apply()} now has an optional third
3634argument, which is a dictionary specifying any keyword arguments to be
3635passed. For example,
3636\begin{verbatim}
3637apply(parrot, (), {'voltage': 20, 'action': 'voomm'})
3638\end{verbatim}
3639is equivalent to
3640\begin{verbatim}
3641parrot(voltage=20, action='voomm')
3642\end{verbatim}
3643
3644\item
3645There is also a mechanism for functions and methods defined in an
3646extension module (i.e., implemented in C or C++) to receive a
3647dictionary of their keyword arguments. By default, such functions do
3648not accept keyword arguments, since the argument names are not
3649available to the interpreter.
3650
3651\item
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003652In the effort of implementing keyword arguments, function and
3653especially method calls have been sped up significantly -- for a
3654method with ten formal parameters, the call overhead has been cut in
3655half; for a function with one formal parameters, the overhead has been
3656reduced by a third.
3657
3658\item
3659The format of \code{.pyc} files has changed (again).
3660
Guido van Rossum9beefa21995-10-08 00:38:51 +00003661\item
3662The \code{access} statement has been disabled. The syntax is still
3663recognized but no code is generated for it. (There were some
3664unpleasant interactions with changes for keyword arguments, and my
3665plan is to get rid of \code{access} altogether in favor of a different
3666approach.)
3667
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003668\end{itemize}
3669
Guido van Rossum9beefa21995-10-08 00:38:51 +00003670\section{Changes to the WWW and Internet tools}
3671
3672\begin{itemize}
3673
3674\item
3675The \code{htmllib} module has been rewritten in an incompatible
3676fashion. The new version is considerably more complete (HTML 2.0
3677except forms, but including all ISO-8859-1 entity definitions), and
3678easy to use. Small changes to \code{sgmllib} have also been made, to
3679better match the tokenization of HTML as recognized by other web
3680tools.
3681
3682\item
3683A new module \code{formatter} has been added, for use with the new
3684\code{htmllib} module.
3685
3686\item
3687The \code{urllib}and \code{httplib} modules have been changed somewhat
3688to allow overriding unknown URL types and to support authentication.
3689They now use \code{mimetools.Message} instead of \code{rfc822.Message}
3690to parse headers. The \code{endrequest()} method has been removed
3691from the HTTP class since it breaks the interaction with some servers.
3692
3693\item
3694The \code{rfc822.Message} class has been changed to allow a flag to be
3695passed in that says that the file is unseekable.
3696
3697\item
3698The \code{ftplib} module has been fixed to be (hopefully) more robust
3699on Linux.
3700
3701\item
3702Several new operations that are optionally supported by servers have
3703been added to \code{nntplib}: \code{xover}, \code{xgtitle},
3704\code{xpath} and \code{date}. % thanks to Kevan Heydon
3705
3706\end{itemize}
3707
3708\section{Other Language Changes}
3709
3710\begin{itemize}
3711
3712\item
3713The \code{raise} statement now takes an optional argument which
3714specifies the traceback to be used when printing the exception's stack
3715trace. This must be a traceback object, such as found in
3716\code{sys.exc_traceback}. When omitted or given as \code{None}, the
3717old behavior (to generate a stack trace entry for the current stack
3718frame) is used.
3719
3720\item
3721The tokenizer is now more tolerant of alien whitespace. Control-L in
3722the leading whitespace of a line resets the column number to zero,
3723while Control-R just before the end of the line is ignored.
3724
3725\end{itemize}
3726
3727\section{Changes to Built-in Operations}
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003728
3729\begin{itemize}
3730
3731\item
3732For file objects, \code{\var{f}.read(0)} and
3733\code{\var{f}.readline(0)} now return an empty string rather than
3734reading an unlimited number of bytes. For the latter, omit the
3735argument altogether or pass a negative value.
3736
3737\item
3738A new system variable, \code{sys.platform}, has been added. It
3739specifies the current platform, e.g. \code{sunos5} or \code{linux1}.
3740
3741\item
3742The built-in functions \code{input()} and \code{raw_input()} now use
3743the GNU readline library when it has been configured (formerly, only
3744interactive input to the interpreter itself was read using GNU
3745readline). The GNU readline library provides elaborate line editing
3746and history. The Python debugger (\code{pdb}) is the first
3747beneficiary of this change.
3748
3749\item
3750Two new built-in functions, \code{globals()} and \code{locals()},
3751provide access to dictionaries containming current global and local
3752variables, respectively. (These augment rather than replace
3753\code{vars()}, which returns the current local variables when called
3754without an argument, and a module's global variables when called with
3755an argument of type module.)
3756
3757\item
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003758The built-in function \code{compile()} now takes a third possible
3759value for the kind of code to be compiled: specifying \code{'single'}
3760generates code for a single interactive statement, which prints the
3761output of expression statements that evaluate to something else than
3762\code{None}.
3763
Guido van Rossum9beefa21995-10-08 00:38:51 +00003764\end{itemize}
3765
3766\section{Library Changes}
3767
3768\begin{itemize}
3769
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003770\item
Guido van Rossum691d4ec1995-10-08 01:14:57 +00003771There are new module \code{ni} and \code{ihooks} that support
3772importing modules with hierarchical names such as \code{A.B.C}. This
3773is enabled by writing \code{import ni; ni.ni()} at the very top of the
3774main program. These modules are amply documented in the Python
3775source.
3776
3777\item
3778The module \code{rexec} has been rewritten (incompatibly) to define a
3779class and to use \code{ihooks}.
3780
3781\item
Guido van Rossum9beefa21995-10-08 00:38:51 +00003782The \code{string.split()} and \code{string.splitfields()} functions
3783are now the same function (the presence or absence of the second
3784argument determines which operation is invoked); similar for
3785\code{string.join()} and \code{string.joinfields()}.
3786
3787\item
3788The \code{Tkinter} module and its helper \code{Dialog} have been
3789revamped to use keyword arguments. Tk 4.0 is now the standard. A new
3790module \code{FileDialog} has been added which implements standard file
3791selection dialogs.
3792
3793\item
3794The optional built-in modules \code{dbm} and \code{gdbm} are more
3795coordinated --- their \code{open()} functions now take the same values
3796for their \var{flag} argument, and the \var{flag} and \var{mode}
3797argument have default values (to open the database for reading only,
3798and to create the database with mode \code{0666} minuse the umask,
3799respectively). The memory leaks have finally been fixed.
3800
3801\item
3802A new dbm-like module, \code{bsddb}, has been added, which uses the
3803BSD DB package's hash method. % thanks to David Ely
3804
3805\item
3806A portable (though slow) dbm-clone, implemented in Python, has been
3807added for systems where none of the above is provided. It is aptly
3808dubbed \code{dumbdbm}.
3809
3810\item
3811The module \code{anydbm} provides a unified interface to \code{bsddb},
3812\code{gdbm}, \code{dbm}, and \code{dumbdbm}, choosing the first one
3813available.
3814
3815\item
3816A new extension module, \code{binascii}, provides a variety of
3817operations for conversion of text-encoded binary data.
3818
3819\item
3820There are three new or rewritten companion modules implemented in
3821Python that can encode and decode the most common such formats:
3822\code{uu} (uuencode), \code{base64} and \code{binhex}.
3823
3824\item
3825A module to handle the MIME encoding quoted-printable has also been
3826added: \code{quopri}.
3827
Guido van Rossumaa93ca81995-10-11 17:47:45 +00003828\item
3829The parser module (which provides an interface to the Python parser's
3830abstract syntax trees) has been rewritten (incompatibly) by Fred
3831Drake. It now lets you change the parse tree and compile the result!
3832
Guido van Rossumbf032a91995-10-11 19:28:39 +00003833\item
3834The \code{syslog} module has been upgraded and documented.
3835% thanks to Steve Clift
3836
Guido van Rossum9beefa21995-10-08 00:38:51 +00003837\end{itemize}
3838
3839\section{Other Changes}
3840
3841\begin{itemize}
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003842
3843\item
3844The dynamic module loader recognizes the fact that different filenames
3845point to the same shared library and loads the library only once, so
3846you can have a single shared library that defines multiple modules.
3847(SunOS / SVR4 style shared libraries only.)
3848
3849\item
3850Jim Fulton's ``abstract object interface'' has been incorporated into
3851the run-time API. For more detailes, read the files
3852\code{Include/abstract.h} and \code{Objects/abstract.c}.
3853
3854\item
Guido van Rossum9beefa21995-10-08 00:38:51 +00003855The Macintosh version is much more robust now.
3856
3857\item
Guido van Rossumeafe32a1995-08-10 14:18:10 +00003858Numerous things I have forgotten or that are so obscure no-one will
3859notice them anyway :-)
3860
3861\end{itemize}
3862
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00003863\end{document}