blob: 58d1575bb1c9f5135a4d95b30419f68a8f057e94 [file] [log] [blame]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001% Format this file with latex.
2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00003\documentstyle[palatino,11pt,myformat]{article}
4%\documentstyle[11pt,myformat]{article}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00005
6\title{\bf
7 Python Tutorial \\
8 (DRAFT)
9}
10
11\author{
12 Guido van Rossum \\
13 Dept. CST, CWI, Kruislaan 413 \\
14 1098 SJ Amsterdam, The Netherlands \\
15 E-mail: {\tt guido@cwi.nl}
16}
17
18\begin{document}
19
20\pagenumbering{roman}
21
22\maketitle
23
24\begin{abstract}
25
26\noindent
27\Python\ is a simple, yet powerful programming language that bridges the
28gap between C and shell programming, and is thus ideally suited for rapid
29prototyping.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000030Its syntax is put together from constructs borrowed from a variety of other
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000031languages; most prominent are influences from ABC, C, Modula-3 and Icon.
32
33The \Python\ interpreter is easily extended with new functions and data
34types implemented in C.
35\Python\ is also suitable as an extension language for highly
36customizable C applications such as editors or window managers.
37
38\Python\ is available for various operating systems, amongst which
39several flavors of \UNIX, Amoeba, and the Apple Macintosh O.S.
40
41This tutorial introduces the reader informally to the basic concepts and
42features of the \Python\ language and system.
43It helps to have a \Python\ interpreter handy for hands-on experience,
44but as the examples are self-contained, the tutorial can be read
45off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000046
47For a description of standard objects and modules, see the Library
48Reference document.
49The Language Reference document (XXX not yet existing)
50gives a more formal reference to the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000051
52\end{abstract}
53
54\pagebreak
55
56\tableofcontents
57
58\pagebreak
59
60\pagenumbering{arabic}
61
62\section{Whetting Your Appetite}
63
64If you ever wrote a large shell script, you probably know this feeling:
65you'd love to add yet another feature, but it's already so slow, and so
66big, and so complicated; or the feature involves a system call or other
Guido van Rossum2292b8e1991-01-23 16:31:24 +000067funcion that is only accessible from C \ldots
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000068Usually the problem at hand isn't serious enough to warrant rewriting
69the script in C; perhaps because the problem requires variable-length
70strings or other data types (like sorted lists of file names) that
71are easy in the shell but lots of work to implement in C; or perhaps
72just because you're not sufficiently familiar with C.
73
74In all such cases, \Python\ is just the language for you.
75\Python\ is simple to use, but it is a real programming language, offering
76much more structure and support for large programs than the shell has.
77On the other hand, it also offers much more error checking than C, and,
78being a
Guido van Rossum2292b8e1991-01-23 16:31:24 +000079{\em very-high-level language},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000080it has high-level data types built in, such as flexible arrays and
81dictionaries that would cost you days to implement efficiently in C.
82Because of its more general data types \Python\ is applicable to a
83much larger problem domain than
Guido van Rossum2292b8e1991-01-23 16:31:24 +000084{\em Awk}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000085or even
Guido van Rossum2292b8e1991-01-23 16:31:24 +000086{\em Perl},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000087yet most simple things are at least as easy in \Python\ as in those
88languages.
89
90\Python\ allows you to split up your program in modules that can be reused
91in other \Python\ programs.
92It comes with a large collection of standard modules that you can use as
93the basis for your programs --- or as examples to start learning to
94program in \Python.
95There are also built-in modules that provide things like file I/O,
96system calls, and even a generic interface to window systems (STDWIN).
97
98\Python\ is an interpreted language, which saves you considerable time
99during program development because no compilation and linking is
100necessary.
101The interpreter can be used interactively, which makes it easy to
102experiment with features of the language, to write throw-away programs,
103or to test functions during bottom-up program development.
104It is also a handy desk calculator.
105
106\Python\ allows writing very compact and readable programs.
107Programs written in \Python\ are typically much shorter than equivalent C
108programs:
109No declarations are necessary (all type checking is
110dynamic); statement grouping is done by indentation instead of begin/end
111brackets; and the high-level data types allow you to express complex
112operations in a single statement.
113
114\Python\ is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000115{\em extensible}:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000116if you know how to program in C it is easy to add a new built-in module
117to the interpreter, either to perform critical operations at maximum
118speed, or to link \Python\ programs to libraries that may be only available
119in binary form (such as a vendor-specific graphics library).
120Once you are really hooked, you can link the \Python\ interpreter into an
121application written in C and use it as an extension or command language.
122
123\subsection{Where From Here}
124
125Now that you are all excited about \Python, you'll want to examine it in
126some more detail.
127Since the best introduction to a language is using it, you are invited
128here to do so.
129
130In the next section, the mechanics of using the interpreter are
131explained.
132This is rather mundane information, but essential for trying out the
133examples shown later.
134The rest of the tutorial introduces various features of the \Python\
135language and system though examples, beginning with simple expressions,
136statements and data types, through functions and modules, and finally
137touching upon advanced concepts like exceptions and classes.
138
139\section{Using the Python Interpreter}
140
141The \Python\ interpreter is usually installed as
142{\tt /usr/local/python}
143on those machines where it is available; putting
144{\tt /usr/local}
145in your \UNIX\ shell's search path makes it possible to start it by
146typing the command
147\begin{code}\begin{verbatim}
148python
149\end{verbatim}\end{code}
150to the shell.
151Since the choice of the directory where the interpreter lives is an
152installation option, other places instead of
153{\tt /usr/local}
154are possible; check with your local \Python\ guru or system
155administrator.%
156\footnote{
157 At CWI, at the time of writing, the interpreter can be found in
158 the following places:
159 On the Amoeba Ultrix machines, use the standard path,
160 {\tt /usr/local/python}.
161 On the Sun file servers, use
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000162 {\tt /ufs/guido/bin/}{\em arch}{\tt /python},
163 where {\em arch} can be {\tt sgi} or {\tt sun4}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000164 On piring, use {\tt /userfs3/amoeba/bin/python}.
165 (If you can't find a binary advertised here, get in touch with me.)
166}
167
168The interpreter operates somewhat like the \UNIX\ shell: when called with
169standard input connected to a tty device, it reads and executes commands
170interactively; when called with a file name argument or with a file as
171standard input, it reads and executes a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000172{\em script}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000173from that file.%
174\footnote{
175 There is a difference between ``{\tt python file}'' and
176 ``{\tt python $<$file}''. In the latter case {\tt input()} and
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000177 {\tt raw\_input()} are satisfied from {\em file}, which has
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000178 already been read until the end by the parser, so they will read
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000179 EOF immediately. In the former case (which is usually what
180 you want) they are satisfied from whatever file or device is
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000181 connected to standard input of the \Python\ interpreter.
182}
183If available, the script name and additional arguments thereafter are
184passed to the script in the variable
185{\tt sys.argv},
186which is a list of strings.
187
188When standard input is a tty, the interpreter is said to be in
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000189{\em interactive\ mode}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000190In this mode it prompts for the next command with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000191{\em primary\ prompt},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000192usually three greater-than signs ({\tt >>>}); for continuation lines
193it prompts with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000194{\em secondary\ prompt},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000195by default three dots ({\tt ...}).
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000196Typing an EOF (Control-D) at the primary prompt causes the interpreter
197to exit with a zero exit status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000198
199When an error occurs in interactive mode, the interpreter prints a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000200message and a stack trace and returns to the primary prompt; with input
201from a file, it exits with a nonzero exit status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000202(Exceptions handled by an
203{\tt except}
204clause in a
205{\tt try}
206statement are not errors in this context.)
207Some errors are unconditionally fatal and cause an exit with a nonzero
208exit; this applies to internal inconsistencies and some cases of running
209out of memory.
210All error messages are written to the standard error stream; normal
211output from the executed commands is written to standard output.
212
213Typing an interrupt (normally Control-C or DEL) to the primary or
214secondary prompt cancels the input and returns to the primary prompt.
215Typing an interrupt while a command is being executed raises the
216{\tt KeyboardInterrupt}
217exception, which may be handled by a
218{\tt try}
219statement.
220
221When a module named
222{\tt foo}
223is imported, the interpreter searches for a file named
224{\tt foo.py}
225in a list of directories specified by the environment variable
226{\tt PYTHONPATH}.
227It has the same syntax as the \UNIX\ shell variable
228{\tt PATH},
229i.e., a list of colon-separated directory names.
230When
231{\tt PYTHONPATH}
232is not set, an installation-dependent default path is used, usually
233{\tt .:/usr/local/lib/python}.%
234\footnote{
235 Modules are really searched in the list of directories given by
236 the variable {\tt sys.path} which is initialized from
237 {\tt PYTHONPATH} or from the installation-dependent default.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000238 See the section on Standard Modules later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000239}
240The built-in module
241{\tt stdwin},
242if supported at all, is only available if the interpreter is started
243with the
244{\bf --s}
245flag.
246If this flag is given, stdwin is initialized as soon as the interpreter
247is started, and in the case of X11 stdwin certain command line arguments
248(like
249{\bf --display} )
250are consumed by stdwin.
251
252On BSD'ish \UNIX\ systems, \Python\ scripts can be made directly executable,
253like shell scripts, by putting the line
254\begin{code}\begin{verbatim}
255#! /usr/local/python
256\end{verbatim}\end{code}
257(assuming that's the name of the interpreter) at the beginning of the
258script and giving the file an executable mode.
259(The
260{\tt \#!}
261must be the first two characters of the file.)
262For scripts that use the built-in module
263{\tt stdwin},
264use
265\begin{code}\begin{verbatim}
266#! /usr/local/python -s
267\end{verbatim}\end{code}
268
269\subsection{Interactive Input Editing and History Substitution}
270
271Some versions of the \Python\ interpreter support editing of the current
272input line and history substitution, similar to facilities found in the
273Korn shell and the GNU Bash shell.
274This is implemented using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000275{\em GNU\ Readline}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000276library, which supports Emacs-style and vi-style editing.
277This library has its own documentation which I won't duplicate here;
278however, the basics are easily explained.
279
280If supported,%
281\footnote{
282 Perhaps the quickest check to see whether command line editing
283 is supported is typing Control-P to the first \Python\ prompt
284 you get. If it beeps, you have command line editing.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000285 If not, you can skip the rest of this section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000286}
287input line editing is active whenever the interpreter prints a primary
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000288or secondary prompt.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000289The current line can be edited using the conventional Emacs control
290characters.
291The most important of these are:
292C-A (Control-A) moves the cursor to the beginning of the line, C-E to
293the end, C-B moves it one position to the left, C-F to the right.
294Backspace erases the character to the left of the cursor, C-D the
295character to its right.
296C-K kills (erases) the rest of the line to the right of the cursor, C-Y
297yanks back the last killed string.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000298C-underscore undoes the last change you made; it can be repeated for
299cumulative effect.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000300
301History substitution works as follows.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000302All non-empty input lines issued are saved in a history buffer,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000303and when a new prompt is given you are positioned on a new line at the
304bottom of this buffer.
305C-P moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000306Any line in the history buffer can be edited; an asterisk appears in
307front of the prompt to mark a line as modified.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000308Pressing the Return key passes the current line to the interpreter.
309C-R starts an incremental reverse search; C-S starts a forward search.
310
311The key bindings and some other parameters of the Readline library can
312be customized by placing commands in an initialization file called
313{\tt \$HOME/.initrc}.
314Key bindings have the form
315\begin{code}\begin{verbatim}
316key-name: function-name
317\end{verbatim}\end{code}
318and options can be set with
319\begin{code}\begin{verbatim}
320set option-name value
321\end{verbatim}\end{code}
322Example:
323\begin{code}\begin{verbatim}
324# I prefer vi-style editing:
325set editing-mode vi
326# Edit using a single line:
327set horizontal-scroll-mode On
328# Rebind some keys:
329Meta-h: backward-kill-word
330Control-u: universal-argument
331\end{verbatim}\end{code}
332Note that the default binding for TAB in \Python\ is to insert a TAB
333instead of Readline's default filename completion function.
334If you insist, you can override this by putting
335\begin{code}\begin{verbatim}
336TAB: complete
337\end{verbatim}\end{code}
338in your
339{\tt \$HOME/.inputrc}.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000340(Of course, this makes it hard to type indented continuation lines.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000341
342This facility is an enormous step forward compared to previous versions of
343the interpreter; however, some wishes are left:
344It would be nice if the proper indentation were suggested on
345continuation lines (the parser knows if an indent token is required
346next).
347The completion mechanism might use the interpreter's symbol table.
348A function to check (or even suggest) matching parentheses, quotes
349etc. would also be useful.
350
351\section{An Informal Introduction to Python}
352
353In the following examples, input and output are distinguished by the
354presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat the
355example, you must type everything after the prompt, when the prompt
356appears; everything on lines that do not begin with a prompt is output
357from the interpreter.
358Note that a secondary prompt on a line by itself in an example means you
359must type a blank line; this is used to end a multi-line command.
360
361\subsection{Using Python as a Calculator}
362
363Let's try some simple \Python\ commands.
364Start the interpreter and wait for the primary prompt,
365{\tt >>>}.
366The interpreter acts as a simple calculator: you can type an expression
367at it and it will write the value.
368Expression syntax is straightforward: the operators
369{\tt +},
370{\tt -},
371{\tt *}
372and
373{\tt /}
374work just as in most other languages (e.g., Pascal or C); parentheses
375can be used for grouping.
376For example:
377\begin{code}\begin{verbatim}
378>>> # This is a comment
379>>> 2+2
3804
381>>>
382>>> (50-5+5*6+25)/4
38325
384>>> # Division truncates towards zero:
385>>> 7/3
3862
387>>>
388\end{verbatim}\end{code}
389As in C, the equal sign ({\tt =}) is used to assign a value to a variable.
390The value of an assignment is not written:
391\begin{code}\begin{verbatim}
392>>> width = 20
393>>> height = 5*9
394>>> width * height
395900
396>>>
397\end{verbatim}\end{code}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000398There is some support for floating point, but you can't mix floating
399point and integral numbers in expression (yet):
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000400\begin{code}\begin{verbatim}
401>>> 10.0 / 3.3
4023.0303030303
403>>>
404\end{verbatim}\end{code}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000405
406Besides numbers, \Python\ can also manipulate strings, enclosed in single
407quotes:
408\begin{code}\begin{verbatim}
409>>> 'foo bar'
410'foo bar'
411>>> 'doesn\'t'
412'doesn\'t'
413>>>
414\end{verbatim}\end{code}
415Strings are written inside quotes and with quotes and other funny
416characters escaped by backslashes, to show the precise value.
417(There is also a way to write strings without quotes and escapes.)
418Strings can be concatenated (glued together) with the
419{\tt +}
420operator, and repeated with
421{\tt *}:
422\begin{code}\begin{verbatim}
423>>> word = 'Help' + 'A'
424>>> word
425'HelpA'
426>>> '<' + word*5 + '>'
427'<HelpAHelpAHelpAHelpAHelpA>'
428>>>
429\end{verbatim}\end{code}
430Strings can be subscripted; as in C, the first character of a string has
431subscript 0.
432There is no separate character type; a character is simply a string of
433size one.
434As in Icon, substrings can be specified with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000435{\em slice}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000436notation: two subscripts (indices) separated by a colon.
437\begin{code}\begin{verbatim}
438>>> word[4]
439'A'
440>>> word[0:2]
441'He'
442>>> word[2:4]
443'lp'
444>>> # Slice indices have useful defaults:
445>>> word[:2] # Take first two characters
446'He'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000447>>> word[2:] # Drop first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000448'lpA'
449>>> # A useful invariant: s[:i] + s[i:] = s
450>>> word[:3] + word[3:]
451'HelpA'
452>>>
453\end{verbatim}\end{code}
454Degenerate cases are handled gracefully: an index that is too large is
455replaced by the string size, an upper bound smaller than the lower bound
456returns an empty string.
457\begin{code}\begin{verbatim}
458>>> word[1:100]
459'elpA'
460>>> word[10:]
461''
462>>> word[2:1]
463''
464>>>
465\end{verbatim}\end{code}
466Slice indices (but not simple subscripts) may be negative numbers, to
467start counting from the right.
468For example:
469\begin{code}\begin{verbatim}
470>>> word[-2:] # Take last two characters
471'pA'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000472>>> word[:-2] # Drop last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000473'Hel'
474>>> # But -0 does not count from the right!
475>>> word[-0:] # (since -0 equals 0)
476'HelpA'
477>>>
478\end{verbatim}\end{code}
479The best way to remember how slices work is to think of the indices as
480pointing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000481{\em between}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000482characters, with the left edge of the first character numbered 0.
483Then the right edge of the last character of a string of
484{\tt n}
485characters has index
486{\tt n},
487for example:
488\begin{code}\begin{verbatim}
489 +---+---+---+---+---+
490 | H | e | l | p | A |
491 +---+---+---+---+---+
492 0 1 2 3 4 5
493-5 -4 -3 -2 -1
494\end{verbatim}\end{code}
495The first row of numbers gives the position of the indices 0...5 in the
496string; the second row gives the corresponding negative indices.
497For nonnegative indices, the length of a slice is the difference of the
498indices, if both are within bounds,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000499e.g.,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000500the length of
501{\tt word[1:3]}
502is 3--1 = 2.
503
504Finally, the built-in function {\tt len()} computes the length of a
505string:
506\begin{code}\begin{verbatim}
507>>> s = 'supercalifragilisticexpialidocious'
508>>> len(s)
50934
510>>>
511\end{verbatim}\end{code}
512
513\Python\ knows a number of
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000514{\em compound}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000515data types, used to group together other values.
516The most versatile is the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000517{\em list},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000518which can be written as a list of comma-separated values between square
519brackets:
520\begin{code}\begin{verbatim}
521>>> a = ['foo', 'bar', 100, 1234]
522>>> a
523['foo', 'bar', 100, 1234]
524>>>
525\end{verbatim}\end{code}
526As for strings, list subscripts start at 0:
527\begin{code}\begin{verbatim}
528>>> a[0]
529'foo'
530>>> a[3]
5311234
532>>>
533\end{verbatim}\end{code}
534Lists can be sliced and concatenated like strings:
535\begin{code}\begin{verbatim}
536>>> a[1:3]
537['bar', 100]
538>>> a[:2] + ['bletch', 2*2]
539['foo', 'bar', 'bletch', 4]
540>>>
541\end{verbatim}\end{code}
542Unlike strings, which are
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000543{\em immutable},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000544it is possible to change individual elements of a list:
545\begin{code}\begin{verbatim}
546>>> a
547['foo', 'bar', 100, 1234]
548>>> a[2] = a[2] + 23
549>>> a
550['foo', 'bar', 123, 1234]
551>>>
552\end{verbatim}\end{code}
553Assignment to slices is also possible, and this may even change the size
554of the list:
555\begin{code}\begin{verbatim}
556>>> # Replace some items:
557>>> a[0:2] = [1, 12]
558>>> a
559[1, 12, 123, 1234]
560>>> # Remove some:
561>>> a[0:2] = []
562>>> a
563[123, 1234]
564>>> # Insert some:
565>>> a[1:1] = ['bletch', 'xyzzy']
566>>> a
567[123, 'bletch', 'xyzzy', 1234]
568>>>
569\end{verbatim}\end{code}
570The built-in function {\tt len()} also applies to lists:
571\begin{code}\begin{verbatim}
572>>> len(a)
5734
574>>>
575\end{verbatim}\end{code}
576
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000577\subsection{Tuples and Sequences}
578
579XXX To Be Done.
580
581\subsection{First Steps Towards Programming}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000582
583Of course, we can use \Python\ for more complicated tasks than adding two
584and two together.
585For instance, we can write an initial subsequence of the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000586{\em Fibonacci}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000587series as follows:
588\begin{code}\begin{verbatim}
589>>> # Fibonacci series:
590>>> # the sum of two elements defines the next
591>>> a, b = 0, 1
592>>> while b < 100:
593... print b
594... a, b = b, a+b
595...
5961
5971
5982
5993
6005
6018
60213
60321
60434
60555
60689
607>>>
608\end{verbatim}\end{code}
609This example introduces several new features.
610\begin{itemize}
611\item
612The first line contains a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000613{\em multiple\ assignment}:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000614the variables
615{\tt a}
616and
617{\tt b}
618simultaneously get the new values 0 and 1.
619On the last line this is used again, demonstrating that the expressions
620on the right-hand side are all evaluated first before any of the
621assignments take place.
622\item
623The
624{\tt while}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000625loop executes as long as the condition (here: $b < 100$) remains true.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000626In \Python, as in C, any non-zero integer value is true; zero is false.
627The condition may also be a string or list value, in fact any sequence;
628anything with a non-zero length is true, empty sequences are false.
629The test used in the example is a simple comparison.
630The standard comparison operators are written as
631{\tt <},
632{\tt >},
633{\tt =},
634{\tt <=},
635{\tt >=}
636and
637{\tt <>}.%
638\footnote{
639 The ambiguity of using {\tt =}
640 for both assignment and equality is resolved by disallowing
641 unparenthesized conditions at the right hand side of assignments.
642}
643\item
644The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000645{\em body}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000646of the loop is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000647{\em indented}: indentation is \Python's way of grouping statements.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000648\Python\ does not (yet!) provide an intelligent input line editing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000649facility, so you have to type a tab or space(s) for each indented line.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000650In practice you will prepare more complicated input for \Python\ with a
651text editor; most text editors have an auto-indent facility.
652When a compound statement is entered interactively, it must be
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000653followed by a blank line to indicate completion (since the parser
654cannot guess when you have typed the last line).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000655\item
656The
657{\tt print}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000658statement writes the value of the expression(s) it is given.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000659It differs from just writing the expression you want to write (as we did
660earlier in the calculator examples) in the way it handles multiple
661expressions and strings.
662Strings are written without quotes and a space is inserted between
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000663items, so you can format things nicely, like this:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000664\begin{code}\begin{verbatim}
665>>> i = 256*256
666>>> print 'The value of i is', i
667The value of i is 65536
668>>>
669\end{verbatim}\end{code}
670A trailing comma avoids the newline after the output:
671\begin{code}\begin{verbatim}
672>>> a, b = 0, 1
673>>> while b < 1000:
674... print b,
675... a, b = b, a+b
676...
6771 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
678>>>
679\end{verbatim}\end{code}
680Note that the interpreter inserts a newline before it prints the next
681prompt if the last line was not completed.
682\end{itemize}
683
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000684\subsection{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000685
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000686Besides the {\tt while} statement just introduced, \Python\ knows the
687usual control flow statements known from other languages, with some
688twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000689
690\subsubsection{If Statements}
691
692Perhaps the most well-known statement type is the {\tt if} statement.
693For example:
694\begin{code}\begin{verbatim}
695>>> if x < 0:
696... x = 0
697... print 'Negative changed to zero'
698... elif x = 0:
699... print 'Zero'
700... elif x = 1:
701... print 'Single'
702... else:
703... print 'More'
704...
705\end{verbatim}\end{code}
706There can be zero or more {\tt elif} parts, and the {\tt else} part is
707optional.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000708The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to
709avoid excessive indentation.
710An {\tt if...elif...elif...} sequence is a substitute for the
711{\em switch} or {\em case} statements found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000712
713\subsubsection{For Statements}
714
715The {\tt for} statement in \Python\ differs a bit from what you may be
716used to in C or Pascal.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000717Rather than always iterating over an arithmetic progression of numbers
718(as Pascal), or leaving the user completely free in the iteration test
719and step (as C), \Python's {\tt for} statement iterates over the items
720of any sequence (e.g., a list or a string).
721For example (no pun intended):
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000722\begin{code}\begin{verbatim}
723>>> # Measure some strings:
724>>> a = ['cat', 'window', 'defenestrate']
725>>> for x in a:
726... print x, len(x)
727...
728cat 3
729window 6
730defenestrate 12
731>>>
732\end{verbatim}\end{code}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000733
734\subsubsection{The {\tt range()} Function}
735
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000736If you do need to iterate over a sequence of numbers, the built-in
737function {\tt range()} comes in handy.
738It generates lists containing arithmetic progressions,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000739e.g.:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000740\begin{code}\begin{verbatim}
741>>> range(10)
742[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
743>>>
744\end{verbatim}\end{code}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000745The given end point is never part of the generated list;
746{\tt range(10)} generates a list of 10 values,
747exactly the legal indices for items of a sequence of length 10.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000748It is possible to let the range start at another number, or to specify a
749different increment (even negative):
750\begin{code}\begin{verbatim}
751>>> range(5, 10)
752[5, 6, 7, 8, 9]
753>>> range(0, 10, 3)
754[0, 3, 6, 9]
755>>> range(-10, -100, -30)
756[-10, -40, -70]
757>>>
758\end{verbatim}\end{code}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000759To iterate over the indices of a sequence, combine {\tt range()}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000760and {\tt len()} as follows:
761\begin{code}\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000762>>> a = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000763>>> for i in range(len(a)):
764... print i, a[i]
765...
7660 Mary
7671 had
7682 a
7693 little
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007704 boy
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000771>>>
772\end{verbatim}\end{code}
773
774\subsubsection{Break Statements and Else Clauses on Loops}
775
776The {\tt break} statement breaks out of the smallest enclosing {\tt for}
777or {\tt while} loop.
778Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000779loop terminates through exhaustion of the list (with {\tt for}) or when
780the condition becomes false (with {\tt while}) but not when the loop is
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000781terminated by a {\tt break} statement.
782This is exemplified by the following loop, which searches for a list
783item of value 0:
784\begin{code}\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000785>>> for n in range(2, 10):
786... for x in range(2, n):
787... if n % x = 0:
788... print n, 'equals', x, '*', n/x
789... break
790... else:
791... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000792...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007932 is a prime number
7943 is a prime number
7954 equals 2 * 2
7965 is a prime number
7976 equals 2 * 3
7987 is a prime number
7998 equals 2 * 4
8009 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000801>>>
802\end{verbatim}\end{code}
803
804\subsubsection{Pass Statements}
805
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000806The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000807It can be used when a statement is required syntactically but the
808program requires no action.
809For example:
810\begin{code}\begin{verbatim}
811>>> while 1:
812... pass # Busy-wait for keyboard interrupt
813...
814\end{verbatim}\end{code}
815
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000816\subsubsection{Conditions Revisited}
817
818XXX To Be Done.
819
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000820\subsection{Defining Functions}
821
822We can create a function that writes the Fibonacci series to an
823arbitrary boundary:
824\begin{code}\begin{verbatim}
825>>> def fib(n): # write Fibonacci series up to n
826... a, b = 0, 1
827... while b <= n:
828... print b,
829... a, b = b, a+b
830...
831>>> # Now call the function we just defined:
832>>> fib(2000)
8331 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
834>>>
835\end{verbatim}\end{code}
836The keyword
837{\tt def}
838introduces a function
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000839{\em definition}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000840It must be followed by the function name and the parenthesized list of
841formal parameters.
842The statements that form the body of the function starts at the next
843line, indented by a tab stop.
844The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000845{\em execution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000846of a function introduces a new symbol table used for the local variables
847of the function.
848More precisely, all variable assignments in a function store the value
849in the local symbol table; variable references first look in the local
850symbol table, then in the global symbol table, and then in the table of
851built-in names.
852Thus, the global symbol table is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000853{\em read-only}
854within a function.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000855The actual parameters (arguments) to a function call are introduced in
856the local symbol table of the called function when it is called;
857thus, arguments are passed using
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000858{\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000859\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000860 Actually, {\em call by object reference} would be a better
861 description, since if a mutable object is passed, the caller
862 will see any changes the callee makes to it (e.g., items
863 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000864}
865When a function calls another function, a new local symbol table is
866created for that call.
867
868A function definition introduces the function name in the global symbol
869table.
870The value has a type that is recognized by the interpreter as a
871user-defined function.
872This value can be assigned to another name which can then also be used
873as a function.
874This serves as a general renaming mechanism:
875\begin{code}\begin{verbatim}
876>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000877<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000878>>> f = fib
879>>> f(100)
8801 1 2 3 5 8 13 21 34 55 89
881>>>
882\end{verbatim}\end{code}
883You might object that
884{\tt fib}
885is not a function but a procedure.
886In \Python, as in C, procedures are just functions that don't return a
887value.
888In fact, technically speaking, procedures do return a value, albeit a
889rather boring one.
890This value is called {\tt None} (it's a built-in name).
891Writing the value {\tt None} is normally suppressed by the interpreter
892if it would be the only value written.
893You can see it if you really want to:
894\begin{code}\begin{verbatim}
895>>> print fib(0)
896None
897>>>
898\end{verbatim}\end{code}
899It is simple to write a function that returns a list of the numbers of
900the Fibonacci series, instead of printing it:
901\begin{code}\begin{verbatim}
902>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000903... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000904... a, b = 0, 1
905... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000906... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000907... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000908... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000909...
910>>> f100 = fib2(100) # call it
911>>> f100 # write the result
912[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
913>>>
914\end{verbatim}\end{code}
915This example, as usual, demonstrates some new \Python\ features:
916\begin{itemize}
917\item
918The
919{\tt return}
920statement returns with a value from a function.
921{\tt return}
922without an expression argument is used to return from the middle of a
923procedure (falling off the end also returns from a proceduce).
924\item
925The statement
926{\tt ret.append(b)}
927calls a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000928{\em method}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000929of the list object
930{\tt ret}.
931A method is a function that `belongs' to an object and is named
932{\tt obj.methodname},
933where
934{\tt obj}
935is some object (this may be an expression), and
936{\tt methodname}
937is the name of a method that is defined by the object's type.
938Different types define different methods.
939Methods of different types may have the same name without causing
940ambiguity.
941See the section on classes, later, to find out how you can define your
942own object types and methods.
943The method
944{\tt append}
945shown in the example, is defined for list objects; it adds a new element
946at the end of the list.
947In this case it is equivalent to
948{\tt ret = ret + [b]},
949but more efficient.%
950\footnote{
951 There is a subtle semantic difference if the object
952 is referenced from more than one place.
953}
954\end{itemize}
955The list object type has two more methods:
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000956\begin{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000957\item[{\tt insert(i, x)}]
958Inserts an item at a given position.
959The first argument is the index of the element before which to insert,
960so {\tt a.insert(0, x)} inserts at the front of the list, and
961{\tt a.insert(len(a), x)} is equivalent to {\tt a.append(x)}.
962\item[{\tt sort()}]
963Sorts the elements of the list.
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000964\end{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000965For example:
966\begin{code}\begin{verbatim}
967>>> a = [10, 100, 1, 1000]
968>>> a.insert(2, -1)
969>>> a
970[10, 100, -1, 1, 1000]
971>>> a.sort()
972>>> a
973[-1, 1, 10, 100, 1000]
974>>> # Strings are sorted according to ASCII:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000975>>> b = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000976>>> b.sort()
977>>> b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000978['Mary', 'a', 'boy', 'had', 'little']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000979>>>
980\end{verbatim}\end{code}
981
982\subsection{Modules}
983
984If you quit from the \Python\ interpreter and enter it again, the
985definitions you have made (functions and variables) are lost.
986Therefore, if you want to write a somewhat longer program, you are
987better off using a text editor to prepare the input for the interpreter
988and run it with that file as input instead.
989This is known as creating a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000990{\em script}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000991As your program gets longer, you may want to split it into several files
992for easier maintenance.
993You may also want to use a handy function that you've written in several
994programs without copying its definition into each program.
995To support this, \Python\ has a way to put definitions in a file and use
996them in a script or in an interactive instance of the interpreter.
997Such a file is called a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000998{\em module};
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000999definitions from a module can be
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001000{\em imported}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001001into other modules or into the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001002{\em main}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001003module (the collection of variables that you have access to in
1004a script and in calculator mode).
1005
1006A module is a file containing \Python\ definitions and statements.
1007The file name is the module name with the suffix
1008{\tt .py}
1009appended.
1010For instance, use your favorite text editor to create a file called
1011{\tt fibo.py}
1012in the current directory with the following contents:
1013\begin{code}\begin{verbatim}
1014# Fibonacci numbers module
1015
1016def fib(n): # write Fibonacci series up to n
1017 a, b = 0, 1
1018 while b <= n:
1019 print b,
1020 a, b = b, a+b
1021
1022def fib2(n): # return Fibonacci series up to n
1023 ret = []
1024 a, b = 0, 1
1025 while b <= n:
1026 ret.append(b)
1027 a, b = b, a+b
1028 return ret
1029\end{verbatim}\end{code}
1030Now enter the \Python\ interpreter and import this module with the
1031following command:
1032\begin{code}\begin{verbatim}
1033>>> import fibo
1034>>>
1035\end{verbatim}\end{code}
1036This does not enter the names of the functions defined in
1037{\tt fibo}
1038directly in the symbol table; it only enters the module name
1039{\tt fibo}
1040there.
1041Using the module name you can access the functions:
1042\begin{code}\begin{verbatim}
1043>>> fibo.fib(1000)
10441 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1045>>> fibo.fib2(100)
1046[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1047>>>
1048\end{verbatim}\end{code}
1049If you intend to use a function often you can assign it to a local name:
1050\begin{code}\begin{verbatim}
1051>>> fib = fibo.fib
1052>>> fib(500)
10531 1 2 3 5 8 13 21 34 55 89 144 233 377
1054>>>
1055\end{verbatim}\end{code}
1056
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001057\subsubsection{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001058
1059A module can contain executable statements as well as function
1060definitions.
1061These statements are intended to initialize the module.
1062They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001063{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001064time the module is imported somewhere.%
1065\footnote{
1066 In fact function definitions are also `statements' that are
1067 `executed'; the execution enters the function name in the
1068 module's global symbol table.
1069}
1070
1071Each module has its own private symbol table, which is used as the
1072global symbol table by all functions defined in the module.
1073Thus, the author of a module can use global variables in the module
1074without worrying about accidental clashes with a user's global
1075variables.
1076On the other hand, if you know what you are doing you can touch a
1077module's global variables with the same notation used to refer to its
1078functions,
1079{\tt modname.itemname}.
1080
1081Modules can import other modules.
1082It is customary but not required to place all
1083{\tt import}
1084statements at the beginning of a module (or script, for that matter).
1085The imported module names are placed in the importing module's global
1086symbol table.
1087
1088There is a variant of the
1089{\tt import}
1090statement that imports names from a module directly into the importing
1091module's symbol table.
1092For example:
1093\begin{code}\begin{verbatim}
1094>>> from fibo import fib, fib2
1095>>> fib(500)
10961 1 2 3 5 8 13 21 34 55 89 144 233 377
1097>>>
1098\end{verbatim}\end{code}
1099This does not introduce the module name from which the imports are taken
1100in the local symbol table (so in the example, {\tt fibo} is not
1101defined).
1102
1103There is even a variant to import all names that a module defines:
1104\begin{code}\begin{verbatim}
1105>>> from fibo import *
1106>>> fib(500)
11071 1 2 3 5 8 13 21 34 55 89 144 233 377
1108>>>
1109\end{verbatim}\end{code}
1110This imports all names except those beginning with an underscore
1111({\tt \_}).
1112
1113\subsubsection{Standard Modules}
1114
1115\Python\ comes with a library of standard modules, described in a separate
1116document (Python Library and Module Reference).
1117Some modules are built into the interpreter; these provide access to
1118operations that are not part of the core of the language but are
1119nevertheless built in, either for efficiency or to provide access to
1120operating system primitives such as system calls.
1121The set of such modules is a configuration option; e.g., the
1122{\tt amoeba}
1123module is only provided on systems that somehow support Amoeba
1124primitives.
1125One particular module deserves some attention:
1126{\tt sys},
1127which is built into every \Python\ interpreter.
1128The variables
1129{\tt sys.ps1}
1130and
1131{\tt sys.ps2}
1132define the strings used as primary and secondary prompts:
1133\begin{code}\begin{verbatim}
1134>>> import sys
1135>>> sys.ps1
1136'>>> '
1137>>> sys.ps2
1138'... '
1139>>> sys.ps1 = 'C> '
1140C> print 'Yuck!'
1141Yuck!
1142C>
1143\end{verbatim}\end{code}
1144These two variables are only defined if the interpreter is in
1145interactive mode.
1146
1147The variable
1148{\tt sys.path}
1149is a list of strings that determine the interpreter's search path for
1150modules.
1151It is initialized to a default path taken from the environment variable
1152{\tt PYTHONPATH},
1153or from a built-in default if
1154{\tt PYTHONPATH}
1155is not set.
1156You can modify it using standard list operations, e.g.:
1157\begin{code}\begin{verbatim}
1158>>> import sys
1159>>> sys.path.append('/ufs/guido/lib/python')
1160>>>
1161\end{verbatim}\end{code}
1162
1163\subsection{Errors and Exceptions}
1164
1165Until now error messages haven't yet been mentioned, but if you have
1166tried out the examples you have probably seen some.
1167There are (at least) two distinguishable kinds of errors:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001168{\em syntax\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001169and
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001170{\em exceptions}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001171
1172\subsubsection{Syntax Errors}
1173
1174Syntax errors, also known as parsing errors, are perhaps the most common
1175kind of complaint you get while you are still learning \Python:
1176\begin{code}\begin{verbatim}
1177>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001178Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001179while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001180 ^
1181Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001182>>>
1183\end{verbatim}\end{code}
1184The parser repeats the offending line and displays a little `arrow'
1185pointing at the earliest point in the line where the error was detected.
1186The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001187{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001188the arrow: in the example, the error is detected at the keyword
1189{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001190File name and line number are printed so you know where to look in case
1191the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001192
1193\subsubsection{Exceptions}
1194
1195Even if a statement or expression is syntactically correct, it may cause
1196an error when an attempt is made to execute it:
1197\begin{code}\begin{verbatim}
1198>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001199Unhandled exception: run-time error: integer division by zero
1200Stack backtrace (innermost last):
1201 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001202>>> 4 + foo*3
1203Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001204Stack backtrace (innermost last):
1205 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001206>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001207Unhandled exception: type error: illegal argument type for built-in operation
1208Stack backtrace (innermost last):
1209 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001210>>>
1211\end{verbatim}\end{code}
1212Errors detected during execution are called
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001213{\em exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001214and are not unconditionally fatal: you will soon learn how to handle
1215them in \Python\ programs.
1216Most exceptions are not handled by programs, however, and result
1217in error messages as shown here.
1218
1219The first line of the error message indicates what happened.
1220Exceptions come in different types, and the type is printed as part of
1221the message: the types in the example are
1222{\tt run-time error},
1223{\tt undefined name}
1224and
1225{\tt type error}.
1226The rest of the line is a detail whose interpretation depends on the
1227exception type.
1228
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001229The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001230exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001231In general it contains a stack backtrace listing source lines; however,
1232it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001233
1234Here is a summary of the most common exceptions:
1235\begin{itemize}
1236\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001237{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001238are generally caused by wrong data used by the program; this can be the
1239programmer's fault or caused by bad input.
1240The detail states the cause of the error in more detail.
1241\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001242{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001243errors are more serious: these are usually caused by misspelled
1244identifiers.%
1245\footnote{
1246 The parser does not check whether names used in a program are at
1247 all defined elsewhere in the program, so such checks are
1248 postponed until run-time. The same holds for type checking.
1249}
1250The detail is the offending identifier.
1251\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001252{\em Type\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001253are also pretty serious: this is another case of using wrong data (or
1254better, using data the wrong way), but here the error can be glanced
1255from the object type(s) alone.
1256The detail shows in what context the error was detected.
1257\end{itemize}
1258
1259\subsubsection{Handling Exceptions}
1260
1261It is possible to write programs that handle selected exceptions.
1262Look at the following example, which prints a table of inverses of
1263some floating point numbers:
1264\begin{code}\begin{verbatim}
1265>>> numbers = [0.3333, 2.5, 0.0, 10.0]
1266>>> for x in numbers:
1267... print x,
1268... try:
1269... print 1.0 / x
1270... except RuntimeError:
1271... print '*** has no inverse ***'
1272...
12730.3333 3.00030003
12742.5 0.4
12750 *** has no inverse ***
127610 0.1
1277>>>
1278\end{verbatim}\end{code}
1279The {\tt try} statement works as follows.
1280\begin{itemize}
1281\item
1282First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001283{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001284(the statement(s) between the {\tt try} and {\tt except} keywords) is
1285executed.
1286\item
1287If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001288{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001289is skipped and execution of the {\tt try} statement is finished.
1290\item
1291If an exception occurs during execution of the try clause, and its
1292type matches the exception named after the {\tt except} keyword, the
1293rest of the try clause is skipped, the except clause is executed, and
1294then execution continues after the {\tt try} statement.
1295\item
1296If an exception occurs which does not match the exception named in the
1297except clause, it is passed on to outer try statements; if no handler is
1298found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001299{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001300and execution stops with a message as shown above.
1301\end{itemize}
1302A {\tt try} statement may have more than one except clause, to specify
1303handlers for different exceptions.
1304At most one handler will be executed.
1305Handlers only handle exceptions that occur in the corresponding try
1306clause, not in other handlers of the same {\tt try} statement.
1307An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001308e.g.:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001309\begin{code}\begin{verbatim}
1310... except (RuntimeError, TypeError, NameError):
1311... pass
1312\end{verbatim}\end{code}
1313The last except clause may omit the exception name(s), to serve as a
1314wildcard.
1315Use this with extreme caution!
1316
1317When an exception occurs, it may have an associated value, also known as
1318the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001319{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001320The presence and type of the argument depend on the exception type.
1321For exception types which have an argument, the except clause may
1322specify a variable after the exception name (or list) to receive the
1323argument's value, as follows:
1324\begin{code}\begin{verbatim}
1325>>> try:
1326... foo()
1327... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001328... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001329...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001330name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001331>>>
1332\end{verbatim}\end{code}
1333If an exception has an argument, it is printed as the third part
1334(`detail') of the message for unhandled exceptions.
1335
1336Standard exception names are built-in identifiers (not reserved
1337keywords).
1338These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001339{\em object\ identity}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001340(not their value!) identifies the exceptions.%
1341\footnote{
1342 There should really be a separate exception type; it is pure
1343 laziness that exceptions are identified by strings, and this may
1344 be fixed in the future.
1345}
1346The string is printed as the second part of the message for unhandled
1347exceptions.
1348Their names and values are:
1349\begin{code}\begin{verbatim}
1350EOFError 'end-of-file read'
1351KeyboardInterrupt 'keyboard interrupt'
1352MemoryError 'out of memory' *
1353NameError 'undefined name' *
1354RuntimeError 'run-time error' *
1355SystemError 'system error' *
1356TypeError 'type error' *
1357\end{verbatim}\end{code}
1358The meanings should be clear enough.
1359Those exceptions with a {\tt *} in the third column have an argument.
1360
1361Exception handlers don't just handle exceptions if they occur
1362immediately in the try clause, but also if they occur inside functions
1363that are called (even indirectly) in the try clause.
1364For example:
1365\begin{code}\begin{verbatim}
1366>>> def this_fails():
1367... x = 1/0
1368...
1369>>> try:
1370... this_fails()
1371... except RuntimeError, detail:
1372... print 'Handling run-time error:', detail
1373...
1374Handling run-time error: domain error or zero division
1375>>>
1376\end{verbatim}\end{code}
1377
1378\subsubsection{Raising Exceptions}
1379
1380The {\tt raise} statement allows the programmer to force a specified
1381exception to occur.
1382For example:
1383\begin{code}\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001384>>> raise NameError, 'Hi There!'
1385Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001386Stack backtrace (innermost last):
1387 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001388>>>
1389\end{verbatim}\end{code}
1390The first argument to {\tt raise} names the exception to be raised.
1391The optional second argument specifies the exception's argument.
1392
1393\subsubsection{User-defined Exceptions}
1394
1395Programs may name their own exceptions by assigning a string to a
1396variable.
1397For example:
1398\begin{code}\begin{verbatim}
1399>>> my_exc = 'nobody likes me!'
1400>>> try:
1401... raise my_exc, 2*2
1402... except my_exc, val:
1403... print 'My exception occured, value:', val
1404...
1405My exception occured, value: 4
1406>>> raise my_exc, 1
1407Unhandled exception: nobody likes me!: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001408Stack backtrace (innermost last):
1409 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001410>>>
1411\end{verbatim}\end{code}
1412Many standard modules use this to report errors that may occur in
1413functions they define.
1414
1415\subsubsection{Defining Clean-up Actions}
1416
1417The {\tt try} statement has another optional clause which is intended to
1418define clean-up actions that must be executed under all circumstances.
1419For example:
1420\begin{code}\begin{verbatim}
1421>>> try:
1422... raise KeyboardInterrupt
1423... finally:
1424... print 'Goodbye, world!'
1425...
1426Goodbye, world!
1427Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001428Stack backtrace (innermost last):
1429 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001430>>>
1431\end{verbatim}\end{code}
1432The
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001433{\em finally\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001434must follow the except clauses(s), if any.
1435It is executed whether or not an exception occurred.
1436If the exception is handled, the finally clause is executed after the
1437handler (and even if another exception occurred in the handler).
1438It is also executed when the {\tt try} statement is left via a
1439{\tt break} or {\tt return} statement.
1440
1441\subsection{Classes}
1442
1443Classes in \Python\ make it possible to play the game of encapsulation in a
1444somewhat different way than it is played with modules.
1445Classes are an advanced topic and are probably best skipped on the first
1446encounter with \Python.
1447
1448\subsubsection{Prologue}
1449
1450\Python's class mechanism is not particularly elegant, but quite powerful.
1451It is a mixture of the class mechanisms found in C++ and Modula-3.
1452As is true for modules, classes in \Python\ do not put an absolute barrier
1453between definition and user, but rather rely on the politeness of the
1454user not to ``break into the definition.''
1455The most important features of classes are retained with full power,
1456however: the class inheritance mechanism allows multiple base classes,
1457a derived class can override any method of its base class(es), a method
1458can call the method of a base class with the same name.
1459Objects can contain an arbitrary amount of private data.
1460
1461In C++ terminology, all class members (including data members) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001462{\em public},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001463and all member functions (methods) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001464{\em virtual}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001465There are no special constructors or destructors.
1466As in Modula-3, there are no shorthands for referencing the object's
1467members from its methods: the method function is declared with an
1468explicit first argument representing the object, which is provided
1469implicitly by the call.
1470As in Smalltalk, classes themselves are objects, albeit in the wider
1471sense of the word: in \Python, all data types are objects.
1472This provides semantics for renaming or aliasing.
1473But, just like in C++ or Modula-3, the built-in types cannot be used as
1474base classes for extension by the user.
1475Also, like Modula-3 but unlike C++, the built-in operators with special
1476syntax (arithmetic operators, subscripting etc.) cannot be redefined for
1477class members.%
1478\footnote{
1479 They can be redefined for new object types implemented in C in
1480 extensions to the interpreter, however. It would require only a
1481 naming convention and a relatively small change to the
1482 interpreter to allow operator overloading for classes, so
1483 perhaps someday...
1484}
1485
1486\subsubsection{A Simple Example}
1487
1488Consider the following example, which defines a class {\tt Set}
1489representing a (finite) mathematical set with operations to add and
1490remove elements, a membership test, and a request for the size of the
1491set.
1492\begin{code}\begin{verbatim}
1493class Set():
1494 def new(self):
1495 self.elements = []
1496 return self
1497 def add(self, e):
1498 if e not in self.elements:
1499 self.elements.append(e)
1500 def remove(self, e):
1501 if e in self.elements:
1502 for i in range(len(self.elements)):
1503 if self.elements[i] = e:
1504 del self.elements[i]
1505 break
1506 def is_element(self, e):
1507 return e in self.elements
1508 def size(self):
1509 return len(self.elements)
1510\end{verbatim}\end{code}
1511Note that the class definition looks like a big compound statement,
1512with all the function definitons indented repective to the
1513{\tt class}
1514keyword.
1515
1516Let's assume that this
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001517{\em class\ definition}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001518is the only contents of the module file
1519{\tt SetClass.py}.
1520We can then use it in a \Python\ program as follows:
1521\begin{code}\begin{verbatim}
1522>>> from SetClass import Set
1523>>> a = Set().new() # create a Set object
1524>>> a.add(2)
1525>>> a.add(3)
1526>>> a.add(1)
1527>>> a.add(1)
1528>>> if a.is_element(3): print '3 is in the set'
1529...
15303 is in the set
1531>>> if not a.is_element(4): print '4 is not in the set'
1532...
15334 is not in the set
1534>>> print 'a has', a.size(), 'elements'
1535a has 3 elements
1536>>> a.remove(1)
1537>>> print 'now a has', a.size(), 'elements'
1538>>>
1539now a has 2 elements
1540>>>
1541\end{verbatim}\end{code}
1542From the example we learn in the first place that the functions defined
1543in the class (e.g.,
1544{\tt add})
1545can be called using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001546{\em member}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001547notation for the object
1548{\tt a}.
1549The member function is called with one less argument than it is defined:
1550the object is implicitly passed as the first argument.
1551Thus, the call
1552{\tt a.add(2)}
1553is equivalent to
1554{\tt Set.add(a, 2)}.
1555
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001556XXX This section is not complete yet!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001557
1558\section{XXX P.M.}
1559
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001560\begin{itemize}
1561\item The {\tt del} statement.
1562\item The {\tt dir()} function.
1563\item Tuples.
1564\item Dictionaries.
1565\item Objects and types in general.
1566\item Backquotes.
1567\item And/Or/Not.
1568\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001569
1570\end{document}