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