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