Guido van Rossum | 3795378 | 1992-04-06 14:04:04 +0000 | [diff] [blame] | 1 | \documentstyle[twoside,11pt,myformat]{report} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 3 | \title{Python Tutorial} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 5 | \input{boilerplate} |
Guido van Rossum | 83eb962 | 1993-11-23 16:28:45 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 7 | \begin{document} |
| 8 | |
| 9 | \pagenumbering{roman} |
| 10 | |
| 11 | \maketitle |
| 12 | |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 13 | \input{copyright} |
| 14 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 15 | \begin{abstract} |
| 16 | |
| 17 | \noindent |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 18 | Python is a simple, yet powerful programming language that bridges the |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 19 | gap between C and shell programming, and is thus ideally suited for |
| 20 | ``throw-away programming'' |
| 21 | and rapid prototyping. Its syntax is put |
| 22 | together from constructs borrowed from a variety of other languages; |
| 23 | most prominent are influences from ABC, C, Modula-3 and Icon. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 25 | The Python interpreter is easily extended with new functions and data |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 26 | types implemented in C. Python is also suitable as an extension |
| 27 | language for highly customizable C applications such as editors or |
| 28 | window managers. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 30 | Python is available for various operating systems, amongst which |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 31 | several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S., |
| 32 | and MS-DOS. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 34 | This tutorial introduces the reader informally to the basic concepts |
| 35 | and features of the Python language and system. It helps to have a |
| 36 | Python interpreter handy for hands-on experience, but as the examples |
| 37 | are self-contained, the tutorial can be read off-line as well. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 481ae68 | 1991-11-25 17:28:03 +0000 | [diff] [blame] | 39 | For a description of standard objects and modules, see the {\em Python |
| 40 | Library Reference} document. The {\em Python Reference Manual} gives |
| 41 | a more formal definition of the language. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 42 | |
| 43 | \end{abstract} |
| 44 | |
| 45 | \pagebreak |
Guido van Rossum | cdc9355 | 1992-02-11 15:53:13 +0000 | [diff] [blame] | 46 | { |
| 47 | \parskip = 0mm |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 48 | \tableofcontents |
Guido van Rossum | cdc9355 | 1992-02-11 15:53:13 +0000 | [diff] [blame] | 49 | } |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 50 | |
| 51 | \pagebreak |
| 52 | |
| 53 | \pagenumbering{arabic} |
| 54 | |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 56 | \chapter{Whetting Your Appetite} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 58 | If you ever wrote a large shell script, you probably know this |
| 59 | feeling: you'd love to add yet another feature, but it's already so |
| 60 | slow, and so big, and so complicated; or the feature involves a system |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 61 | call or other function that is only accessible from C \ldots Usually |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 62 | the problem at hand isn't serious enough to warrant rewriting the |
| 63 | script in C; perhaps because the problem requires variable-length |
| 64 | strings or other data types (like sorted lists of file names) that are |
| 65 | easy in the shell but lots of work to implement in C; or perhaps just |
| 66 | because you're not sufficiently familiar with C. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 68 | In such cases, Python may be just the language for you. Python is |
| 69 | simple to use, but it is a real programming language, offering much |
| 70 | more structure and support for large programs than the shell has. On |
| 71 | the other hand, it also offers much more error checking than C, and, |
| 72 | being a {\em very-high-level language}, it has high-level data types |
| 73 | built in, such as flexible arrays and dictionaries that would cost you |
| 74 | days to implement efficiently in C. Because of its more general data |
| 75 | types Python is applicable to a much larger problem domain than {\em |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 76 | Awk} or even {\em Perl}, yet many things are at least as easy in |
| 77 | Python as in those languages. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 79 | Python allows you to split up your program in modules that can be |
| 80 | reused in other Python programs. It comes with a large collection of |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 81 | standard modules that you can use as the basis of your programs --- or |
| 82 | as examples to start learning to program in Python. There are also |
| 83 | built-in modules that provide things like file I/O, system calls, |
| 84 | sockets, and even a generic interface to window systems (STDWIN). |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 86 | Python is an interpreted language, which can save you considerable time |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 87 | during program development because no compilation and linking is |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 88 | necessary. The interpreter can be used interactively, which makes it |
| 89 | easy to experiment with features of the language, to write throw-away |
| 90 | programs, or to test functions during bottom-up program development. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 91 | It is also a handy desk calculator. |
| 92 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 93 | Python allows writing very compact and readable programs. Programs |
| 94 | written in Python are typically much shorter than equivalent C |
| 95 | programs, for several reasons: |
| 96 | \begin{itemize} |
| 97 | \item |
| 98 | the high-level data types allow you to express complex operations in a |
| 99 | single statement; |
| 100 | \item |
| 101 | statement grouping is done by indentation instead of begin/end |
| 102 | brackets; |
| 103 | \item |
| 104 | no variable or argument declarations are necessary. |
| 105 | \end{itemize} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 107 | Python is {\em extensible}: if you know how to program in C it is easy |
| 108 | to add a new built-in |
| 109 | function or |
| 110 | module to the interpreter, either to |
| 111 | perform critical operations at maximum speed, or to link Python |
| 112 | programs to libraries that may only be available in binary form (such |
| 113 | as a vendor-specific graphics library). Once you are really hooked, |
| 114 | you can link the Python interpreter into an application written in C |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 115 | and use it as an extension or command language for that application. |
| 116 | |
| 117 | By the way, the language is named after the BBC show ``Monty |
| 118 | Python's Flying Circus'' and has nothing to do with nasty reptiles... |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 120 | \section{Where From Here} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 122 | Now that you are all excited about Python, you'll want to examine it |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 123 | in some more detail. Since the best way to learn a language is |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 124 | using it, you are invited here to do so. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 126 | In the next chapter, the mechanics of using the interpreter are |
| 127 | explained. This is rather mundane information, but essential for |
| 128 | trying out the examples shown later. |
| 129 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 130 | The rest of the tutorial introduces various features of the Python |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 131 | language and system though examples, beginning with simple |
| 132 | expressions, statements and data types, through functions and modules, |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 133 | and finally touching upon advanced concepts like exceptions |
| 134 | and user-defined classes. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 136 | When you're through with the tutorial (or just getting bored), you |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 137 | should read the Library Reference, which gives complete (though terse) |
| 138 | reference material about built-in and standard types, functions and |
| 139 | modules that can save you a lot of time when writing Python programs. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 142 | \chapter{Using the Python Interpreter} |
| 143 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 144 | \section{Invoking the Interpreter} |
| 145 | |
Guido van Rossum | 9a4e3fc | 1992-09-03 21:27:55 +0000 | [diff] [blame] | 146 | The Python interpreter is usually installed as {\tt /usr/local/bin/python} |
| 147 | on those machines where it is available; putting {\tt /usr/local/bin} in |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 148 | 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] | 149 | typing the command |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 151 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 152 | python |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 153 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 154 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 155 | to the shell. Since the choice of the directory where the interpreter |
| 156 | lives is an installation option, other places are possible; check with |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 157 | your local Python guru or system administrator. (E.g., {\tt |
Guido van Rossum | 9a4e3fc | 1992-09-03 21:27:55 +0000 | [diff] [blame] | 158 | /usr/local/python} is a popular alternative location.) |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 160 | The interpreter operates somewhat like the {\UNIX} shell: when called |
| 161 | with standard input connected to a tty device, it reads and executes |
| 162 | commands interactively; when called with a file name argument or with |
| 163 | a file as standard input, it reads and executes a {\em script} from |
| 164 | that file. |
| 165 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 166 | A third way of starting the interpreter is |
| 167 | ``{\tt python -c command [arg] ...}'', which |
| 168 | executes the statement(s) in {\tt command}, analogous to the shell's |
| 169 | {\tt -c} option. Since Python statements often contain spaces or other |
| 170 | characters that are special to the shell, it is best to quote {\tt |
| 171 | command} in its entirety with double quotes. |
| 172 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 173 | Note that there is a difference between ``{\tt python file}'' and |
| 174 | ``{\tt python $<$file}''. In the latter case, input requests from the |
Guido van Rossum | 573805a | 1992-03-06 10:56:03 +0000 | [diff] [blame] | 175 | program, such as calls to {\tt input()} and {\tt raw_input()}, are |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 176 | satisfied from {\em file}. Since this file has already been read |
| 177 | until the end by the parser before the program starts executing, the |
| 178 | program will encounter EOF immediately. In the former case (which is |
| 179 | usually what you want) they are satisfied from whatever file or device |
| 180 | is connected to standard input of the Python interpreter. |
| 181 | |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 182 | When a script file is used, it is sometimes useful to be able to run |
| 183 | the script and enter interactive mode afterwards. This can be done by |
| 184 | passing {\tt -i} before the script. (This does not work if the script |
| 185 | is read from standard input, for the same reason as explained in the |
| 186 | previous paragraph.) |
| 187 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 188 | \subsection{Argument Passing} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 190 | When known to the interpreter, the script name and additional |
| 191 | arguments thereafter are passed to the script in the variable {\tt |
| 192 | sys.argv}, which is a list of strings. Its length is at least one; |
| 193 | when no script and no arguments are given, {\tt sys.argv[0]} is an |
| 194 | empty string. When the script name is given as {\tt '-'} (meaning |
| 195 | standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c |
| 196 | command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options |
| 197 | found after {\tt -c command} are not consumed by the Python |
| 198 | interpreter's option processing but left in {\tt sys.argv} for the |
| 199 | command to handle. |
| 200 | |
| 201 | \subsection{Interactive Mode} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | dd01080 | 1991-06-07 14:31:11 +0000 | [diff] [blame] | 203 | When commands are read from a tty, the interpreter is said to be in |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 204 | {\em interactive\ mode}. In this mode it prompts for the next command |
| 205 | with the {\em primary\ prompt}, usually three greater-than signs ({\tt |
Guido van Rossum | a67dee3 | 1995-09-13 17:34:25 +0000 | [diff] [blame] | 206 | >>>}); for continuation lines it prompts with the |
| 207 | {\em secondary\ prompt}, |
Guido van Rossum | 34e1777 | 1996-06-10 19:44:49 +0000 | [diff] [blame] | 208 | by default three dots ({\tt ...}). Typing an EOF character |
| 209 | (Control-D on {\UNIX}, Control-Z on DOS or Windows) |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 210 | at the primary prompt causes the interpreter to exit with a zero exit |
| 211 | status. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 213 | The interpreter prints a welcome message stating its version number |
| 214 | and a copyright notice before printing the first prompt, e.g.: |
| 215 | |
| 216 | \bcode\begin{verbatim} |
| 217 | python |
Guido van Rossum | 97662c8 | 1996-08-23 15:35:47 +0000 | [diff] [blame] | 218 | Python 1.4b3 (Aug 25 1996) [GCC 2.7.0] |
| 219 | Copyright 1991-1996 Stichting Mathematisch Centrum, Amsterdam |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 220 | >>> |
| 221 | \end{verbatim}\ecode |
| 222 | |
| 223 | \section{The Interpreter and its Environment} |
| 224 | |
| 225 | \subsection{Error Handling} |
| 226 | |
| 227 | When an error occurs, the interpreter prints an error |
| 228 | message and a stack trace. In interactive mode, it then returns to |
| 229 | the primary prompt; when input came from a file, it exits with a |
| 230 | nonzero exit status after printing |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 231 | the stack trace. (Exceptions handled by an {\tt except} clause in a |
| 232 | {\tt try} statement are not errors in this context.) Some errors are |
| 233 | unconditionally fatal and cause an exit with a nonzero exit; this |
| 234 | applies to internal inconsistencies and some cases of running out of |
| 235 | memory. All error messages are written to the standard error stream; |
| 236 | normal output from the executed commands is written to standard |
| 237 | output. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 239 | Typing the interrupt character (usually Control-C or DEL) to the |
| 240 | primary or secondary prompt cancels the input and returns to the |
| 241 | primary prompt.% |
| 242 | \footnote{ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 243 | A problem with the GNU Readline package may prevent this. |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 244 | } |
| 245 | Typing an interrupt while a command is executing raises the {\tt |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 246 | KeyboardInterrupt} exception, which may be handled by a {\tt try} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 247 | statement. |
| 248 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 249 | \subsection{The Module Search Path} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 251 | When a module named {\tt spam} is imported, the interpreter searches |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 252 | for a file named {\tt spam.py} in the current directory, |
| 253 | and then in the list of directories specified by |
| 254 | the environment variable {\tt PYTHONPATH}. This has the same syntax as |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 255 | the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated |
Guido van Rossum | 9a4e3fc | 1992-09-03 21:27:55 +0000 | [diff] [blame] | 256 | directory names. When {\tt PYTHONPATH} is not set, or when the file |
| 257 | is not found there, the search continues in an installation-dependent |
| 258 | default path, usually {\tt .:/usr/local/lib/python}. |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 260 | Actually, modules are searched in the list of directories given by the |
| 261 | variable {\tt sys.path} which is initialized from the directory |
| 262 | containing the input script (or the current directory), {\tt |
| 263 | PYTHONPATH} and the installation-dependent default. This allows |
| 264 | Python programs that know what they're doing to modify or replace the |
| 265 | module search path. See the section on Standard Modules later. |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 266 | |
| 267 | \subsection{``Compiled'' Python files} |
| 268 | |
| 269 | As an important speed-up of the start-up time for short programs that |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 270 | use a lot of standard modules, if a file called {\tt spam.pyc} exists |
| 271 | in the directory where {\tt spam.py} is found, this is assumed to |
| 272 | contain an already-``compiled'' version of the module {\tt spam}. The |
| 273 | modification time of the version of {\tt spam.py} used to create {\tt |
| 274 | spam.pyc} is recorded in {\tt spam.pyc}, and the file is ignored if |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 275 | these don't match. |
| 276 | |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 277 | Normally, you don't need to do anything to create the {\tt spam.pyc} file. |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 278 | Whenever {\tt spam.py} is successfully compiled, an attempt is made to |
| 279 | write the compiled version to {\tt spam.pyc}. It is not an error if |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 280 | this attempt fails; if for any reason the file is not written |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 281 | completely, the resulting {\tt spam.pyc} file will be recognized as |
Guido van Rossum | 34e1777 | 1996-06-10 19:44:49 +0000 | [diff] [blame] | 282 | invalid and thus ignored later. The contents of the {\tt spam.pyc} |
| 283 | file is platform independent, so a Python module directory can be |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 284 | shared by machines of different architectures. (Tip for experts: |
| 285 | the module {\tt compileall} creates {\tt .pyc} files for all modules.) |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 286 | |
| 287 | \subsection{Executable Python scripts} |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 289 | On BSD'ish {\UNIX} systems, Python scripts can be made directly |
| 290 | executable, like shell scripts, by putting the line |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 292 | \bcode\begin{verbatim} |
Guido van Rossum | 9a4e3fc | 1992-09-03 21:27:55 +0000 | [diff] [blame] | 293 | #! /usr/local/bin/python |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 294 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 295 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 296 | (assuming that's the name of the interpreter) at the beginning of the |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 297 | script and giving the file an executable mode. The {\tt \#!} must be |
| 298 | the first two characters of the file. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | 9a4e3fc | 1992-09-03 21:27:55 +0000 | [diff] [blame] | 300 | \subsection{The Interactive Startup File} |
| 301 | |
| 302 | When you use Python interactively, it is frequently handy to have some |
| 303 | standard commands executed every time the interpreter is started. You |
| 304 | can do this by setting an environment variable named {\tt |
| 305 | PYTHONSTARTUP} to the name of a file containing your start-up |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 306 | commands. This is similar to the {\tt .profile} feature of the UNIX |
Guido van Rossum | 9a4e3fc | 1992-09-03 21:27:55 +0000 | [diff] [blame] | 307 | shells. |
| 308 | |
| 309 | This file is only read in interactive sessions, not when Python reads |
| 310 | commands from a script, and not when {\tt /dev/tty} is given as the |
| 311 | explicit source of commands (which otherwise behaves like an |
| 312 | interactive session). It is executed in the same name space where |
| 313 | interactive commands are executed, so that objects that it defines or |
| 314 | imports can be used without qualification in the interactive session. |
Guido van Rossum | 7b3c8a1 | 1992-09-08 09:20:13 +0000 | [diff] [blame] | 315 | You can also change the prompts {\tt sys.ps1} and {\tt sys.ps2} in |
| 316 | this file. |
Guido van Rossum | 9a4e3fc | 1992-09-03 21:27:55 +0000 | [diff] [blame] | 317 | |
| 318 | If you want to read an additional start-up file from the current |
| 319 | directory, you can program this in the global start-up file, e.g. |
| 320 | \verb\execfile('.pythonrc')\. If you want to use the startup file |
| 321 | in a script, you must write this explicitly in the script, e.g. |
| 322 | \verb\import os;\ \verb\execfile(os.environ['PYTHONSTARTUP'])\. |
| 323 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 324 | \section{Interactive Input Editing and History Substitution} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 326 | Some versions of the Python interpreter support editing of the current |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 327 | input line and history substitution, similar to facilities found in |
| 328 | the Korn shell and the GNU Bash shell. This is implemented using the |
| 329 | {\em GNU\ Readline} library, which supports Emacs-style and vi-style |
| 330 | editing. This library has its own documentation which I won't |
| 331 | duplicate here; however, the basics are easily explained. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 333 | Perhaps the quickest check to see whether command line editing is |
| 334 | supported is typing Control-P to the first Python prompt you get. If |
| 335 | it beeps, you have command line editing. If nothing appears to |
| 336 | happen, or if \verb/^P/ is echoed, you can skip the rest of this |
| 337 | section. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 339 | \subsection{Line Editing} |
| 340 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 341 | If supported, input line editing is active whenever the interpreter |
| 342 | prints a primary or secondary prompt. The current line can be edited |
| 343 | using the conventional Emacs control characters. The most important |
| 344 | of these are: C-A (Control-A) moves the cursor to the beginning of the |
| 345 | line, C-E to the end, C-B moves it one position to the left, C-F to |
| 346 | the right. Backspace erases the character to the left of the cursor, |
| 347 | C-D the character to its right. C-K kills (erases) the rest of the |
| 348 | line to the right of the cursor, C-Y yanks back the last killed |
| 349 | string. C-underscore undoes the last change you made; it can be |
| 350 | repeated for cumulative effect. |
| 351 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 352 | \subsection{History Substitution} |
| 353 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 354 | History substitution works as follows. All non-empty input lines |
| 355 | issued are saved in a history buffer, and when a new prompt is given |
| 356 | you are positioned on a new line at the bottom of this buffer. C-P |
| 357 | 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] | 358 | Any line in the history buffer can be edited; an asterisk appears in |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 359 | front of the prompt to mark a line as modified. Pressing the Return |
| 360 | key passes the current line to the interpreter. C-R starts an |
| 361 | incremental reverse search; C-S starts a forward search. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 362 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 363 | \subsection{Key Bindings} |
| 364 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 365 | The key bindings and some other parameters of the Readline library can |
| 366 | be customized by placing commands in an initialization file called |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 367 | {\tt \$HOME/.inputrc}. Key bindings have the form |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 369 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 370 | key-name: function-name |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 371 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 372 | % |
| 373 | or |
| 374 | |
| 375 | \bcode\begin{verbatim} |
| 376 | "string": function-name |
| 377 | \end{verbatim}\ecode |
| 378 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 379 | and options can be set with |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 380 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 381 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 382 | set option-name value |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 383 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 384 | % |
| 385 | For example: |
| 386 | |
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 | # I prefer vi-style editing: |
| 389 | set editing-mode vi |
| 390 | # Edit using a single line: |
| 391 | set horizontal-scroll-mode On |
| 392 | # Rebind some keys: |
| 393 | Meta-h: backward-kill-word |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 394 | "\C-u": universal-argument |
| 395 | "\C-x\C-r": re-read-init-file |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 396 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 397 | % |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 398 | Note that the default binding for TAB in Python is to insert a TAB |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 399 | instead of Readline's default filename completion function. If you |
| 400 | insist, you can override this by putting |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 402 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 403 | TAB: complete |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 404 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 405 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 406 | in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 407 | indented continuation lines...) |
| 408 | |
| 409 | \subsection{Commentary} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 410 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 411 | This facility is an enormous step forward compared to previous |
| 412 | versions of the interpreter; however, some wishes are left: It would |
| 413 | be nice if the proper indentation were suggested on continuation lines |
| 414 | (the parser knows if an indent token is required next). The |
| 415 | completion mechanism might use the interpreter's symbol table. A |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 416 | command to check (or even suggest) matching parentheses, quotes etc. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 417 | would also be useful. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 418 | |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 420 | \chapter{An Informal Introduction to Python} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 421 | |
| 422 | In the following examples, input and output are distinguished by the |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 423 | presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat |
| 424 | the example, you must type everything after the prompt, when the |
| 425 | prompt appears; lines that do not begin with a prompt are output from |
| 426 | the interpreter.% |
| 427 | \footnote{ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 428 | I'd prefer to use different fonts to distinguish input |
| 429 | from output, but the amount of LaTeX hacking that would require |
| 430 | is currently beyond my ability. |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 431 | } |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 432 | Note that a secondary prompt on a line by itself in an example means |
| 433 | you must type a blank line; this is used to end a multi-line command. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 434 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 435 | \section{Using Python as a Calculator} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 436 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 437 | Let's try some simple Python commands. Start the interpreter and wait |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 438 | for the primary prompt, {\tt >>>}. (It shouldn't take long.) |
| 439 | |
| 440 | \subsection{Numbers} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 441 | |
| 442 | The interpreter acts as a simple calculator: you can type an |
| 443 | expression at it and it will write the value. Expression syntax is |
| 444 | straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /} |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 445 | work just like in most other languages (e.g., Pascal or C); parentheses |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 446 | can be used for grouping. For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 447 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 448 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 449 | >>> 2+2 |
| 450 | 4 |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 451 | >>> # This is a comment |
| 452 | ... 2+2 |
| 453 | 4 |
| 454 | >>> 2+2 # and a comment on the same line as code |
| 455 | 4 |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 456 | >>> (50-5*6)/4 |
| 457 | 5 |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 458 | >>> # Integer division returns the floor: |
| 459 | ... 7/3 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 460 | 2 |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 461 | >>> 7/-3 |
| 462 | -3 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 463 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 464 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 465 | % |
| 466 | Like in C, the equal sign ({\tt =}) is used to assign a value to a |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 467 | variable. The value of an assignment is not written: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 468 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 469 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 470 | >>> width = 20 |
| 471 | >>> height = 5*9 |
| 472 | >>> width * height |
| 473 | 900 |
| 474 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 475 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 476 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 477 | A value can be assigned to several variables simultaneously: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 479 | \bcode\begin{verbatim} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 480 | >>> x = y = z = 0 # Zero x, y and z |
| 481 | >>> x |
| 482 | 0 |
| 483 | >>> y |
| 484 | 0 |
| 485 | >>> z |
| 486 | 0 |
| 487 | >>> |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 488 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 489 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 490 | There is full support for floating point; operators with mixed type |
| 491 | operands convert the integer operand to floating point: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 492 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 493 | \bcode\begin{verbatim} |
| 494 | >>> 4 * 2.5 / 3.3 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 495 | 3.0303030303 |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 496 | >>> 7.0 / 2 |
| 497 | 3.5 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 498 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 499 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 500 | |
| 501 | \subsection{Strings} |
| 502 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 503 | Besides numbers, Python can also manipulate strings, enclosed in |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 504 | single quotes or double quotes: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 506 | \bcode\begin{verbatim} |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 507 | >>> 'spam eggs' |
| 508 | 'spam eggs' |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 509 | >>> 'doesn\'t' |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 510 | "doesn't" |
| 511 | >>> "doesn't" |
| 512 | "doesn't" |
| 513 | >>> '"Yes," he said.' |
| 514 | '"Yes," he said.' |
| 515 | >>> "\"Yes,\" he said." |
| 516 | '"Yes," he said.' |
| 517 | >>> '"Isn\'t," she said.' |
| 518 | '"Isn\'t," she said.' |
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 | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 521 | % |
| 522 | Strings are written the same way as they are typed for input: inside |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 523 | quotes and with quotes and other funny characters escaped by backslashes, |
| 524 | to show the precise value. The string is enclosed in double quotes if |
| 525 | the string contains a single quote and no double quotes, else it's |
| 526 | enclosed in single quotes. (The {\tt print} statement, described later, |
| 527 | can be used to write strings without quotes or escapes.) |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 528 | |
| 529 | Strings can be concatenated (glued together) with the {\tt +} |
| 530 | operator, and repeated with {\tt *}: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 531 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 532 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 533 | >>> word = 'Help' + 'A' |
| 534 | >>> word |
| 535 | 'HelpA' |
| 536 | >>> '<' + word*5 + '>' |
| 537 | '<HelpAHelpAHelpAHelpAHelpA>' |
| 538 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 539 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 540 | % |
| 541 | Strings can be subscripted (indexed); like in C, the first character of |
| 542 | a string has subscript (index) 0. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 543 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 544 | There is no separate character type; a character is simply a string of |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 545 | size one. Like in Icon, substrings can be specified with the {\em |
| 546 | slice} notation: two indices separated by a colon. |
| 547 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 548 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 549 | >>> word[4] |
| 550 | 'A' |
| 551 | >>> word[0:2] |
| 552 | 'He' |
| 553 | >>> word[2:4] |
| 554 | 'lp' |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 555 | >>> |
| 556 | \end{verbatim}\ecode |
| 557 | % |
| 558 | Slice indices have useful defaults; an omitted first index defaults to |
| 559 | zero, an omitted second index defaults to the size of the string being |
| 560 | sliced. |
| 561 | |
| 562 | \bcode\begin{verbatim} |
| 563 | >>> word[:2] # The first two characters |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 564 | 'He' |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 565 | >>> word[2:] # All but the first two characters |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 566 | 'lpA' |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 567 | >>> |
| 568 | \end{verbatim}\ecode |
| 569 | % |
| 570 | Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\ |
| 571 | equals \verb\s\. |
| 572 | |
| 573 | \bcode\begin{verbatim} |
| 574 | >>> word[:2] + word[2:] |
| 575 | 'HelpA' |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 576 | >>> word[:3] + word[3:] |
| 577 | 'HelpA' |
| 578 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 579 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 580 | % |
| 581 | Degenerate slice indices are handled gracefully: an index that is too |
| 582 | large is replaced by the string size, an upper bound smaller than the |
| 583 | lower bound returns an empty string. |
| 584 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 585 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 586 | >>> word[1:100] |
| 587 | 'elpA' |
| 588 | >>> word[10:] |
| 589 | '' |
| 590 | >>> word[2:1] |
| 591 | '' |
| 592 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 593 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 594 | % |
| 595 | Indices may be negative numbers, to start counting from the right. |
| 596 | For example: |
| 597 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 598 | \bcode\begin{verbatim} |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 599 | >>> word[-1] # The last character |
| 600 | 'A' |
| 601 | >>> word[-2] # The last-but-one character |
| 602 | 'p' |
| 603 | >>> word[-2:] # The last two characters |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 604 | 'pA' |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 605 | >>> word[:-2] # All but the last two characters |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 606 | 'Hel' |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 607 | >>> |
| 608 | \end{verbatim}\ecode |
| 609 | % |
| 610 | But note that -0 is really the same as 0, so it does not count from |
| 611 | the right! |
| 612 | |
| 613 | \bcode\begin{verbatim} |
| 614 | >>> word[-0] # (since -0 equals 0) |
| 615 | 'H' |
| 616 | >>> |
| 617 | \end{verbatim}\ecode |
| 618 | % |
| 619 | Out-of-range negative slice indices are truncated, but don't try this |
| 620 | for single-element (non-slice) indices: |
| 621 | |
| 622 | \bcode\begin{verbatim} |
| 623 | >>> word[-100:] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 624 | 'HelpA' |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 625 | >>> word[-10] # error |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 626 | Traceback (innermost last): |
| 627 | File "<stdin>", line 1 |
| 628 | IndexError: string index out of range |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 629 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 630 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 631 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 632 | The best way to remember how slices work is to think of the indices as |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 633 | pointing {\em between} characters, with the left edge of the first |
| 634 | character numbered 0. Then the right edge of the last character of a |
| 635 | string of {\tt n} characters has index {\tt n}, for example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 636 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 637 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 638 | +---+---+---+---+---+ |
| 639 | | H | e | l | p | A | |
| 640 | +---+---+---+---+---+ |
| 641 | 0 1 2 3 4 5 |
| 642 | -5 -4 -3 -2 -1 |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 643 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 644 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 645 | The first row of numbers gives the position of the indices 0...5 in |
| 646 | the string; the second row gives the corresponding negative indices. |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 647 | The slice from \verb\i\ to \verb\j\ consists of all characters between |
| 648 | the edges labeled \verb\i\ and \verb\j\, respectively. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 649 | |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 650 | For nonnegative indices, the length of a slice is the difference of |
| 651 | the indices, if both are within bounds, e.g., the length of |
| 652 | \verb\word[1:3]\ is 2. |
| 653 | |
| 654 | The built-in function {\tt len()} returns the length of a string: |
| 655 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 656 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 657 | >>> s = 'supercalifragilisticexpialidocious' |
| 658 | >>> len(s) |
| 659 | 34 |
| 660 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 661 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 662 | |
| 663 | \subsection{Lists} |
| 664 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 665 | Python knows a number of {\em compound} data types, used to group |
| 666 | together other values. The most versatile is the {\em list}, which |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 667 | can be written as a list of comma-separated values (items) between |
| 668 | square brackets. List items need not all have the same type. |
| 669 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 670 | \bcode\begin{verbatim} |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 671 | >>> a = ['spam', 'eggs', 100, 1234] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 672 | >>> a |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 673 | ['spam', 'eggs', 100, 1234] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 674 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 675 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 676 | % |
| 677 | Like string indices, list indices start at 0, and lists can be sliced, |
| 678 | concatenated and so on: |
| 679 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 680 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 681 | >>> a[0] |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 682 | 'spam' |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 683 | >>> a[3] |
| 684 | 1234 |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 685 | >>> a[-2] |
| 686 | 100 |
| 687 | >>> a[1:-1] |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 688 | ['eggs', 100] |
| 689 | >>> a[:2] + ['bacon', 2*2] |
| 690 | ['spam', 'eggs', 'bacon', 4] |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 691 | >>> 3*a[:3] + ['Boe!'] |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 692 | ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!'] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 693 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 694 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 695 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 696 | Unlike strings, which are {\em immutable}, it is possible to change |
| 697 | individual elements of a list: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 699 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 700 | >>> a |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 701 | ['spam', 'eggs', 100, 1234] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 702 | >>> a[2] = a[2] + 23 |
| 703 | >>> a |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 704 | ['spam', 'eggs', 123, 1234] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 705 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 706 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 707 | % |
| 708 | Assignment to slices is also possible, and this can even change the size |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 709 | of the list: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 710 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 711 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 712 | >>> # Replace some items: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 713 | ... a[0:2] = [1, 12] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 714 | >>> a |
| 715 | [1, 12, 123, 1234] |
| 716 | >>> # Remove some: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 717 | ... a[0:2] = [] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 718 | >>> a |
| 719 | [123, 1234] |
| 720 | >>> # Insert some: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 721 | ... a[1:1] = ['bletch', 'xyzzy'] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 722 | >>> a |
| 723 | [123, 'bletch', 'xyzzy', 1234] |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 724 | >>> a[:0] = a # Insert (a copy of) itself at the beginning |
| 725 | >>> a |
| 726 | [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 727 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 728 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 729 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 730 | The built-in function {\tt len()} also applies to lists: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 731 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 732 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 733 | >>> len(a) |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 734 | 8 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 735 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 736 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 737 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 738 | It is possible to nest lists (create lists containing other lists), |
| 739 | for example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 740 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 741 | \bcode\begin{verbatim} |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 742 | >>> q = [2, 3] |
| 743 | >>> p = [1, q, 4] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 744 | >>> len(p) |
| 745 | 3 |
| 746 | >>> p[1] |
| 747 | [2, 3] |
| 748 | >>> p[1][0] |
| 749 | 2 |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 750 | >>> p[1].append('xtra') # See section 5.1 |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 751 | >>> p |
| 752 | [1, [2, 3, 'xtra'], 4] |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 753 | >>> q |
| 754 | [2, 3, 'xtra'] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 755 | >>> |
| 756 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 757 | % |
| 758 | Note that in the last example, {\tt p[1]} and {\tt q} really refer to |
| 759 | the same object! We'll come back to {\em object semantics} later. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 761 | \section{First Steps Towards Programming} |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 762 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 763 | Of course, we can use Python for more complicated tasks than adding |
| 764 | two and two together. For instance, we can write an initial |
| 765 | subsequence of the {\em Fibonacci} series as follows: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 766 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 767 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 768 | >>> # Fibonacci series: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 769 | ... # the sum of two elements defines the next |
| 770 | ... a, b = 0, 1 |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 771 | >>> while b < 10: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 772 | ... print b |
| 773 | ... a, b = b, a+b |
| 774 | ... |
| 775 | 1 |
| 776 | 1 |
| 777 | 2 |
| 778 | 3 |
| 779 | 5 |
| 780 | 8 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 781 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 782 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 783 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 784 | This example introduces several new features. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 785 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 786 | \begin{itemize} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 787 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 788 | \item |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 789 | The first line contains a {\em multiple assignment}: the variables |
| 790 | {\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the |
| 791 | last line this is used again, demonstrating that the expressions on |
| 792 | the right-hand side are all evaluated first before any of the |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 793 | assignments take place. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 794 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 795 | \item |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 796 | The {\tt while} loop executes as long as the condition (here: {\tt b < |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 797 | 10}) remains true. In Python, like in C, any non-zero integer value is |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 798 | true; zero is false. The condition may also be a string or list value, |
| 799 | in fact any sequence; anything with a non-zero length is true, empty |
| 800 | sequences are false. The test used in the example is a simple |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 801 | comparison. The standard comparison operators are written the same as |
| 802 | in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 803 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 804 | \item |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 805 | The {\em body} of the loop is {\em indented}: indentation is Python's |
| 806 | way of grouping statements. Python does not (yet!) provide an |
| 807 | intelligent input line editing facility, so you have to type a tab or |
| 808 | space(s) for each indented line. In practice you will prepare more |
| 809 | complicated input for Python with a text editor; most text editors have |
| 810 | an auto-indent facility. When a compound statement is entered |
| 811 | interactively, it must be followed by a blank line to indicate |
| 812 | completion (since the parser cannot guess when you have typed the last |
| 813 | line). |
| 814 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 815 | \item |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 816 | The {\tt print} statement writes the value of the expression(s) it is |
| 817 | given. It differs from just writing the expression you want to write |
| 818 | (as we did earlier in the calculator examples) in the way it handles |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 819 | multiple expressions and strings. Strings are printed without quotes, |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 820 | and a space is inserted between items, so you can format things nicely, |
| 821 | like this: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 822 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 823 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 824 | >>> i = 256*256 |
| 825 | >>> print 'The value of i is', i |
| 826 | The value of i is 65536 |
| 827 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 828 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 829 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 830 | A trailing comma avoids the newline after the output: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 831 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 832 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 833 | >>> a, b = 0, 1 |
| 834 | >>> while b < 1000: |
| 835 | ... print b, |
| 836 | ... a, b = b, a+b |
| 837 | ... |
| 838 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 |
| 839 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 840 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 841 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 842 | Note that the interpreter inserts a newline before it prints the next |
| 843 | prompt if the last line was not completed. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 845 | \end{itemize} |
| 846 | |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 847 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 848 | \chapter{More Control Flow Tools} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 849 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 850 | Besides the {\tt while} statement just introduced, Python knows the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 851 | usual control flow statements known from other languages, with some |
| 852 | twists. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 853 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 854 | \section{If Statements} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 855 | |
| 856 | Perhaps the most well-known statement type is the {\tt if} statement. |
| 857 | For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 858 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 859 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 860 | >>> if x < 0: |
| 861 | ... x = 0 |
| 862 | ... print 'Negative changed to zero' |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 863 | ... elif x == 0: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 864 | ... print 'Zero' |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 865 | ... elif x == 1: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 866 | ... print 'Single' |
| 867 | ... else: |
| 868 | ... print 'More' |
| 869 | ... |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 870 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 871 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 872 | There can be zero or more {\tt elif} parts, and the {\tt else} part is |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 873 | optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is |
| 874 | useful to avoid excessive indentation. An {\tt if...elif...elif...} |
| 875 | sequence is a substitute for the {\em switch} or {\em case} statements |
| 876 | found in other languages. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 877 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 878 | \section{For Statements} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 879 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 880 | The {\tt for} statement in Python differs a bit from what you may be |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 881 | used to in C or Pascal. Rather than always iterating over an |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 882 | arithmetic progression of numbers (like in Pascal), or leaving the user |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 883 | completely free in the iteration test and step (as C), Python's {\tt |
| 884 | for} statement iterates over the items of any sequence (e.g., a list |
| 885 | or a string), in the order that they appear in the sequence. For |
| 886 | example (no pun intended): |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 887 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 888 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 889 | >>> # Measure some strings: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 890 | ... a = ['cat', 'window', 'defenestrate'] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 891 | >>> for x in a: |
| 892 | ... print x, len(x) |
| 893 | ... |
| 894 | cat 3 |
| 895 | window 6 |
| 896 | defenestrate 12 |
| 897 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 898 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 899 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 900 | It is not safe to modify the sequence being iterated over in the loop |
| 901 | (this can only happen for mutable sequence types, i.e., lists). If |
| 902 | you need to modify the list you are iterating over, e.g., duplicate |
| 903 | selected items, you must iterate over a copy. The slice notation |
| 904 | makes this particularly convenient: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 905 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 906 | \bcode\begin{verbatim} |
| 907 | >>> for x in a[:]: # make a slice copy of the entire list |
| 908 | ... if len(x) > 6: a.insert(0, x) |
| 909 | ... |
| 910 | >>> a |
| 911 | ['defenestrate', 'cat', 'window', 'defenestrate'] |
| 912 | >>> |
| 913 | \end{verbatim}\ecode |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 914 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 915 | \section{The {\tt range()} Function} |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 916 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 917 | If you do need to iterate over a sequence of numbers, the built-in |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 918 | function {\tt range()} comes in handy. It generates lists containing |
| 919 | arithmetic progressions, e.g.: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 920 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 921 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 922 | >>> range(10) |
| 923 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 924 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 925 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 926 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 927 | The given end point is never part of the generated list; {\tt range(10)} |
| 928 | generates a list of 10 values, exactly the legal indices for items of a |
| 929 | sequence of length 10. It is possible to let the range start at another |
| 930 | number, or to specify a different increment (even negative): |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 931 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 932 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 933 | >>> range(5, 10) |
| 934 | [5, 6, 7, 8, 9] |
| 935 | >>> range(0, 10, 3) |
| 936 | [0, 3, 6, 9] |
| 937 | >>> range(-10, -100, -30) |
| 938 | [-10, -40, -70] |
| 939 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 940 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 941 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 942 | To iterate over the indices of a sequence, combine {\tt range()} and |
| 943 | {\tt len()} as follows: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 944 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 945 | \bcode\begin{verbatim} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 946 | >>> a = ['Mary', 'had', 'a', 'little', 'lamb'] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 947 | >>> for i in range(len(a)): |
| 948 | ... print i, a[i] |
| 949 | ... |
| 950 | 0 Mary |
| 951 | 1 had |
| 952 | 2 a |
| 953 | 3 little |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 954 | 4 lamb |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 955 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 956 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 957 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 958 | \section{Break and Continue Statements, and Else Clauses on Loops} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 960 | The {\tt break} statement, like in C, breaks out of the smallest |
| 961 | enclosing {\tt for} or {\tt while} loop. |
| 962 | |
| 963 | The {\tt continue} statement, also borrowed from C, continues with the |
| 964 | next iteration of the loop. |
| 965 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 966 | 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] | 967 | loop terminates through exhaustion of the list (with {\tt for}) or when |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 968 | the condition becomes false (with {\tt while}), but not when the loop is |
| 969 | terminated by a {\tt break} statement. This is exemplified by the |
Guido van Rossum | cfb45e4 | 1994-11-10 23:04:43 +0000 | [diff] [blame] | 970 | following loop, which searches for prime numbers: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 971 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 972 | \bcode\begin{verbatim} |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 973 | >>> for n in range(2, 10): |
| 974 | ... for x in range(2, n): |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 975 | ... if n % x == 0: |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 976 | ... print n, 'equals', x, '*', n/x |
| 977 | ... break |
| 978 | ... else: |
| 979 | ... print n, 'is a prime number' |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 980 | ... |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 981 | 2 is a prime number |
| 982 | 3 is a prime number |
| 983 | 4 equals 2 * 2 |
| 984 | 5 is a prime number |
| 985 | 6 equals 2 * 3 |
| 986 | 7 is a prime number |
| 987 | 8 equals 2 * 4 |
| 988 | 9 equals 3 * 3 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 989 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 990 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 991 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 992 | \section{Pass Statements} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 993 | |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 994 | The {\tt pass} statement does nothing. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 995 | It can be used when a statement is required syntactically but the |
| 996 | program requires no action. |
| 997 | For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 998 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 999 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1000 | >>> while 1: |
| 1001 | ... pass # Busy-wait for keyboard interrupt |
| 1002 | ... |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1003 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1004 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1005 | \section{Defining Functions} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1006 | |
| 1007 | We can create a function that writes the Fibonacci series to an |
| 1008 | arbitrary boundary: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1009 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1010 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1011 | >>> def fib(n): # write Fibonacci series up to n |
| 1012 | ... a, b = 0, 1 |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 1013 | ... while b < n: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1014 | ... print b, |
| 1015 | ... a, b = b, a+b |
| 1016 | ... |
| 1017 | >>> # Now call the function we just defined: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1018 | ... fib(2000) |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1019 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 |
| 1020 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1021 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1022 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1023 | The keyword {\tt def} introduces a function {\em definition}. It must |
| 1024 | be followed by the function name and the parenthesized list of formal |
| 1025 | parameters. The statements that form the body of the function starts at |
| 1026 | the next line, indented by a tab stop. |
| 1027 | |
| 1028 | The {\em execution} of a function introduces a new symbol table used |
| 1029 | for the local variables of the function. More precisely, all variable |
| 1030 | assignments in a function store the value in the local symbol table; |
| 1031 | whereas |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1032 | variable references first look in the local symbol table, then |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1033 | in the global symbol table, and then in the table of built-in names. |
| 1034 | Thus, |
Guido van Rossum | cfb45e4 | 1994-11-10 23:04:43 +0000 | [diff] [blame] | 1035 | global variables cannot be directly assigned a value within a |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1036 | function (unless named in a {\tt global} statement), although |
| 1037 | they may be referenced. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1038 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1039 | The actual parameters (arguments) to a function call are introduced in |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1040 | the local symbol table of the called function when it is called; thus, |
| 1041 | arguments are passed using {\em call\ by\ value}.% |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1042 | \footnote{ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1043 | Actually, {\em call by object reference} would be a better |
| 1044 | description, since if a mutable object is passed, the caller |
| 1045 | will see any changes the callee makes to it (e.g., items |
| 1046 | inserted into a list). |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1047 | } |
| 1048 | When a function calls another function, a new local symbol table is |
| 1049 | created for that call. |
| 1050 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1051 | A function definition introduces the function name in the |
| 1052 | current |
| 1053 | symbol table. The value |
| 1054 | of the function name |
| 1055 | has a type that is recognized by the interpreter as a user-defined |
| 1056 | function. This value can be assigned to another name which can then |
| 1057 | also be used as a function. This serves as a general renaming |
| 1058 | mechanism: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1059 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1060 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1061 | >>> fib |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1062 | <function object at 10042ed0> |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1063 | >>> f = fib |
| 1064 | >>> f(100) |
| 1065 | 1 1 2 3 5 8 13 21 34 55 89 |
| 1066 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1067 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1068 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1069 | You might object that {\tt fib} is not a function but a procedure. In |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1070 | Python, like in C, procedures are just functions that don't return a |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1071 | value. In fact, technically speaking, procedures do return a value, |
| 1072 | albeit a rather boring one. This value is called {\tt None} (it's a |
| 1073 | built-in name). Writing the value {\tt None} is normally suppressed by |
| 1074 | the interpreter if it would be the only value written. You can see it |
| 1075 | if you really want to: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1076 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1077 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1078 | >>> print fib(0) |
| 1079 | None |
| 1080 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1081 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1082 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1083 | It is simple to write a function that returns a list of the numbers of |
| 1084 | the Fibonacci series, instead of printing it: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1085 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1086 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1087 | >>> def fib2(n): # return Fibonacci series up to n |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1088 | ... result = [] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1089 | ... a, b = 0, 1 |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 1090 | ... while b < n: |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1091 | ... result.append(b) # see below |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1092 | ... a, b = b, a+b |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1093 | ... return result |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1094 | ... |
| 1095 | >>> f100 = fib2(100) # call it |
| 1096 | >>> f100 # write the result |
| 1097 | [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
| 1098 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1099 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1100 | % |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1101 | This example, as usual, demonstrates some new Python features: |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1102 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1103 | \begin{itemize} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1104 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1105 | \item |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1106 | The {\tt return} statement returns with a value from a function. {\tt |
| 1107 | return} without an expression argument is used to return from the middle |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1108 | of a procedure (falling off the end also returns from a procedure), in |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1109 | which case the {\tt None} value is returned. |
| 1110 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1111 | \item |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1112 | The statement {\tt result.append(b)} calls a {\em method} of the list |
| 1113 | object {\tt result}. A method is a function that `belongs' to an |
| 1114 | object and is named {\tt obj.methodname}, where {\tt obj} is some |
| 1115 | object (this may be an expression), and {\tt methodname} is the name |
| 1116 | of a method that is defined by the object's type. Different types |
| 1117 | define different methods. Methods of different types may have the |
| 1118 | same name without causing ambiguity. (It is possible to define your |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1119 | own object types and methods, using {\em classes}, as discussed later |
| 1120 | in this tutorial.) |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1121 | The method {\tt append} shown in the example, is defined for |
| 1122 | list objects; it adds a new element at the end of the list. In this |
| 1123 | example |
| 1124 | it is equivalent to {\tt result = result + [b]}, but more efficient. |
| 1125 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1126 | \end{itemize} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1127 | |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 1128 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1129 | \chapter{Odds and Ends} |
| 1130 | |
| 1131 | This chapter describes some things you've learned about already in |
| 1132 | more detail, and adds some new things as well. |
| 1133 | |
| 1134 | \section{More on Lists} |
| 1135 | |
| 1136 | The list data type has some more methods. Here are all of the methods |
| 1137 | of lists objects: |
| 1138 | |
Guido van Rossum | 7d9f8d7 | 1991-01-22 11:45:00 +0000 | [diff] [blame] | 1139 | \begin{description} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1140 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1141 | \item[{\tt insert(i, x)}] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1142 | Insert an item at a given position. The first argument is the index of |
| 1143 | the element before which to insert, so {\tt a.insert(0, x)} inserts at |
| 1144 | the front of the list, and {\tt a.insert(len(a), x)} is equivalent to |
| 1145 | {\tt a.append(x)}. |
| 1146 | |
| 1147 | \item[{\tt append(x)}] |
| 1148 | Equivalent to {\tt a.insert(len(a), x)}. |
| 1149 | |
| 1150 | \item[{\tt index(x)}] |
| 1151 | Return the index in the list of the first item whose value is {\tt x}. |
| 1152 | It is an error if there is no such item. |
| 1153 | |
| 1154 | \item[{\tt remove(x)}] |
| 1155 | Remove the first item from the list whose value is {\tt x}. |
| 1156 | It is an error if there is no such item. |
| 1157 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1158 | \item[{\tt sort()}] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1159 | Sort the items of the list, in place. |
| 1160 | |
| 1161 | \item[{\tt reverse()}] |
| 1162 | Reverse the elements of the list, in place. |
| 1163 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1164 | \item[{\tt count(x)}] |
| 1165 | Return the number of times {\tt x} appears in the list. |
| 1166 | |
Guido van Rossum | 7d9f8d7 | 1991-01-22 11:45:00 +0000 | [diff] [blame] | 1167 | \end{description} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1168 | |
| 1169 | An example that uses all list methods: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1170 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1171 | \bcode\begin{verbatim} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1172 | >>> a = [66.6, 333, 333, 1, 1234.5] |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1173 | >>> print a.count(333), a.count(66.6), a.count('x') |
| 1174 | 2 1 0 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1175 | >>> a.insert(2, -1) |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1176 | >>> a.append(333) |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1177 | >>> a |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1178 | [66.6, 333, -1, 333, 1, 1234.5, 333] |
| 1179 | >>> a.index(333) |
| 1180 | 1 |
| 1181 | >>> a.remove(333) |
| 1182 | >>> a |
| 1183 | [66.6, -1, 333, 1, 1234.5, 333] |
| 1184 | >>> a.reverse() |
| 1185 | >>> a |
| 1186 | [333, 1234.5, 1, 333, -1, 66.6] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1187 | >>> a.sort() |
| 1188 | >>> a |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1189 | [-1, 1, 66.6, 333, 333, 1234.5] |
| 1190 | >>> |
| 1191 | \end{verbatim}\ecode |
| 1192 | |
| 1193 | \section{The {\tt del} statement} |
| 1194 | |
| 1195 | There is a way to remove an item from a list given its index instead |
| 1196 | of its value: the {\tt del} statement. This can also be used to |
| 1197 | remove slices from a list (which we did earlier by assignment of an |
| 1198 | empty list to the slice). For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1199 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1200 | \bcode\begin{verbatim} |
| 1201 | >>> a |
| 1202 | [-1, 1, 66.6, 333, 333, 1234.5] |
| 1203 | >>> del a[0] |
| 1204 | >>> a |
| 1205 | [1, 66.6, 333, 333, 1234.5] |
| 1206 | >>> del a[2:4] |
| 1207 | >>> a |
| 1208 | [1, 66.6, 1234.5] |
| 1209 | >>> |
| 1210 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1211 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1212 | {\tt del} can also be used to delete entire variables: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1213 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1214 | \bcode\begin{verbatim} |
| 1215 | >>> del a |
| 1216 | >>> |
| 1217 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1218 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1219 | Referencing the name {\tt a} hereafter is an error (at least until |
| 1220 | another value is assigned to it). We'll find other uses for {\tt del} |
| 1221 | later. |
| 1222 | |
| 1223 | \section{Tuples and Sequences} |
| 1224 | |
| 1225 | We saw that lists and strings have many common properties, e.g., |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1226 | indexing and slicing operations. They are two examples of {\em |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1227 | sequence} data types. Since Python is an evolving language, other |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1228 | sequence data types may be added. There is also another standard |
| 1229 | sequence data type: the {\em tuple}. |
| 1230 | |
| 1231 | A tuple consists of a number of values separated by commas, for |
| 1232 | instance: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1233 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1234 | \bcode\begin{verbatim} |
| 1235 | >>> t = 12345, 54321, 'hello!' |
| 1236 | >>> t[0] |
| 1237 | 12345 |
| 1238 | >>> t |
| 1239 | (12345, 54321, 'hello!') |
| 1240 | >>> # Tuples may be nested: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1241 | ... u = t, (1, 2, 3, 4, 5) |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1242 | >>> u |
| 1243 | ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5)) |
| 1244 | >>> |
| 1245 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1246 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1247 | As you see, on output tuples are alway enclosed in parentheses, so |
| 1248 | that nested tuples are interpreted correctly; they may be input with |
| 1249 | or without surrounding parentheses, although often parentheses are |
| 1250 | necessary anyway (if the tuple is part of a larger expression). |
| 1251 | |
| 1252 | Tuples have many uses, e.g., (x, y) coordinate pairs, employee records |
| 1253 | from a database, etc. Tuples, like strings, are immutable: it is not |
| 1254 | possible to assign to the individual items of a tuple (you can |
| 1255 | simulate much of the same effect with slicing and concatenation, |
| 1256 | though). |
| 1257 | |
| 1258 | A special problem is the construction of tuples containing 0 or 1 |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1259 | items: the syntax has some extra quirks to accommodate these. Empty |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1260 | tuples are constructed by an empty pair of parentheses; a tuple with |
| 1261 | one item is constructed by following a value with a comma |
| 1262 | (it is not sufficient to enclose a single value in parentheses). |
| 1263 | Ugly, but effective. For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1264 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1265 | \bcode\begin{verbatim} |
| 1266 | >>> empty = () |
| 1267 | >>> singleton = 'hello', # <-- note trailing comma |
| 1268 | >>> len(empty) |
| 1269 | 0 |
| 1270 | >>> len(singleton) |
| 1271 | 1 |
| 1272 | >>> singleton |
| 1273 | ('hello',) |
| 1274 | >>> |
| 1275 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1276 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1277 | The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em |
| 1278 | tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'} |
| 1279 | are packed together in a tuple. The reverse operation is also |
| 1280 | possible, e.g.: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1281 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1282 | \bcode\begin{verbatim} |
| 1283 | >>> x, y, z = t |
| 1284 | >>> |
| 1285 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1286 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1287 | This is called, appropriately enough, {\em tuple unpacking}. Tuple |
| 1288 | unpacking requires that the list of variables on the left has the same |
| 1289 | number of elements as the length of the tuple. Note that multiple |
| 1290 | assignment is really just a combination of tuple packing and tuple |
| 1291 | unpacking! |
| 1292 | |
| 1293 | Occasionally, the corresponding operation on lists is useful: {\em list |
| 1294 | unpacking}. This is supported by enclosing the list of variables in |
| 1295 | square brackets: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1296 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1297 | \bcode\begin{verbatim} |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 1298 | >>> a = ['spam', 'eggs', 100, 1234] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1299 | >>> [a1, a2, a3, a4] = a |
| 1300 | >>> |
| 1301 | \end{verbatim}\ecode |
| 1302 | |
| 1303 | \section{Dictionaries} |
| 1304 | |
| 1305 | Another useful data type built into Python is the {\em dictionary}. |
| 1306 | Dictionaries are sometimes found in other languages as ``associative |
| 1307 | memories'' or ``associative arrays''. Unlike sequences, which are |
| 1308 | indexed by a range of numbers, dictionaries are indexed by {\em keys}, |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1309 | which are strings (the use of non-string values as keys |
| 1310 | is supported, but beyond the scope of this tutorial). |
| 1311 | It is best to think of a dictionary as an unordered set of |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1312 | {\em key:value} pairs, with the requirement that the keys are unique |
| 1313 | (within one dictionary). |
| 1314 | A pair of braces creates an empty dictionary: \verb/{}/. |
| 1315 | Placing a comma-separated list of key:value pairs within the |
| 1316 | braces adds initial key:value pairs to the dictionary; this is also the |
| 1317 | way dictionaries are written on output. |
| 1318 | |
| 1319 | The main operations on a dictionary are storing a value with some key |
| 1320 | and extracting the value given the key. It is also possible to delete |
| 1321 | a key:value pair |
| 1322 | with {\tt del}. |
| 1323 | If you store using a key that is already in use, the old value |
| 1324 | associated with that key is forgotten. It is an error to extract a |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1325 | value using a non-existent key. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1326 | |
| 1327 | The {\tt keys()} method of a dictionary object returns a list of all the |
| 1328 | keys used in the dictionary, in random order (if you want it sorted, |
| 1329 | just apply the {\tt sort()} method to the list of keys). To check |
| 1330 | whether a single key is in the dictionary, use the \verb/has_key()/ |
| 1331 | method of the dictionary. |
| 1332 | |
| 1333 | Here is a small example using a dictionary: |
| 1334 | |
| 1335 | \bcode\begin{verbatim} |
| 1336 | >>> tel = {'jack': 4098, 'sape': 4139} |
| 1337 | >>> tel['guido'] = 4127 |
| 1338 | >>> tel |
Guido van Rossum | 8f96f77 | 1991-11-12 15:45:03 +0000 | [diff] [blame] | 1339 | {'sape': 4139, 'guido': 4127, 'jack': 4098} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1340 | >>> tel['jack'] |
| 1341 | 4098 |
| 1342 | >>> del tel['sape'] |
| 1343 | >>> tel['irv'] = 4127 |
| 1344 | >>> tel |
Guido van Rossum | 8f96f77 | 1991-11-12 15:45:03 +0000 | [diff] [blame] | 1345 | {'guido': 4127, 'irv': 4127, 'jack': 4098} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1346 | >>> tel.keys() |
| 1347 | ['guido', 'irv', 'jack'] |
| 1348 | >>> tel.has_key('guido') |
| 1349 | 1 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1350 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1351 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1352 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1353 | \section{More on Conditions} |
| 1354 | |
| 1355 | The conditions used in {\tt while} and {\tt if} statements above can |
| 1356 | contain other operators besides comparisons. |
| 1357 | |
| 1358 | The comparison operators {\tt in} and {\tt not in} check whether a value |
| 1359 | occurs (does not occur) in a sequence. The operators {\tt is} and {\tt |
| 1360 | is not} compare whether two objects are really the same object; this |
| 1361 | only matters for mutable objects like lists. All comparison operators |
| 1362 | have the same priority, which is lower than that of all numerical |
| 1363 | operators. |
| 1364 | |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 1365 | Comparisons can be chained: e.g., {\tt a < b == c} tests whether {\tt a} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1366 | is less than {\tt b} and moreover {\tt b} equals {\tt c}. |
| 1367 | |
| 1368 | Comparisons may be combined by the Boolean operators {\tt and} and {\tt |
| 1369 | or}, and the outcome of a comparison (or of any other Boolean |
| 1370 | expression) may be negated with {\tt not}. These all have lower |
| 1371 | priorities than comparison operators again; between them, {\tt not} has |
| 1372 | the highest priority, and {\tt or} the lowest, so that |
| 1373 | {\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of |
| 1374 | course, parentheses can be used to express the desired composition. |
| 1375 | |
| 1376 | The Boolean operators {\tt and} and {\tt or} are so-called {\em |
| 1377 | shortcut} operators: their arguments are evaluated from left to right, |
| 1378 | and evaluation stops as soon as the outcome is determined. E.g., if |
| 1379 | {\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C} |
| 1380 | does not evaluate the expression C. In general, the return value of a |
| 1381 | shortcut operator, when used as a general value and not as a Boolean, is |
| 1382 | the last evaluated argument. |
| 1383 | |
| 1384 | It is possible to assign the result of a comparison or other Boolean |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1385 | expression to a variable. For example, |
| 1386 | |
| 1387 | \bcode\begin{verbatim} |
| 1388 | >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance' |
| 1389 | >>> non_null = string1 or string2 or string3 |
| 1390 | >>> non_null |
| 1391 | 'Trondheim' |
| 1392 | >>> |
| 1393 | \end{verbatim}\ecode |
| 1394 | % |
| 1395 | Note that in Python, unlike C, assignment cannot occur inside expressions. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1396 | |
| 1397 | \section{Comparing Sequences and Other Types} |
| 1398 | |
| 1399 | Sequence objects may be compared to other objects with the same |
| 1400 | sequence type. The comparison uses {\em lexicographical} ordering: |
| 1401 | first the first two items are compared, and if they differ this |
| 1402 | determines the outcome of the comparison; if they are equal, the next |
| 1403 | two items are compared, and so on, until either sequence is exhausted. |
| 1404 | If two items to be compared are themselves sequences of the same type, |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1405 | the lexicographical comparison is carried out recursively. If all |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1406 | items of two sequences compare equal, the sequences are considered |
| 1407 | equal. If one sequence is an initial subsequence of the other, the |
| 1408 | shorted sequence is the smaller one. Lexicographical ordering for |
Guido van Rossum | 47b4c0f | 1995-03-15 11:25:32 +0000 | [diff] [blame] | 1409 | strings uses the \ASCII{} ordering for individual characters. Some |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1410 | examples of comparisons between sequences with the same types: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1411 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1412 | \bcode\begin{verbatim} |
| 1413 | (1, 2, 3) < (1, 2, 4) |
| 1414 | [1, 2, 3] < [1, 2, 4] |
| 1415 | 'ABC' < 'C' < 'Pascal' < 'Python' |
| 1416 | (1, 2, 3, 4) < (1, 2, 4) |
| 1417 | (1, 2) < (1, 2, -1) |
| 1418 | (1, 2, 3) = (1.0, 2.0, 3.0) |
| 1419 | (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4) |
| 1420 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1421 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1422 | Note that comparing objects of different types is legal. The outcome |
| 1423 | is deterministic but arbitrary: the types are ordered by their name. |
| 1424 | Thus, a list is always smaller than a string, a string is always |
| 1425 | smaller than a tuple, etc. Mixed numeric types are compared according |
| 1426 | to their numeric value, so 0 equals 0.0, etc.% |
| 1427 | \footnote{ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1428 | The rules for comparing objects of different types should |
| 1429 | not be relied upon; they may change in a future version of |
| 1430 | the language. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 1433 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1434 | \chapter{Modules} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1435 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1436 | 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] | 1437 | definitions you have made (functions and variables) are lost. |
| 1438 | Therefore, if you want to write a somewhat longer program, you are |
| 1439 | better off using a text editor to prepare the input for the interpreter |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 1440 | and running it with that file as input instead. This is known as creating a |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1441 | {\em script}. As your program gets longer, you may want to split it |
| 1442 | into several files for easier maintenance. You may also want to use a |
| 1443 | handy function that you've written in several programs without copying |
| 1444 | its definition into each program. |
| 1445 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1446 | 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] | 1447 | them in a script or in an interactive instance of the interpreter. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1448 | Such a file is called a {\em module}; definitions from a module can be |
| 1449 | {\em imported} into other modules or into the {\em main} module (the |
| 1450 | collection of variables that you have access to in a script |
| 1451 | executed at the top level |
| 1452 | and in calculator mode). |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1453 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1454 | A module is a file containing Python definitions and statements. The |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1455 | file name is the module name with the suffix {\tt .py} appended. Within |
| 1456 | a module, the module's name (as a string) is available as the value of |
| 1457 | the global variable {\tt __name__}. For instance, use your favorite text |
| 1458 | editor to create a file called {\tt fibo.py} in the current directory |
| 1459 | with the following contents: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1460 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1461 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1462 | # Fibonacci numbers module |
| 1463 | |
| 1464 | def fib(n): # write Fibonacci series up to n |
| 1465 | a, b = 0, 1 |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 1466 | while b < n: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1467 | print b, |
| 1468 | a, b = b, a+b |
| 1469 | |
| 1470 | def fib2(n): # return Fibonacci series up to n |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1471 | result = [] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1472 | a, b = 0, 1 |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 1473 | while b < n: |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1474 | result.append(b) |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1475 | a, b = b, a+b |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1476 | return result |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1477 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1478 | % |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1479 | Now enter the Python interpreter and import this module with the |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1480 | following command: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1481 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1482 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1483 | >>> import fibo |
| 1484 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1485 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1486 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1487 | This does not enter the names of the functions defined in |
| 1488 | {\tt fibo} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1489 | directly in the current symbol table; it only enters the module name |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1490 | {\tt fibo} |
| 1491 | there. |
| 1492 | Using the module name you can access the functions: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1493 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1494 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1495 | >>> fibo.fib(1000) |
| 1496 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 |
| 1497 | >>> fibo.fib2(100) |
| 1498 | [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1499 | >>> fibo.__name__ |
| 1500 | 'fibo' |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1501 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1502 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1503 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1504 | If you intend to use a function often you can assign it to a local name: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1505 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1506 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1507 | >>> fib = fibo.fib |
| 1508 | >>> fib(500) |
| 1509 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |
| 1510 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1511 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1512 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1513 | \section{More on Modules} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1514 | |
| 1515 | A module can contain executable statements as well as function |
| 1516 | definitions. |
| 1517 | These statements are intended to initialize the module. |
| 1518 | They are executed only the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1519 | {\em first} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1520 | time the module is imported somewhere.% |
| 1521 | \footnote{ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1522 | In fact function definitions are also `statements' that are |
| 1523 | `executed'; the execution enters the function name in the |
| 1524 | module's global symbol table. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | Each module has its own private symbol table, which is used as the |
| 1528 | global symbol table by all functions defined in the module. |
| 1529 | Thus, the author of a module can use global variables in the module |
| 1530 | without worrying about accidental clashes with a user's global |
| 1531 | variables. |
| 1532 | On the other hand, if you know what you are doing you can touch a |
| 1533 | module's global variables with the same notation used to refer to its |
| 1534 | functions, |
| 1535 | {\tt modname.itemname}. |
| 1536 | |
| 1537 | Modules can import other modules. |
| 1538 | It is customary but not required to place all |
| 1539 | {\tt import} |
| 1540 | statements at the beginning of a module (or script, for that matter). |
| 1541 | The imported module names are placed in the importing module's global |
| 1542 | symbol table. |
| 1543 | |
| 1544 | There is a variant of the |
| 1545 | {\tt import} |
| 1546 | statement that imports names from a module directly into the importing |
| 1547 | module's symbol table. |
| 1548 | For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1549 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1550 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1551 | >>> from fibo import fib, fib2 |
| 1552 | >>> fib(500) |
| 1553 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |
| 1554 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1555 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1556 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1557 | This does not introduce the module name from which the imports are taken |
| 1558 | in the local symbol table (so in the example, {\tt fibo} is not |
| 1559 | defined). |
| 1560 | |
| 1561 | There is even a variant to import all names that a module defines: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1562 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1563 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1564 | >>> from fibo import * |
| 1565 | >>> fib(500) |
| 1566 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |
| 1567 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1568 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1569 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1570 | This imports all names except those beginning with an underscore |
Guido van Rossum | 573805a | 1992-03-06 10:56:03 +0000 | [diff] [blame] | 1571 | ({\tt _}). |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1572 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1573 | \section{Standard Modules} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1574 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1575 | Python comes with a library of standard modules, described in a separate |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1576 | document (Python Library Reference). Some modules are built into the |
| 1577 | interpreter; these provide access to operations that are not part of the |
| 1578 | core of the language but are nevertheless built in, either for |
| 1579 | efficiency or to provide access to operating system primitives such as |
| 1580 | system calls. The set of such modules is a configuration option; e.g., |
| 1581 | the {\tt amoeba} module is only provided on systems that somehow support |
| 1582 | Amoeba primitives. One particular module deserves some attention: {\tt |
| 1583 | sys}, which is built into every Python interpreter. The variables {\tt |
| 1584 | sys.ps1} and {\tt sys.ps2} define the strings used as primary and |
| 1585 | secondary prompts: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1586 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1587 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1588 | >>> import sys |
| 1589 | >>> sys.ps1 |
| 1590 | '>>> ' |
| 1591 | >>> sys.ps2 |
| 1592 | '... ' |
| 1593 | >>> sys.ps1 = 'C> ' |
| 1594 | C> print 'Yuck!' |
| 1595 | Yuck! |
| 1596 | C> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1597 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1598 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1599 | These two variables are only defined if the interpreter is in |
| 1600 | interactive mode. |
| 1601 | |
| 1602 | The variable |
| 1603 | {\tt sys.path} |
| 1604 | is a list of strings that determine the interpreter's search path for |
| 1605 | modules. |
| 1606 | It is initialized to a default path taken from the environment variable |
| 1607 | {\tt PYTHONPATH}, |
| 1608 | or from a built-in default if |
| 1609 | {\tt PYTHONPATH} |
| 1610 | is not set. |
| 1611 | You can modify it using standard list operations, e.g.: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1612 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1613 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1614 | >>> import sys |
| 1615 | >>> sys.path.append('/ufs/guido/lib/python') |
| 1616 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1617 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1618 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1619 | \section{The {\tt dir()} function} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1620 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1621 | The built-in function {\tt dir} is used to find out which names a module |
| 1622 | defines. It returns a sorted list of strings: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1623 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1624 | \bcode\begin{verbatim} |
| 1625 | >>> import fibo, sys |
| 1626 | >>> dir(fibo) |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1627 | ['__name__', 'fib', 'fib2'] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1628 | >>> dir(sys) |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1629 | ['__name__', 'argv', 'builtin_module_names', 'copyright', 'exit', |
| 1630 | 'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace', |
| 1631 | 'stderr', 'stdin', 'stdout', 'version'] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1632 | >>> |
| 1633 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1634 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1635 | Without arguments, {\tt dir()} lists the names you have defined currently: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1636 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1637 | \bcode\begin{verbatim} |
| 1638 | >>> a = [1, 2, 3, 4, 5] |
| 1639 | >>> import fibo, sys |
| 1640 | >>> fib = fibo.fib |
| 1641 | >>> dir() |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1642 | ['__name__', 'a', 'fib', 'fibo', 'sys'] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1643 | >>> |
| 1644 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1645 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1646 | Note that it lists all types of names: variables, modules, functions, etc. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1647 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1648 | {\tt dir()} does not list the names of built-in functions and variables. |
| 1649 | If you want a list of those, they are defined in the standard module |
Guido van Rossum | 4bd023f | 1993-10-27 13:49:20 +0000 | [diff] [blame] | 1650 | {\tt __builtin__}: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1651 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1652 | \bcode\begin{verbatim} |
Guido van Rossum | 4bd023f | 1993-10-27 13:49:20 +0000 | [diff] [blame] | 1653 | >>> import __builtin__ |
| 1654 | >>> dir(__builtin__) |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1655 | ['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError', |
| 1656 | 'ImportError', 'IndexError', 'KeyError', 'KeyboardInterrupt', |
| 1657 | 'MemoryError', 'NameError', 'None', 'OverflowError', 'RuntimeError', |
| 1658 | 'SyntaxError', 'SystemError', 'SystemExit', 'TypeError', 'ValueError', |
| 1659 | 'ZeroDivisionError', '__name__', 'abs', 'apply', 'chr', 'cmp', 'coerce', |
| 1660 | 'compile', 'dir', 'divmod', 'eval', 'execfile', 'filter', 'float', |
| 1661 | 'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'len', 'long', |
| 1662 | 'map', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input', |
| 1663 | 'reduce', 'reload', 'repr', 'round', 'setattr', 'str', 'type', 'xrange'] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1664 | >>> |
| 1665 | \end{verbatim}\ecode |
| 1666 | |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 1667 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1668 | \chapter{Output Formatting} |
| 1669 | |
| 1670 | So far we've encountered two ways of writing values: {\em expression |
| 1671 | statements} and the {\tt print} statement. (A third way is using the |
| 1672 | {\tt write} method of file objects; the standard output file can be |
| 1673 | referenced as {\tt sys.stdout}. See the Library Reference for more |
| 1674 | information on this.) |
| 1675 | |
| 1676 | Often you'll want more control over the formatting of your output than |
| 1677 | simply printing space-separated values. The key to nice formatting in |
| 1678 | Python is to do all the string handling yourself; using string slicing |
| 1679 | and concatenation operations you can create any lay-out you can imagine. |
| 1680 | The standard module {\tt string} contains some useful operations for |
| 1681 | padding strings to a given column width; these will be discussed shortly. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1682 | Finally, the \code{\%} operator (modulo) with a string left argument |
| 1683 | interprets this string as a C sprintf format string to be applied to the |
| 1684 | right argument, and returns the string resulting from this formatting |
| 1685 | operation. |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1686 | |
| 1687 | One question remains, of course: how do you convert values to strings? |
| 1688 | Luckily, Python has a way to convert any value to a string: just write |
| 1689 | the value between reverse quotes (\verb/``/). Some examples: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1690 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1691 | \bcode\begin{verbatim} |
| 1692 | >>> x = 10 * 3.14 |
| 1693 | >>> y = 200*200 |
| 1694 | >>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...' |
| 1695 | >>> print s |
| 1696 | The value of x is 31.4, and y is 40000... |
| 1697 | >>> # Reverse quotes work on other types besides numbers: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1698 | ... p = [x, y] |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1699 | >>> ps = `p` |
| 1700 | >>> ps |
| 1701 | '[31.4, 40000]' |
| 1702 | >>> # Converting a string adds string quotes and backslashes: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1703 | ... hello = 'hello, world\n' |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1704 | >>> hellos = `hello` |
| 1705 | >>> print hellos |
| 1706 | 'hello, world\012' |
| 1707 | >>> # The argument of reverse quotes may be a tuple: |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 1708 | ... `x, y, ('spam', 'eggs')` |
| 1709 | "(31.4, 40000, ('spam', 'eggs'))" |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1710 | >>> |
| 1711 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1712 | % |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1713 | Here are two ways to write a table of squares and cubes: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1714 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1715 | \bcode\begin{verbatim} |
| 1716 | >>> import string |
| 1717 | >>> for x in range(1, 11): |
| 1718 | ... print string.rjust(`x`, 2), string.rjust(`x*x`, 3), |
| 1719 | ... # Note trailing comma on previous line |
| 1720 | ... print string.rjust(`x*x*x`, 4) |
| 1721 | ... |
| 1722 | 1 1 1 |
| 1723 | 2 4 8 |
| 1724 | 3 9 27 |
| 1725 | 4 16 64 |
| 1726 | 5 25 125 |
| 1727 | 6 36 216 |
| 1728 | 7 49 343 |
| 1729 | 8 64 512 |
| 1730 | 9 81 729 |
| 1731 | 10 100 1000 |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1732 | >>> for x in range(1,11): |
| 1733 | ... print '%2d %3d %4d' % (x, x*x, x*x*x) |
| 1734 | ... |
| 1735 | 1 1 1 |
| 1736 | 2 4 8 |
| 1737 | 3 9 27 |
| 1738 | 4 16 64 |
| 1739 | 5 25 125 |
| 1740 | 6 36 216 |
| 1741 | 7 49 343 |
| 1742 | 8 64 512 |
| 1743 | 9 81 729 |
| 1744 | 10 100 1000 |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1745 | >>> |
| 1746 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1747 | % |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1748 | (Note that one space between each column was added by the way {\tt print} |
| 1749 | works: it always adds spaces between its arguments.) |
| 1750 | |
| 1751 | This example demonstrates the function {\tt string.rjust()}, which |
| 1752 | right-justifies a string in a field of a given width by padding it with |
| 1753 | spaces on the left. There are similar functions {\tt string.ljust()} |
| 1754 | and {\tt string.center()}. These functions do not write anything, they |
| 1755 | just return a new string. If the input string is too long, they don't |
| 1756 | truncate it, but return it unchanged; this will mess up your column |
| 1757 | lay-out but that's usually better than the alternative, which would be |
| 1758 | lying about a value. (If you really want truncation you can always add |
| 1759 | a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.) |
| 1760 | |
| 1761 | There is another function, {\tt string.zfill}, which pads a numeric |
| 1762 | string on the left with zeros. It understands about plus and minus |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1763 | signs: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1764 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1765 | \bcode\begin{verbatim} |
| 1766 | >>> string.zfill('12', 5) |
| 1767 | '00012' |
| 1768 | >>> string.zfill('-3.14', 7) |
| 1769 | '-003.14' |
| 1770 | >>> string.zfill('3.14159265359', 5) |
| 1771 | '3.14159265359' |
| 1772 | >>> |
| 1773 | \end{verbatim}\ecode |
| 1774 | |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 1775 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1776 | \chapter{Errors and Exceptions} |
| 1777 | |
| 1778 | Until now error messages haven't been more than mentioned, but if you |
| 1779 | have tried out the examples you have probably seen some. There are |
| 1780 | (at least) two distinguishable kinds of errors: {\em syntax\ errors} |
| 1781 | and {\em exceptions}. |
| 1782 | |
| 1783 | \section{Syntax Errors} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1784 | |
| 1785 | 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] | 1786 | kind of complaint you get while you are still learning Python: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1787 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1788 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1789 | >>> while 1 print 'Hello world' |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1790 | File "<stdin>", line 1 |
| 1791 | while 1 print 'Hello world' |
| 1792 | ^ |
| 1793 | SyntaxError: invalid syntax |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1794 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1795 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1796 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1797 | The parser repeats the offending line and displays a little `arrow' |
| 1798 | pointing at the earliest point in the line where the error was detected. |
| 1799 | 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] | 1800 | {\em preceding} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1801 | the arrow: in the example, the error is detected at the keyword |
| 1802 | {\tt print}, since a colon ({\tt :}) is missing before it. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1803 | File name and line number are printed so you know where to look in case |
| 1804 | the input came from a script. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1805 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1806 | \section{Exceptions} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1807 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1808 | Even if a statement or expression is syntactically correct, it may |
| 1809 | cause an error when an attempt is made to execute it. |
| 1810 | Errors detected during execution are called {\em exceptions} and are |
| 1811 | not unconditionally fatal: you will soon learn how to handle them in |
| 1812 | Python programs. Most exceptions are not handled by programs, |
| 1813 | however, and result in error messages as shown here: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1814 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1815 | \bcode\small\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1816 | >>> 10 * (1/0) |
Guido van Rossum | 3cbc16d | 1993-12-17 12:13:53 +0000 | [diff] [blame] | 1817 | Traceback (innermost last): |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1818 | File "<stdin>", line 1 |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1819 | ZeroDivisionError: integer division or modulo |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 1820 | >>> 4 + spam*3 |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1821 | Traceback (innermost last): |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1822 | File "<stdin>", line 1 |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 1823 | NameError: spam |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1824 | >>> '2' + 2 |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1825 | Traceback (innermost last): |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1826 | File "<stdin>", line 1 |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1827 | TypeError: illegal argument type for built-in operation |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1828 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1829 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1830 | % |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1831 | The last line of the error message indicates what happened. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1832 | Exceptions come in different types, and the type is printed as part of |
| 1833 | the message: the types in the example are |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1834 | {\tt ZeroDivisionError}, |
| 1835 | {\tt NameError} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1836 | and |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1837 | {\tt TypeError}. |
| 1838 | The string printed as the exception type is the name of the built-in |
| 1839 | name for the exception that occurred. This is true for all built-in |
| 1840 | exceptions, but need not be true for user-defined exceptions (although |
| 1841 | it is a useful convention). |
| 1842 | Standard exception names are built-in identifiers (not reserved |
| 1843 | keywords). |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1844 | |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1845 | The rest of the line is a detail whose interpretation depends on the |
| 1846 | exception type; its meaning is dependent on the exception type. |
| 1847 | |
| 1848 | The preceding part of the error message shows the context where the |
| 1849 | exception happened, in the form of a stack backtrace. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1850 | In general it contains a stack backtrace listing source lines; however, |
| 1851 | it will not display lines read from standard input. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1852 | |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1853 | The Python library reference manual lists the built-in exceptions and |
| 1854 | their meanings. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1855 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1856 | \section{Handling Exceptions} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1857 | |
| 1858 | It is possible to write programs that handle selected exceptions. |
| 1859 | Look at the following example, which prints a table of inverses of |
| 1860 | some floating point numbers: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1861 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1862 | \bcode\begin{verbatim} |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1863 | >>> numbers = [0.3333, 2.5, 0, 10] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1864 | >>> for x in numbers: |
| 1865 | ... print x, |
| 1866 | ... try: |
| 1867 | ... print 1.0 / x |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1868 | ... except ZeroDivisionError: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1869 | ... print '*** has no inverse ***' |
| 1870 | ... |
| 1871 | 0.3333 3.00030003 |
| 1872 | 2.5 0.4 |
| 1873 | 0 *** has no inverse *** |
| 1874 | 10 0.1 |
| 1875 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1876 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1877 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1878 | The {\tt try} statement works as follows. |
| 1879 | \begin{itemize} |
| 1880 | \item |
| 1881 | First, the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1882 | {\em try\ clause} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1883 | (the statement(s) between the {\tt try} and {\tt except} keywords) is |
| 1884 | executed. |
| 1885 | \item |
| 1886 | If no exception occurs, the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1887 | {\em except\ clause} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1888 | is skipped and execution of the {\tt try} statement is finished. |
| 1889 | \item |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1890 | If an exception occurs during execution of the try clause, |
| 1891 | the rest of the clause is skipped. Then if |
| 1892 | its type matches the exception named after the {\tt except} keyword, |
| 1893 | the rest of the try clause is skipped, the except clause is executed, |
| 1894 | and then execution continues after the {\tt try} statement. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1895 | \item |
| 1896 | If an exception occurs which does not match the exception named in the |
| 1897 | except clause, it is passed on to outer try statements; if no handler is |
| 1898 | found, it is an |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1899 | {\em unhandled\ exception} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1900 | and execution stops with a message as shown above. |
| 1901 | \end{itemize} |
| 1902 | A {\tt try} statement may have more than one except clause, to specify |
| 1903 | handlers for different exceptions. |
| 1904 | At most one handler will be executed. |
| 1905 | Handlers only handle exceptions that occur in the corresponding try |
| 1906 | clause, not in other handlers of the same {\tt try} statement. |
| 1907 | An except clause may name multiple exceptions as a parenthesized list, |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1908 | e.g.: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1909 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1910 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1911 | ... except (RuntimeError, TypeError, NameError): |
| 1912 | ... pass |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1913 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1914 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1915 | The last except clause may omit the exception name(s), to serve as a |
| 1916 | wildcard. |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1917 | Use this with extreme caution, since it is easy to mask a real |
| 1918 | programming error in this way! |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1919 | |
| 1920 | When an exception occurs, it may have an associated value, also known as |
| 1921 | the exceptions's |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1922 | {\em argument}. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1923 | The presence and type of the argument depend on the exception type. |
| 1924 | For exception types which have an argument, the except clause may |
| 1925 | specify a variable after the exception name (or list) to receive the |
| 1926 | argument's value, as follows: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1927 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1928 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1929 | >>> try: |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 1930 | ... spam() |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1931 | ... except NameError, x: |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1932 | ... print 'name', x, 'undefined' |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1933 | ... |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 1934 | name spam undefined |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1935 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1936 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1937 | % |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1938 | If an exception has an argument, it is printed as the last part |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1939 | (`detail') of the message for unhandled exceptions. |
| 1940 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1941 | Exception handlers don't just handle exceptions if they occur |
| 1942 | immediately in the try clause, but also if they occur inside functions |
| 1943 | that are called (even indirectly) in the try clause. |
| 1944 | For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1945 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1946 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1947 | >>> def this_fails(): |
| 1948 | ... x = 1/0 |
| 1949 | ... |
| 1950 | >>> try: |
| 1951 | ... this_fails() |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1952 | ... except ZeroDivisionError, detail: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1953 | ... print 'Handling run-time error:', detail |
| 1954 | ... |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1955 | Handling run-time error: integer division or modulo |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1956 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1957 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1958 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1959 | \section{Raising Exceptions} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1960 | |
| 1961 | The {\tt raise} statement allows the programmer to force a specified |
| 1962 | exception to occur. |
| 1963 | For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1964 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1965 | \bcode\begin{verbatim} |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1966 | >>> raise NameError, 'HiThere' |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1967 | Traceback (innermost last): |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1968 | File "<stdin>", line 1 |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1969 | NameError: HiThere |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1970 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1971 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1972 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1973 | The first argument to {\tt raise} names the exception to be raised. |
| 1974 | The optional second argument specifies the exception's argument. |
| 1975 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 1976 | \section{User-defined Exceptions} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1977 | |
| 1978 | Programs may name their own exceptions by assigning a string to a |
| 1979 | variable. |
| 1980 | For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1981 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1982 | \bcode\begin{verbatim} |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1983 | >>> my_exc = 'my_exc' |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1984 | >>> try: |
| 1985 | ... raise my_exc, 2*2 |
| 1986 | ... except my_exc, val: |
Guido van Rossum | 67fa160 | 1991-04-23 14:14:57 +0000 | [diff] [blame] | 1987 | ... print 'My exception occurred, value:', val |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1988 | ... |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1989 | My exception occurred, value: 4 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1990 | >>> raise my_exc, 1 |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1991 | Traceback (innermost last): |
| 1992 | File "<stdin>", line 1 |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 1993 | my_exc: 1 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1994 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1995 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 1996 | % |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1997 | Many standard modules use this to report errors that may occur in |
| 1998 | functions they define. |
| 1999 | |
Guido van Rossum | 6fc178f | 1991-08-16 09:13:42 +0000 | [diff] [blame] | 2000 | \section{Defining Clean-up Actions} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 2001 | |
| 2002 | The {\tt try} statement has another optional clause which is intended to |
| 2003 | define clean-up actions that must be executed under all circumstances. |
| 2004 | For example: |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 2005 | |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 2006 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 2007 | >>> try: |
| 2008 | ... raise KeyboardInterrupt |
| 2009 | ... finally: |
| 2010 | ... print 'Goodbye, world!' |
| 2011 | ... |
| 2012 | Goodbye, world! |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2013 | Traceback (innermost last): |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 2014 | File "<stdin>", line 2 |
Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 2015 | KeyboardInterrupt |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 2016 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 2017 | \end{verbatim}\ecode |
Guido van Rossum | a8d754e | 1992-01-07 16:44:35 +0000 | [diff] [blame] | 2018 | % |
Guido van Rossum | da8c3fd | 1992-08-09 13:55:25 +0000 | [diff] [blame] | 2019 | A {\tt finally} clause is executed whether or not an exception has |
| 2020 | occurred in the {\tt try} clause. When an exception has occurred, it |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2021 | is re-raised after the {\tt finally} clause is executed. The |
Guido van Rossum | da8c3fd | 1992-08-09 13:55:25 +0000 | [diff] [blame] | 2022 | {\tt finally} clause is also executed ``on the way out'' when the |
| 2023 | {\tt try} statement is left via a {\tt break} or {\tt return} |
| 2024 | statement. |
| 2025 | |
| 2026 | A {\tt try} statement must either have one or more {\tt except} |
| 2027 | clauses or one {\tt finally} clause, but not both. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 2028 | |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2029 | |
| 2030 | \chapter{Classes} |
| 2031 | |
| 2032 | Python's class mechanism adds classes to the language with a minimum |
| 2033 | of new syntax and semantics. It is a mixture of the class mechanisms |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2034 | found in \Cpp{} and Modula-3. As is true for modules, classes in Python |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2035 | do not put an absolute barrier between definition and user, but rather |
| 2036 | rely on the politeness of the user not to ``break into the |
| 2037 | definition.'' The most important features of classes are retained |
| 2038 | with full power, however: the class inheritance mechanism allows |
| 2039 | multiple base classes, a derived class can override any methods of its |
| 2040 | base class(es), a method can call the method of a base class with the |
| 2041 | same name. Objects can contain an arbitrary amount of private data. |
| 2042 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2043 | In \Cpp{} terminology, all class members (including the data members) are |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2044 | {\em public}, and all member functions are {\em virtual}. There are |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2045 | no special constructors or destructors. As in Modula-3, there are no |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2046 | shorthands for referencing the object's members from its methods: the |
| 2047 | method function is declared with an explicit first argument |
| 2048 | representing the object, which is provided implicitly by the call. As |
| 2049 | in Smalltalk, classes themselves are objects, albeit in the wider |
| 2050 | sense of the word: in Python, all data types are objects. This |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2051 | provides semantics for importing and renaming. But, just like in \Cpp{} |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2052 | or Modula-3, built-in types cannot be used as base classes for |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2053 | extension by the user. Also, like in \Cpp{} but unlike in Modula-3, most |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2054 | built-in operators with special syntax (arithmetic operators, |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2055 | subscripting etc.) can be redefined for class members. |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2056 | |
| 2057 | |
| 2058 | \section{A word about terminology} |
| 2059 | |
| 2060 | Lacking universally accepted terminology to talk about classes, I'll |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2061 | make occasional use of Smalltalk and \Cpp{} terms. (I'd use Modula-3 |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2062 | terms, since its object-oriented semantics are closer to those of |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2063 | Python than \Cpp{}, but I expect that few readers have heard of it...) |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2064 | |
| 2065 | I also have to warn you that there's a terminological pitfall for |
| 2066 | object-oriented readers: the word ``object'' in Python does not |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2067 | necessarily mean a class instance. Like \Cpp{} and Modula-3, and unlike |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2068 | Smalltalk, not all types in Python are classes: the basic built-in |
| 2069 | types like integers and lists aren't, and even somewhat more exotic |
| 2070 | types like files aren't. However, {\em all} Python types share a little |
| 2071 | bit of common semantics that is best described by using the word |
| 2072 | object. |
| 2073 | |
| 2074 | Objects have individuality, and multiple names (in multiple scopes) |
| 2075 | can be bound to the same object. This is known as aliasing in other |
| 2076 | languages. This is usually not appreciated on a first glance at |
| 2077 | Python, and can be safely ignored when dealing with immutable basic |
| 2078 | types (numbers, strings, tuples). However, aliasing has an |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2079 | (intended!) effect on the semantics of Python code involving mutable |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2080 | objects such as lists, dictionaries, and most types representing |
| 2081 | entities outside the program (files, windows, etc.). This is usually |
| 2082 | used to the benefit of the program, since aliases behave like pointers |
| 2083 | in some respects. For example, passing an object is cheap since only |
| 2084 | a pointer is passed by the implementation; and if a function modifies |
| 2085 | an object passed as an argument, the caller will see the change --- this |
| 2086 | obviates the need for two different argument passing mechanisms as in |
| 2087 | Pascal. |
| 2088 | |
| 2089 | |
| 2090 | \section{Python scopes and name spaces} |
| 2091 | |
| 2092 | Before introducing classes, I first have to tell you something about |
| 2093 | Python's scope rules. Class definitions play some neat tricks with |
| 2094 | name spaces, and you need to know how scopes and name spaces work to |
| 2095 | fully understand what's going on. Incidentally, knowledge about this |
| 2096 | subject is useful for any advanced Python programmer. |
| 2097 | |
| 2098 | Let's begin with some definitions. |
| 2099 | |
| 2100 | A {\em name space} is a mapping from names to objects. Most name |
| 2101 | spaces are currently implemented as Python dictionaries, but that's |
| 2102 | normally not noticeable in any way (except for performance), and it |
| 2103 | may change in the future. Examples of name spaces are: the set of |
| 2104 | built-in names (functions such as \verb\abs()\, and built-in exception |
| 2105 | names); the global names in a module; and the local names in a |
| 2106 | function invocation. In a sense the set of attributes of an object |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 2107 | also form a name space. The important thing to know about name |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2108 | spaces is that there is absolutely no relation between names in |
| 2109 | different name spaces; for instance, two different modules may both |
| 2110 | define a function ``maximize'' without confusion --- users of the |
| 2111 | modules must prefix it with the module name. |
| 2112 | |
| 2113 | By the way, I use the word {\em attribute} for any name following a |
| 2114 | dot --- for example, in the expression \verb\z.real\, \verb\real\ is |
| 2115 | an attribute of the object \verb\z\. Strictly speaking, references to |
| 2116 | names in modules are attribute references: in the expression |
| 2117 | \verb\modname.funcname\, \verb\modname\ is a module object and |
| 2118 | \verb\funcname\ is an attribute of it. In this case there happens to |
| 2119 | be a straightforward mapping between the module's attributes and the |
| 2120 | global names defined in the module: they share the same name space!% |
| 2121 | \footnote{ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2122 | Except for one thing. Module objects have a secret read-only |
| 2123 | attribute called {\tt __dict__} which returns the dictionary |
| 2124 | used to implement the module's name space; the name |
| 2125 | {\tt __dict__} is an attribute but not a global name. |
| 2126 | Obviously, using this violates the abstraction of name space |
| 2127 | implementation, and should be restricted to things like |
| 2128 | post-mortem debuggers... |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | Attributes may be read-only or writable. In the latter case, |
| 2132 | assignment to attributes is possible. Module attributes are writable: |
| 2133 | you can write \verb\modname.the_answer = 42\. Writable attributes may |
| 2134 | also be deleted with the del statement, e.g. |
| 2135 | \verb\del modname.the_answer\. |
| 2136 | |
| 2137 | Name spaces are created at different moments and have different |
| 2138 | lifetimes. The name space containing the built-in names is created |
| 2139 | when the Python interpreter starts up, and is never deleted. The |
| 2140 | global name space for a module is created when the module definition |
| 2141 | is read in; normally, module name spaces also last until the |
| 2142 | interpreter quits. The statements executed by the top-level |
| 2143 | invocation of the interpreter, either read from a script file or |
| 2144 | interactively, are considered part of a module called \verb\__main__\, |
| 2145 | so they have their own global name space. (The built-in names |
Guido van Rossum | 4bd023f | 1993-10-27 13:49:20 +0000 | [diff] [blame] | 2146 | actually also live in a module; this is called \verb\__builtin__\.) |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2147 | |
| 2148 | The local name space for a function is created when the function is |
| 2149 | called, and deleted when the function returns or raises an exception |
| 2150 | that is not handled within the function. (Actually, forgetting would |
| 2151 | be a better way to describe what actually happens.) Of course, |
| 2152 | recursive invocations each have their own local name space. |
| 2153 | |
| 2154 | A {\em scope} is a textual region of a Python program where a name space |
| 2155 | is directly accessible. ``Directly accessible'' here means that an |
| 2156 | unqualified reference to a name attempts to find the name in the name |
| 2157 | space. |
| 2158 | |
| 2159 | Although scopes are determined statically, they are used dynamically. |
| 2160 | At any time during execution, exactly three nested scopes are in use |
| 2161 | (i.e., exactly three name spaces are directly accessible): the |
| 2162 | innermost scope, which is searched first, contains the local names, |
| 2163 | the middle scope, searched next, contains the current module's global |
| 2164 | names, and the outermost scope (searched last) is the name space |
| 2165 | containing built-in names. |
| 2166 | |
| 2167 | Usually, the local scope references the local names of the (textually) |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 2168 | current function. Outside of functions, the local scope references |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2169 | the same name space as the global scope: the module's name space. |
| 2170 | Class definitions place yet another name space in the local scope. |
| 2171 | |
| 2172 | It is important to realize that scopes are determined textually: the |
| 2173 | global scope of a function defined in a module is that module's name |
| 2174 | space, no matter from where or by what alias the function is called. |
| 2175 | On the other hand, the actual search for names is done dynamically, at |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 2176 | run time --- however, the language definition is evolving towards |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2177 | static name resolution, at ``compile'' time, so don't rely on dynamic |
| 2178 | name resolution! (In fact, local variables are already determined |
| 2179 | statically.) |
| 2180 | |
| 2181 | A special quirk of Python is that assignments always go into the |
| 2182 | innermost scope. Assignments do not copy data --- they just |
| 2183 | bind names to objects. The same is true for deletions: the statement |
| 2184 | \verb\del x\ removes the binding of x from the name space referenced by the |
| 2185 | local scope. In fact, all operations that introduce new names use the |
| 2186 | local scope: in particular, import statements and function definitions |
| 2187 | bind the module or function name in the local scope. (The |
| 2188 | \verb\global\ statement can be used to indicate that particular |
| 2189 | variables live in the global scope.) |
| 2190 | |
| 2191 | |
| 2192 | \section{A first look at classes} |
| 2193 | |
| 2194 | Classes introduce a little bit of new syntax, three new object types, |
| 2195 | and some new semantics. |
| 2196 | |
| 2197 | |
| 2198 | \subsection{Class definition syntax} |
| 2199 | |
| 2200 | The simplest form of class definition looks like this: |
| 2201 | |
| 2202 | \begin{verbatim} |
| 2203 | class ClassName: |
| 2204 | <statement-1> |
| 2205 | . |
| 2206 | . |
| 2207 | . |
| 2208 | <statement-N> |
| 2209 | \end{verbatim} |
| 2210 | |
| 2211 | Class definitions, like function definitions (\verb\def\ statements) |
| 2212 | must be executed before they have any effect. (You could conceivably |
| 2213 | place a class definition in a branch of an \verb\if\ statement, or |
| 2214 | inside a function.) |
| 2215 | |
| 2216 | In practice, the statements inside a class definition will usually be |
| 2217 | function definitions, but other statements are allowed, and sometimes |
| 2218 | useful --- we'll come back to this later. The function definitions |
| 2219 | inside a class normally have a peculiar form of argument list, |
| 2220 | dictated by the calling conventions for methods --- again, this is |
| 2221 | explained later. |
| 2222 | |
| 2223 | When a class definition is entered, a new name space is created, and |
| 2224 | used as the local scope --- thus, all assignments to local variables |
| 2225 | go into this new name space. In particular, function definitions bind |
| 2226 | the name of the new function here. |
| 2227 | |
| 2228 | When a class definition is left normally (via the end), a {\em class |
| 2229 | object} is created. This is basically a wrapper around the contents |
| 2230 | of the name space created by the class definition; we'll learn more |
| 2231 | about class objects in the next section. The original local scope |
| 2232 | (the one in effect just before the class definitions was entered) is |
| 2233 | reinstated, and the class object is bound here to class name given in |
| 2234 | the class definition header (ClassName in the example). |
| 2235 | |
| 2236 | |
| 2237 | \subsection{Class objects} |
| 2238 | |
| 2239 | Class objects support two kinds of operations: attribute references |
| 2240 | and instantiation. |
| 2241 | |
| 2242 | {\em Attribute references} use the standard syntax used for all |
| 2243 | attribute references in Python: \verb\obj.name\. Valid attribute |
| 2244 | names are all the names that were in the class's name space when the |
| 2245 | class object was created. So, if the class definition looked like |
| 2246 | this: |
| 2247 | |
| 2248 | \begin{verbatim} |
| 2249 | class MyClass: |
| 2250 | i = 12345 |
| 2251 | def f(x): |
| 2252 | return 'hello world' |
| 2253 | \end{verbatim} |
| 2254 | |
| 2255 | then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute |
| 2256 | references, returning an integer and a function object, respectively. |
| 2257 | Class attributes can also be assigned to, so you can change the |
| 2258 | value of \verb\MyClass.i\ by assignment. |
| 2259 | |
| 2260 | Class {\em instantiation} uses function notation. Just pretend that |
| 2261 | the class object is a parameterless function that returns a new |
| 2262 | instance of the class. For example, (assuming the above class): |
| 2263 | |
| 2264 | \begin{verbatim} |
| 2265 | x = MyClass() |
| 2266 | \end{verbatim} |
| 2267 | |
| 2268 | creates a new {\em instance} of the class and assigns this object to |
| 2269 | the local variable \verb\x\. |
| 2270 | |
| 2271 | |
| 2272 | \subsection{Instance objects} |
| 2273 | |
| 2274 | Now what can we do with instance objects? The only operations |
| 2275 | understood by instance objects are attribute references. There are |
| 2276 | two kinds of valid attribute names. |
| 2277 | |
| 2278 | The first I'll call {\em data attributes}. These correspond to |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2279 | ``instance variables'' in Smalltalk, and to ``data members'' in \Cpp{}. |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2280 | Data attributes need not be declared; like local variables, they |
| 2281 | spring into existence when they are first assigned to. For example, |
| 2282 | if \verb\x\ in the instance of \verb\MyClass\ created above, the |
| 2283 | following piece of code will print the value 16, without leaving a |
| 2284 | trace: |
| 2285 | |
| 2286 | \begin{verbatim} |
| 2287 | x.counter = 1 |
| 2288 | while x.counter < 10: |
| 2289 | x.counter = x.counter * 2 |
| 2290 | print x.counter |
| 2291 | del x.counter |
| 2292 | \end{verbatim} |
| 2293 | |
| 2294 | The second kind of attribute references understood by instance objects |
| 2295 | are {\em methods}. A method is a function that ``belongs to'' an |
| 2296 | object. (In Python, the term method is not unique to class instances: |
| 2297 | other object types can have methods as well, e.g., list objects have |
| 2298 | methods called append, insert, remove, sort, and so on. However, |
| 2299 | below, we'll use the term method exclusively to mean methods of class |
| 2300 | instance objects, unless explicitly stated otherwise.) |
| 2301 | |
| 2302 | Valid method names of an instance object depend on its class. By |
| 2303 | definition, all attributes of a class that are (user-defined) function |
| 2304 | objects define corresponding methods of its instances. So in our |
| 2305 | example, \verb\x.f\ is a valid method reference, since |
| 2306 | \verb\MyClass.f\ is a function, but \verb\x.i\ is not, since |
| 2307 | \verb\MyClass.i\ is not. But \verb\x.f\ is not the |
| 2308 | same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a |
| 2309 | function object. |
| 2310 | |
| 2311 | |
| 2312 | \subsection{Method objects} |
| 2313 | |
| 2314 | Usually, a method is called immediately, e.g.: |
| 2315 | |
| 2316 | \begin{verbatim} |
| 2317 | x.f() |
| 2318 | \end{verbatim} |
| 2319 | |
| 2320 | In our example, this will return the string \verb\'hello world'\. |
| 2321 | However, it is not necessary to call a method right away: \verb\x.f\ |
| 2322 | is a method object, and can be stored away and called at a later |
| 2323 | moment, for example: |
| 2324 | |
| 2325 | \begin{verbatim} |
| 2326 | xf = x.f |
| 2327 | while 1: |
| 2328 | print xf() |
| 2329 | \end{verbatim} |
| 2330 | |
| 2331 | will continue to print \verb\hello world\ until the end of time. |
| 2332 | |
| 2333 | What exactly happens when a method is called? You may have noticed |
| 2334 | that \verb\x.f()\ was called without an argument above, even though |
| 2335 | the function definition for \verb\f\ specified an argument. What |
| 2336 | happened to the argument? Surely Python raises an exception when a |
| 2337 | function that requires an argument is called without any --- even if |
| 2338 | the argument isn't actually used... |
| 2339 | |
| 2340 | Actually, you may have guessed the answer: the special thing about |
| 2341 | methods is that the object is passed as the first argument of the |
| 2342 | function. In our example, the call \verb\x.f()\ is exactly equivalent |
| 2343 | to \verb\MyClass.f(x)\. In general, calling a method with a list of |
| 2344 | {\em n} arguments is equivalent to calling the corresponding function |
| 2345 | with an argument list that is created by inserting the method's object |
| 2346 | before the first argument. |
| 2347 | |
| 2348 | If you still don't understand how methods work, a look at the |
| 2349 | implementation can perhaps clarify matters. When an instance |
| 2350 | attribute is referenced that isn't a data attribute, its class is |
| 2351 | searched. If the name denotes a valid class attribute that is a |
| 2352 | function object, a method object is created by packing (pointers to) |
| 2353 | the instance object and the function object just found together in an |
| 2354 | abstract object: this is the method object. When the method object is |
| 2355 | called with an argument list, it is unpacked again, a new argument |
| 2356 | list is constructed from the instance object and the original argument |
| 2357 | list, and the function object is called with this new argument list. |
| 2358 | |
| 2359 | |
| 2360 | \section{Random remarks} |
| 2361 | |
| 2362 | |
| 2363 | [These should perhaps be placed more carefully...] |
| 2364 | |
| 2365 | |
| 2366 | Data attributes override method attributes with the same name; to |
| 2367 | avoid accidental name conflicts, which may cause hard-to-find bugs in |
| 2368 | large programs, it is wise to use some kind of convention that |
| 2369 | minimizes the chance of conflicts, e.g., capitalize method names, |
| 2370 | prefix data attribute names with a small unique string (perhaps just |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2371 | an underscore), or use verbs for methods and nouns for data attributes. |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2372 | |
| 2373 | |
| 2374 | Data attributes may be referenced by methods as well as by ordinary |
| 2375 | users (``clients'') of an object. In other words, classes are not |
| 2376 | usable to implement pure abstract data types. In fact, nothing in |
| 2377 | Python makes it possible to enforce data hiding --- it is all based |
| 2378 | upon convention. (On the other hand, the Python implementation, |
| 2379 | written in C, can completely hide implementation details and control |
| 2380 | access to an object if necessary; this can be used by extensions to |
| 2381 | Python written in C.) |
| 2382 | |
| 2383 | |
| 2384 | Clients should use data attributes with care --- clients may mess up |
| 2385 | invariants maintained by the methods by stamping on their data |
| 2386 | attributes. Note that clients may add data attributes of their own to |
| 2387 | an instance object without affecting the validity of the methods, as |
| 2388 | long as name conflicts are avoided --- again, a naming convention can |
| 2389 | save a lot of headaches here. |
| 2390 | |
| 2391 | |
| 2392 | There is no shorthand for referencing data attributes (or other |
| 2393 | methods!) from within methods. I find that this actually increases |
| 2394 | the readability of methods: there is no chance of confusing local |
| 2395 | variables and instance variables when glancing through a method. |
| 2396 | |
| 2397 | |
| 2398 | Conventionally, the first argument of methods is often called |
| 2399 | \verb\self\. This is nothing more than a convention: the name |
| 2400 | \verb\self\ has absolutely no special meaning to Python. (Note, |
| 2401 | however, that by not following the convention your code may be less |
| 2402 | readable by other Python programmers, and it is also conceivable that |
| 2403 | a {\em class browser} program be written which relies upon such a |
| 2404 | convention.) |
| 2405 | |
| 2406 | |
| 2407 | Any function object that is a class attribute defines a method for |
| 2408 | instances of that class. It is not necessary that the function |
| 2409 | definition is textually enclosed in the class definition: assigning a |
| 2410 | function object to a local variable in the class is also ok. For |
| 2411 | example: |
| 2412 | |
| 2413 | \begin{verbatim} |
| 2414 | # Function defined outside the class |
| 2415 | def f1(self, x, y): |
| 2416 | return min(x, x+y) |
| 2417 | |
| 2418 | class C: |
| 2419 | f = f1 |
| 2420 | def g(self): |
| 2421 | return 'hello world' |
| 2422 | h = g |
| 2423 | \end{verbatim} |
| 2424 | |
| 2425 | Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class |
| 2426 | \verb\C\ that refer to function objects, and consequently they are all |
| 2427 | methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent |
| 2428 | to \verb\g\. Note that this practice usually only serves to confuse |
| 2429 | the reader of a program. |
| 2430 | |
| 2431 | |
| 2432 | Methods may call other methods by using method attributes of the |
| 2433 | \verb\self\ argument, e.g.: |
| 2434 | |
| 2435 | \begin{verbatim} |
| 2436 | class Bag: |
| 2437 | def empty(self): |
| 2438 | self.data = [] |
| 2439 | def add(self, x): |
| 2440 | self.data.append(x) |
| 2441 | def addtwice(self, x): |
Guido van Rossum | 084b0b2 | 1992-08-14 09:19:56 +0000 | [diff] [blame] | 2442 | self.add(x) |
| 2443 | self.add(x) |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2444 | \end{verbatim} |
| 2445 | |
| 2446 | |
| 2447 | The instantiation operation (``calling'' a class object) creates an |
| 2448 | empty object. Many classes like to create objects in a known initial |
Guido van Rossum | ca3f6c8 | 1994-10-06 14:08:53 +0000 | [diff] [blame] | 2449 | state. Therefore a class may define a special method named |
| 2450 | \verb\__init__\, like this: |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2451 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2452 | \begin{verbatim} |
| 2453 | def __init__(self): |
| 2454 | self.empty() |
| 2455 | \end{verbatim} |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2456 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2457 | When a class defines an \verb\__init__\ method, class instantiation |
| 2458 | automatically invokes \verb\__init__\ for the newly-created class |
| 2459 | instance. So in the \verb\Bag\ example, a new and initialized instance |
| 2460 | can be obtained by: |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2461 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2462 | \begin{verbatim} |
| 2463 | x = Bag() |
| 2464 | \end{verbatim} |
| 2465 | |
| 2466 | Of course, the \verb\__init__\ method may have arguments for greater |
| 2467 | flexibility. In that case, arguments given to the class instantiation |
| 2468 | operator are passed on to \verb\__init__\. For example, |
| 2469 | |
| 2470 | \bcode\begin{verbatim} |
| 2471 | >>> class Complex: |
| 2472 | ... def __init__(self, realpart, imagpart): |
| 2473 | ... self.r = realpart |
| 2474 | ... self.i = imagpart |
| 2475 | ... |
| 2476 | >>> x = Complex(3.0,-4.5) |
| 2477 | >>> x.r, x.i |
| 2478 | (3.0, -4.5) |
| 2479 | >>> |
| 2480 | \end{verbatim}\ecode |
| 2481 | % |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2482 | Methods may reference global names in the same way as ordinary |
| 2483 | functions. The global scope associated with a method is the module |
| 2484 | containing the class definition. (The class itself is never used as a |
| 2485 | global scope!) While one rarely encounters a good reason for using |
| 2486 | global data in a method, there are many legitimate uses of the global |
| 2487 | scope: for one thing, functions and modules imported into the global |
| 2488 | scope can be used by methods, as well as functions and classes defined |
| 2489 | in it. Usually, the class containing the method is itself defined in |
| 2490 | this global scope, and in the next section we'll find some good |
| 2491 | reasons why a method would want to reference its own class! |
| 2492 | |
| 2493 | |
| 2494 | \section{Inheritance} |
| 2495 | |
| 2496 | Of course, a language feature would not be worthy of the name ``class'' |
| 2497 | without supporting inheritance. The syntax for a derived class |
| 2498 | definition looks as follows: |
| 2499 | |
| 2500 | \begin{verbatim} |
| 2501 | class DerivedClassName(BaseClassName): |
| 2502 | <statement-1> |
| 2503 | . |
| 2504 | . |
| 2505 | . |
| 2506 | <statement-N> |
| 2507 | \end{verbatim} |
| 2508 | |
| 2509 | The name \verb\BaseClassName\ must be defined in a scope containing |
| 2510 | the derived class definition. Instead of a base class name, an |
| 2511 | expression is also allowed. This is useful when the base class is |
| 2512 | defined in another module, e.g., |
| 2513 | |
| 2514 | \begin{verbatim} |
| 2515 | class DerivedClassName(modname.BaseClassName): |
| 2516 | \end{verbatim} |
| 2517 | |
| 2518 | Execution of a derived class definition proceeds the same as for a |
| 2519 | base class. When the class object is constructed, the base class is |
| 2520 | remembered. This is used for resolving attribute references: if a |
| 2521 | requested attribute is not found in the class, it is searched in the |
| 2522 | base class. This rule is applied recursively if the base class itself |
| 2523 | is derived from some other class. |
| 2524 | |
| 2525 | There's nothing special about instantiation of derived classes: |
| 2526 | \verb\DerivedClassName()\ creates a new instance of the class. Method |
| 2527 | references are resolved as follows: the corresponding class attribute |
| 2528 | is searched, descending down the chain of base classes if necessary, |
| 2529 | and the method reference is valid if this yields a function object. |
| 2530 | |
| 2531 | Derived classes may override methods of their base classes. Because |
| 2532 | methods have no special privileges when calling other methods of the |
| 2533 | same object, a method of a base class that calls another method |
| 2534 | defined in the same base class, may in fact end up calling a method of |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2535 | a derived class that overrides it. (For \Cpp{} programmers: all methods |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2536 | in Python are ``virtual functions''.) |
| 2537 | |
| 2538 | An overriding method in a derived class may in fact want to extend |
| 2539 | rather than simply replace the base class method of the same name. |
| 2540 | There is a simple way to call the base class method directly: just |
| 2541 | call \verb\BaseClassName.methodname(self, arguments)\. This is |
| 2542 | occasionally useful to clients as well. (Note that this only works if |
| 2543 | the base class is defined or imported directly in the global scope.) |
| 2544 | |
| 2545 | |
| 2546 | \subsection{Multiple inheritance} |
| 2547 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2548 | Python supports a limited form of multiple inheritance as well. A |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2549 | class definition with multiple base classes looks as follows: |
| 2550 | |
| 2551 | \begin{verbatim} |
| 2552 | class DerivedClassName(Base1, Base2, Base3): |
| 2553 | <statement-1> |
| 2554 | . |
| 2555 | . |
| 2556 | . |
| 2557 | <statement-N> |
| 2558 | \end{verbatim} |
| 2559 | |
| 2560 | The only rule necessary to explain the semantics is the resolution |
| 2561 | rule used for class attribute references. This is depth-first, |
| 2562 | left-to-right. Thus, if an attribute is not found in |
| 2563 | \verb\DerivedClassName\, it is searched in \verb\Base1\, then |
| 2564 | (recursively) in the base classes of \verb\Base1\, and only if it is |
| 2565 | not found there, it is searched in \verb\Base2\, and so on. |
| 2566 | |
Guido van Rossum | 95cd2ef | 1992-12-08 14:37:55 +0000 | [diff] [blame] | 2567 | (To some people breadth first---searching \verb\Base2\ and |
| 2568 | \verb\Base3\ before the base classes of \verb\Base1\---looks more |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2569 | natural. However, this would require you to know whether a particular |
| 2570 | attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in |
| 2571 | one of its base classes before you can figure out the consequences of |
| 2572 | a name conflict with an attribute of \verb\Base2\. The depth-first |
| 2573 | rule makes no differences between direct and inherited attributes of |
| 2574 | \verb\Base1\.) |
| 2575 | |
| 2576 | It is clear that indiscriminate use of multiple inheritance is a |
| 2577 | maintenance nightmare, given the reliance in Python on conventions to |
| 2578 | avoid accidental name conflicts. A well-known problem with multiple |
| 2579 | inheritance is a class derived from two classes that happen to have a |
| 2580 | common base class. While it is easy enough to figure out what happens |
| 2581 | in this case (the instance will have a single copy of ``instance |
| 2582 | variables'' or data attributes used by the common base class), it is |
| 2583 | not clear that these semantics are in any way useful. |
| 2584 | |
| 2585 | |
| 2586 | \section{Odds and ends} |
| 2587 | |
| 2588 | Sometimes it is useful to have a data type similar to the Pascal |
| 2589 | ``record'' or C ``struct'', bundling together a couple of named data |
| 2590 | items. An empty class definition will do nicely, e.g.: |
| 2591 | |
| 2592 | \begin{verbatim} |
| 2593 | class Employee: |
| 2594 | pass |
| 2595 | |
| 2596 | john = Employee() # Create an empty employee record |
| 2597 | |
| 2598 | # Fill the fields of the record |
| 2599 | john.name = 'John Doe' |
| 2600 | john.dept = 'computer lab' |
| 2601 | john.salary = 1000 |
| 2602 | \end{verbatim} |
| 2603 | |
| 2604 | |
| 2605 | A piece of Python code that expects a particular abstract data type |
| 2606 | can often be passed a class that emulates the methods of that data |
| 2607 | type instead. For instance, if you have a function that formats some |
| 2608 | data from a file object, you can define a class with methods |
| 2609 | \verb\read()\ and \verb\readline()\ that gets the data from a string |
| 2610 | buffer instead, and pass it as an argument. (Unfortunately, this |
| 2611 | technique has its limitations: a class can't define operations that |
| 2612 | are accessed by special syntax such as sequence subscripting or |
| 2613 | arithmetic operators, and assigning such a ``pseudo-file'' to |
| 2614 | \verb\sys.stdin\ will not cause the interpreter to read further input |
| 2615 | from it.) |
| 2616 | |
| 2617 | |
| 2618 | Instance method objects have attributes, too: \verb\m.im_self\ is the |
| 2619 | object of which the method is an instance, and \verb\m.im_func\ is the |
| 2620 | function object corresponding to the method. |
| 2621 | |
| 2622 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2623 | \chapter{Recent Additions} |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 2624 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2625 | Python is an evolving language. Since this tutorial was last |
| 2626 | thoroughly revised, several new features have been added to the |
| 2627 | language. While ideally I should revise the tutorial to incorporate |
| 2628 | them in the mainline of the text, lack of time currently requires me |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 2629 | to take a more modest approach. In this chapter I will briefly list the |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2630 | most important improvements to the language and how you can use them |
| 2631 | to your benefit. |
| 2632 | |
| 2633 | \section{The Last Printed Expression} |
| 2634 | |
| 2635 | In interactive mode, the last printed expression is assigned to the |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 2636 | variable \code{_}. This means that when you are using Python as a |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2637 | desk calculator, it is somewhat easier to continue calculations, for |
| 2638 | example: |
| 2639 | |
| 2640 | \begin{verbatim} |
| 2641 | >>> tax = 17.5 / 100 |
| 2642 | >>> price = 3.50 |
| 2643 | >>> price * tax |
| 2644 | 0.6125 |
| 2645 | >>> price + _ |
| 2646 | 4.1125 |
| 2647 | >>> round(_, 2) |
| 2648 | 4.11 |
| 2649 | >>> |
| 2650 | \end{verbatim} |
| 2651 | |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 2652 | For reasons too embarrassing to explain, this variable is implemented |
| 2653 | as a built-in (living in the module \code{__builtin__}), so it should |
| 2654 | be treated as read-only by the user. I.e. don't explicitly assign a |
| 2655 | value to it --- you would create an independent local variable with |
| 2656 | the same name masking the built-in variable with its magic behavior. |
| 2657 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2658 | \section{String Literals} |
| 2659 | |
| 2660 | \subsection{Double Quotes} |
| 2661 | |
| 2662 | Python can now also use double quotes to surround string literals, |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 2663 | e.g. \verb\"this doesn't hurt a bit"\. There is no semantic |
| 2664 | difference between strings surrounded by single or double quotes. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2665 | |
| 2666 | \subsection{Continuation Of String Literals} |
| 2667 | |
| 2668 | String literals can span multiple lines by escaping newlines with |
| 2669 | backslashes, e.g. |
| 2670 | |
| 2671 | \begin{verbatim} |
| 2672 | hello = "This is a rather long string containing\n\ |
| 2673 | several lines of text just as you would do in C.\n\ |
| 2674 | Note that whitespace at the beginning of the line is\ |
| 2675 | significant.\n" |
| 2676 | print hello |
| 2677 | \end{verbatim} |
| 2678 | |
| 2679 | which would print the following: |
| 2680 | \begin{verbatim} |
| 2681 | This is a rather long string containing |
| 2682 | several lines of text just as you would do in C. |
| 2683 | Note that whitespace at the beginning of the line is significant. |
| 2684 | \end{verbatim} |
| 2685 | |
| 2686 | \subsection{Triple-quoted strings} |
| 2687 | |
| 2688 | In some cases, when you need to include really long strings (e.g. |
| 2689 | containing several paragraphs of informational text), it is annoying |
| 2690 | that you have to terminate each line with \verb@\n\@, especially if |
| 2691 | you would like to reformat the text occasionally with a powerful text |
| 2692 | editor like Emacs. For such situations, ``triple-quoted'' strings can |
| 2693 | be used, e.g. |
| 2694 | |
| 2695 | \begin{verbatim} |
| 2696 | hello = """ |
| 2697 | |
| 2698 | This string is bounded by triple double quotes (3 times "). |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 2699 | Unescaped newlines in the string are retained, though \ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2700 | it is still possible\nto use all normal escape sequences. |
| 2701 | |
| 2702 | Whitespace at the beginning of a line is |
| 2703 | significant. If you need to include three opening quotes |
| 2704 | you have to escape at least one of them, e.g. \""". |
| 2705 | |
| 2706 | This string ends in a newline. |
| 2707 | """ |
| 2708 | \end{verbatim} |
| 2709 | |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 2710 | Triple-quoted strings can be surrounded by three single quotes as |
| 2711 | well, again without semantic difference. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2712 | |
| 2713 | \subsection{String Literal Juxtaposition} |
| 2714 | |
| 2715 | One final twist: you can juxtapose multiple string literals. Two or |
| 2716 | more adjacent string literals (but not arbitrary expressions!) |
| 2717 | separated only by whitespace will be concatenated (without intervening |
| 2718 | whitespace) into a single string object at compile time. This makes |
| 2719 | it possible to continue a long string on the next line without |
| 2720 | sacrificing indentation or performance, unlike the use of the string |
| 2721 | concatenation operator \verb\+\ or the continuation of the literal |
| 2722 | itself on the next line (since leading whitespace is significant |
| 2723 | inside all types of string literals). Note that this feature, like |
| 2724 | all string features except triple-quoted strings, is borrowed from |
| 2725 | Standard C. |
| 2726 | |
| 2727 | \section{The Formatting Operator} |
| 2728 | |
| 2729 | \subsection{Basic Usage} |
| 2730 | |
| 2731 | The chapter on output formatting is really out of date: there is now |
| 2732 | an almost complete interface to C-style printf formats. This is done |
| 2733 | by overloading the modulo operator (\verb\%\) for a left operand |
| 2734 | which is a string, e.g. |
| 2735 | |
| 2736 | \begin{verbatim} |
| 2737 | >>> import math |
| 2738 | >>> print 'The value of PI is approximately %5.3f.' % math.pi |
| 2739 | The value of PI is approximately 3.142. |
| 2740 | >>> |
| 2741 | \end{verbatim} |
| 2742 | |
| 2743 | If there is more than one format in the string you pass a tuple as |
| 2744 | right operand, e.g. |
| 2745 | |
| 2746 | \begin{verbatim} |
| 2747 | >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} |
| 2748 | >>> for name, phone in table.items(): |
| 2749 | ... print '%-10s ==> %10d' % (name, phone) |
| 2750 | ... |
| 2751 | Jack ==> 4098 |
| 2752 | Dcab ==> 8637678 |
| 2753 | Sjoerd ==> 4127 |
| 2754 | >>> |
| 2755 | \end{verbatim} |
| 2756 | |
| 2757 | Most formats work exactly as in C and require that you pass the proper |
| 2758 | type (however, if you don't you get an exception, not a core dump). |
| 2759 | The \verb\%s\ format is more relaxed: if the corresponding argument is |
| 2760 | not a string object, it is converted to string using the \verb\str()\ |
| 2761 | built-in function. Using \verb\*\ to pass the width or precision in |
| 2762 | as a separate (integer) argument is supported. The C formats |
| 2763 | \verb\%n\ and \verb\%p\ are not supported. |
| 2764 | |
| 2765 | \subsection{Referencing Variables By Name} |
| 2766 | |
| 2767 | If you have a really long format string that you don't want to split |
| 2768 | up, it would be nice if you could reference the variables to be |
| 2769 | formatted by name instead of by position. This can be done by using |
| 2770 | an extension of C formats using the form \verb\%(name)format\, e.g. |
| 2771 | |
| 2772 | \begin{verbatim} |
| 2773 | >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} |
| 2774 | >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table |
| 2775 | Jack: 4098; Sjoerd: 4127; Dcab: 8637678 |
| 2776 | >>> |
| 2777 | \end{verbatim} |
| 2778 | |
| 2779 | This is particularly useful in combination with the new built-in |
| 2780 | \verb\vars()\ function, which returns a dictionary containing all |
| 2781 | local variables. |
| 2782 | |
| 2783 | \section{Optional Function Arguments} |
| 2784 | |
| 2785 | It is now possible to define functions with a variable number of |
| 2786 | arguments. There are two forms, which can be combined. |
| 2787 | |
| 2788 | \subsection{Default Argument Values} |
| 2789 | |
| 2790 | The most useful form is to specify a default value for one or more |
| 2791 | arguments. This creates a function that can be called with fewer |
| 2792 | arguments than it is defined, e.g. |
| 2793 | |
| 2794 | \begin{verbatim} |
| 2795 | def ask_ok(prompt, retries = 4, complaint = 'Yes or no, please!'): |
| 2796 | while 1: |
| 2797 | ok = raw_input(prompt) |
| 2798 | if ok in ('y', 'ye', 'yes'): return 1 |
| 2799 | if ok in ('n', 'no', 'nop', 'nope'): return 0 |
| 2800 | retries = retries - 1 |
| 2801 | if retries < 0: raise IOError, 'refusenik user' |
| 2802 | print complaint |
| 2803 | \end{verbatim} |
| 2804 | |
| 2805 | This function can be called either like this: |
| 2806 | \verb\ask_ok('Do you really want to quit?')\ or like this: |
| 2807 | \verb\ask_ok('OK to overwrite the file?', 2)\. |
| 2808 | |
| 2809 | The default values are evaluated at the point of function definition |
| 2810 | in the {\em defining} scope, so that e.g. |
| 2811 | |
| 2812 | \begin{verbatim} |
| 2813 | i = 5 |
| 2814 | def f(arg = i): print arg |
| 2815 | i = 6 |
| 2816 | f() |
| 2817 | \end{verbatim} |
| 2818 | |
| 2819 | will print \verb\5\. |
| 2820 | |
| 2821 | \subsection{Arbitrary Argument Lists} |
| 2822 | |
| 2823 | It is also possible to specify that a function can be called with an |
| 2824 | arbitrary number of arguments. These arguments will be wrapped up in |
| 2825 | a tuple. Before the variable number of arguments, zero or more normal |
| 2826 | arguments may occur, e.g. |
| 2827 | |
| 2828 | \begin{verbatim} |
| 2829 | def fprintf(file, format, *args): |
| 2830 | file.write(format % args) |
| 2831 | \end{verbatim} |
| 2832 | |
| 2833 | This feature may be combined with the previous, e.g. |
| 2834 | |
| 2835 | \begin{verbatim} |
| 2836 | def but_is_it_useful(required, optional = None, *remains): |
| 2837 | print "I don't know" |
| 2838 | \end{verbatim} |
| 2839 | |
| 2840 | \section{Lambda And Functional Programming Tools} |
| 2841 | |
| 2842 | \subsection{Lambda Forms} |
| 2843 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 2844 | By popular demand, a few features commonly found in functional |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2845 | programming languages and Lisp have been added to Python. With the |
| 2846 | \verb\lambda\ keyword, small anonymous functions can be created. |
| 2847 | Here's a function that returns the sum of its two arguments: |
| 2848 | \verb\lambda a, b: a+b\. Lambda forms can be used wherever function |
| 2849 | objects are required. They are syntactically restricted to a single |
| 2850 | expression. Semantically, they are just syntactic sugar for a normal |
| 2851 | function definition. Like nested function definitions, lambda forms |
| 2852 | cannot reference variables from the containing scope, but this can be |
| 2853 | overcome through the judicious use of default argument values, e.g. |
| 2854 | |
| 2855 | \begin{verbatim} |
| 2856 | def make_incrementor(n): |
Guido van Rossum | c1be9d5 | 1994-08-30 12:08:58 +0000 | [diff] [blame] | 2857 | return lambda x, incr=n: x+incr |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2858 | \end{verbatim} |
| 2859 | |
| 2860 | \subsection{Map, Reduce and Filter} |
| 2861 | |
| 2862 | Three new built-in functions on sequences are good candidate to pass |
| 2863 | lambda forms. |
| 2864 | |
| 2865 | \subsubsection{Map.} |
| 2866 | |
| 2867 | \verb\map(function, sequence)\ calls \verb\function(item)\ for each of |
| 2868 | the sequence's items and returns a list of the return values. For |
| 2869 | example, to compute some cubes: |
| 2870 | |
| 2871 | \begin{verbatim} |
| 2872 | >>> map(lambda x: x*x*x, range(1, 11)) |
| 2873 | [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] |
| 2874 | >>> |
| 2875 | \end{verbatim} |
| 2876 | |
| 2877 | More than one sequence may be passed; the function must then have as |
| 2878 | many arguments as there are sequences and is called with the |
| 2879 | corresponding item from each sequence (or \verb\None\ if some sequence |
| 2880 | is shorter than another). If \verb\None\ is passed for the function, |
| 2881 | a function returning its argument(s) is substituted. |
| 2882 | |
| 2883 | Combining these two special cases, we see that |
| 2884 | \verb\map(None, list1, list2)\ is a convenient way of turning a pair |
| 2885 | of lists into a list of pairs. For example: |
| 2886 | |
| 2887 | \begin{verbatim} |
| 2888 | >>> seq = range(8) |
| 2889 | >>> map(None, seq, map(lambda x: x*x, seq)) |
| 2890 | [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)] |
| 2891 | >>> |
| 2892 | \end{verbatim} |
| 2893 | |
| 2894 | \subsubsection{Filter.} |
| 2895 | |
| 2896 | \verb\filter(function, sequence)\ returns a sequence (of the same |
| 2897 | type, if possible) consisting of those items from the sequence for |
| 2898 | which \verb\function(item)\ is true. For example, to compute some |
| 2899 | primes: |
| 2900 | |
| 2901 | \begin{verbatim} |
| 2902 | >>> filter(lambda x: x%2 != 0 and x%3 != 0, range(2, 25)) |
| 2903 | [5, 7, 11, 13, 17, 19, 23] |
| 2904 | >>> |
| 2905 | \end{verbatim} |
| 2906 | |
| 2907 | \subsubsection{Reduce.} |
| 2908 | |
| 2909 | \verb\reduce(function, sequence)\ returns a single value constructed |
| 2910 | by calling the (binary) function on the first two items of the |
| 2911 | sequence, then on the result and the next item, and so on. For |
| 2912 | example, to compute the sum of the numbers 1 through 10: |
| 2913 | |
| 2914 | \begin{verbatim} |
| 2915 | >>> reduce(lambda x, y: x+y, range(1, 11)) |
| 2916 | 55 |
| 2917 | >>> |
| 2918 | \end{verbatim} |
| 2919 | |
| 2920 | If there's only one item in the sequence, its value is returned; if |
| 2921 | the sequence is empty, an exception is raised. |
| 2922 | |
| 2923 | A third argument can be passed to indicate the starting value. In this |
| 2924 | case the starting value is returned for an empty sequence, and the |
| 2925 | function is first applied to the starting value and the first sequence |
| 2926 | item, then to the result and the next item, and so on. For example, |
| 2927 | |
| 2928 | \begin{verbatim} |
| 2929 | >>> def sum(seq): |
| 2930 | ... return reduce(lambda x, y: x+y, seq, 0) |
| 2931 | ... |
| 2932 | >>> sum(range(1, 11)) |
| 2933 | 55 |
| 2934 | >>> sum([]) |
| 2935 | 0 |
| 2936 | >>> |
| 2937 | \end{verbatim} |
| 2938 | |
| 2939 | \section{Continuation Lines Without Backslashes} |
| 2940 | |
| 2941 | While the general mechanism for continuation of a source line on the |
| 2942 | next physical line remains to place a backslash on the end of the |
| 2943 | line, expressions inside matched parentheses (or square brackets, or |
| 2944 | curly braces) can now also be continued without using a backslash. |
| 2945 | This is particularly useful for calls to functions with many |
| 2946 | arguments, and for initializations of large tables. |
| 2947 | |
| 2948 | For example: |
| 2949 | |
| 2950 | \begin{verbatim} |
| 2951 | month_names = ['Januari', 'Februari', 'Maart', |
| 2952 | 'April', 'Mei', 'Juni', |
| 2953 | 'Juli', 'Augustus', 'September', |
| 2954 | 'Oktober', 'November', 'December'] |
| 2955 | \end{verbatim} |
| 2956 | |
| 2957 | and |
| 2958 | |
| 2959 | \begin{verbatim} |
| 2960 | CopyInternalHyperLinks(self.context.hyperlinks, |
| 2961 | copy.context.hyperlinks, |
| 2962 | uidremap) |
| 2963 | \end{verbatim} |
| 2964 | |
| 2965 | \section{Regular Expressions} |
| 2966 | |
| 2967 | While C's printf-style output formats, transformed into Python, are |
| 2968 | adequate for most output formatting jobs, C's scanf-style input |
| 2969 | formats are not very powerful. Instead of scanf-style input, Python |
| 2970 | offers Emacs-style regular expressions as a powerful input and |
| 2971 | scanning mechanism. Read the corresponding section in the Library |
| 2972 | Reference for a full description. |
| 2973 | |
| 2974 | \section{Generalized Dictionaries} |
| 2975 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 2976 | The keys of dictionaries are no longer restricted to strings --- they |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 2977 | can be any immutable basic type including strings, numbers, tuples, or |
| 2978 | (certain) class instances. (Lists and dictionaries are not acceptable |
| 2979 | as dictionary keys, in order to avoid problems when the object used as |
| 2980 | a key is modified.) |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 2981 | |
| 2982 | Dictionaries have two new methods: \verb\d.values()\ returns a list of |
| 2983 | the dictionary's values, and \verb\d.items()\ returns a list of the |
| 2984 | dictionary's (key, value) pairs. Like \verb\d.keys()\, these |
| 2985 | operations are slow for large dictionaries. Examples: |
| 2986 | |
| 2987 | \begin{verbatim} |
| 2988 | >>> d = {100: 'honderd', 1000: 'duizend', 10: 'tien'} |
| 2989 | >>> d.keys() |
| 2990 | [100, 10, 1000] |
| 2991 | >>> d.values() |
| 2992 | ['honderd', 'tien', 'duizend'] |
| 2993 | >>> d.items() |
| 2994 | [(100, 'honderd'), (10, 'tien'), (1000, 'duizend')] |
| 2995 | >>> |
| 2996 | \end{verbatim} |
| 2997 | |
| 2998 | \section{Miscellaneous New Built-in Functions} |
| 2999 | |
| 3000 | The function \verb\vars()\ returns a dictionary containing the current |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 3001 | local variables. With a module argument, it returns that module's |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 3002 | global variables. The old function \verb\dir(x)\ returns |
| 3003 | \verb\vars(x).keys()\. |
| 3004 | |
| 3005 | The function \verb\round(x)\ returns a floating point number rounded |
| 3006 | to the nearest integer (but still expressed as a floating point |
| 3007 | number). E.g. \verb\round(3.4) == 3.0\ and \verb\round(3.5) == 4.0\. |
| 3008 | With a second argument it rounds to the specified number of digits, |
| 3009 | e.g. \verb\round(math.pi, 4) == 3.1416\ or even |
| 3010 | \verb\round(123.4, -2) == 100.0\. |
| 3011 | |
| 3012 | The function \verb\hash(x)\ returns a hash value for an object. |
| 3013 | All object types acceptable as dictionary keys have a hash value (and |
| 3014 | it is this hash value that the dictionary implementation uses). |
| 3015 | |
| 3016 | The function \verb\id(x)\ return a unique identifier for an object. |
| 3017 | For two objects x and y, \verb\id(x) == id(y)\ if and only if |
| 3018 | \verb\x is y\. (In fact the object's address is used.) |
| 3019 | |
| 3020 | The function \verb\hasattr(x, name)\ returns whether an object has an |
| 3021 | attribute with the given name (a string value). The function |
| 3022 | \verb\getattr(x, name)\ returns the object's attribute with the given |
| 3023 | name. The function \verb\setattr(x, name, value)\ assigns a value to |
| 3024 | an object's attribute with the given name. These three functions are |
| 3025 | useful if the attribute names are not known beforehand. Note that |
Guido van Rossum | e5f8b60 | 1995-01-04 19:12:49 +0000 | [diff] [blame] | 3026 | \verb\getattr(x, 'spam')\ is equivalent to \verb\x.spam\, and |
| 3027 | \verb\setattr(x, 'spam', y)\ is equivalent to \verb\x.spam = y\. By |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 3028 | definition, \verb\hasattr(x, name)\ returns true if and only if |
| 3029 | \verb\getattr(x, name)\ returns without raising an exception. |
| 3030 | |
| 3031 | \section{Else Clause For Try Statement} |
| 3032 | |
| 3033 | The \verb\try...except\ statement now has an optional \verb\else\ |
| 3034 | clause, which must follow all \verb\except\ clauses. It is useful to |
| 3035 | place code that must be executed if the \verb\try\ clause does not |
| 3036 | raise an exception. For example: |
| 3037 | |
| 3038 | \begin{verbatim} |
| 3039 | for arg in sys.argv: |
| 3040 | try: |
| 3041 | f = open(arg, 'r') |
| 3042 | except IOError: |
| 3043 | print 'cannot open', arg |
| 3044 | else: |
| 3045 | print arg, 'has', len(f.readlines()), 'lines' |
| 3046 | f.close() |
| 3047 | \end{verbatim} |
Guido van Rossum | 5e0759d | 1992-08-07 16:06:24 +0000 | [diff] [blame] | 3048 | |
Guido van Rossum | ca3f6c8 | 1994-10-06 14:08:53 +0000 | [diff] [blame] | 3049 | |
| 3050 | \section{New Class Features in Release 1.1} |
| 3051 | |
Guido van Rossum | cfb45e4 | 1994-11-10 23:04:43 +0000 | [diff] [blame] | 3052 | Some changes have been made to classes: the operator overloading |
Guido van Rossum | ca3f6c8 | 1994-10-06 14:08:53 +0000 | [diff] [blame] | 3053 | mechanism is more flexible, providing more support for non-numeric use |
Guido van Rossum | 29766b2 | 1994-10-06 15:33:25 +0000 | [diff] [blame] | 3054 | of operators (including calling an object as if it were a function), |
| 3055 | and it is possible to trap attribute accesses. |
Guido van Rossum | ca3f6c8 | 1994-10-06 14:08:53 +0000 | [diff] [blame] | 3056 | |
| 3057 | \subsection{New Operator Overloading} |
| 3058 | |
| 3059 | It is no longer necessary to coerce both sides of an operator to the |
| 3060 | same class or type. A class may still provide a \code{__coerce__} |
| 3061 | method, but this method may return objects of different types or |
| 3062 | classes if it feels like it. If no \code{__coerce__} is defined, any |
| 3063 | argument type or class is acceptable. |
| 3064 | |
| 3065 | In order to make it possible to implement binary operators where the |
| 3066 | right-hand side is a class instance but the left-hand side is not, |
| 3067 | without using coercions, right-hand versions of all binary operators |
| 3068 | may be defined. These have an `r' prepended to their name, |
| 3069 | e.g. \code{__radd__}. |
| 3070 | |
| 3071 | For example, here's a very simple class for representing times. Times |
| 3072 | are initialized from a number of seconds (like time.time()). Times |
Guido van Rossum | 1133aec | 1995-03-15 11:34:18 +0000 | [diff] [blame] | 3073 | are printed like this: \code{Wed Mar 15 12:28:48 1995}. Subtracting |
Guido van Rossum | ca3f6c8 | 1994-10-06 14:08:53 +0000 | [diff] [blame] | 3074 | two Times gives their difference in seconds. Adding or subtracting a |
| 3075 | Time and a number gives a new Time. You can't add two times, nor can |
| 3076 | you subtract a Time from a number. |
| 3077 | |
| 3078 | \begin{verbatim} |
| 3079 | import time |
| 3080 | |
| 3081 | class Time: |
| 3082 | def __init__(self, seconds): |
| 3083 | self.seconds = seconds |
| 3084 | def __repr__(self): |
| 3085 | return time.ctime(self.seconds) |
| 3086 | def __add__(self, x): |
| 3087 | return Time(self.seconds + x) |
| 3088 | __radd__ = __add__ # support for x+t |
| 3089 | def __sub__(self, x): |
| 3090 | if hasattr(x, 'seconds'): # test if x could be a Time |
| 3091 | return self.seconds - x.seconds |
| 3092 | else: |
| 3093 | return self.seconds - x |
| 3094 | |
| 3095 | now = Time(time.time()) |
| 3096 | tomorrow = 24*3600 + now |
| 3097 | yesterday = now - today |
| 3098 | print tomorrow - yesterday # prints 172800 |
| 3099 | \end{verbatim} |
| 3100 | |
| 3101 | \subsection{Trapping Attribute Access} |
| 3102 | |
| 3103 | You can define three new ``magic'' methods in a class now: |
| 3104 | \code{__getattr__(self, name)}, \code{__setattr__(self, name, value)} |
| 3105 | and \code{__delattr__(self, name)}. |
| 3106 | |
| 3107 | The \code{__getattr__} method is called when an attribute access fails, |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 3108 | i.e. when an attribute access would otherwise raise AttributeError --- |
Guido van Rossum | ca3f6c8 | 1994-10-06 14:08:53 +0000 | [diff] [blame] | 3109 | this is {\em after} the instance's dictionary and its class hierarchy |
| 3110 | have been searched for the named attribute. Note that if this method |
| 3111 | attempts to access any undefined instance attribute it will be called |
| 3112 | recursively! |
| 3113 | |
| 3114 | The \code{__setattr__} and \code{__delattr__} methods are called when |
| 3115 | assignment to, respectively deletion of an attribute are attempted. |
| 3116 | They are called {\em instead} of the normal action (which is to insert |
| 3117 | or delete the attribute in the instance dictionary). If either of |
| 3118 | these methods most set or delete any attribute, they can only do so by |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 3119 | using the instance dictionary directly --- \code{self.__dict__} --- else |
Guido van Rossum | ca3f6c8 | 1994-10-06 14:08:53 +0000 | [diff] [blame] | 3120 | they would be called recursively. |
| 3121 | |
| 3122 | For example, here's a near-universal ``Wrapper'' class that passes all |
| 3123 | its attribute accesses to another object. Note how the |
| 3124 | \code{__init__} method inserts the wrapped object in |
| 3125 | \code{self.__dict__} in order to avoid endless recursion |
| 3126 | (\code{__setattr__} would call \code{__getattr__} which would call |
| 3127 | itself recursively). |
| 3128 | |
| 3129 | \begin{verbatim} |
| 3130 | class Wrapper: |
| 3131 | def __init__(self, wrapped): |
| 3132 | self.__dict__['wrapped'] = wrapped |
| 3133 | def __getattr__(self, name): |
| 3134 | return getattr(self.wrapped, name) |
| 3135 | def __setattr__(self, name, value): |
Guido van Rossum | cfb45e4 | 1994-11-10 23:04:43 +0000 | [diff] [blame] | 3136 | setattr(self.wrapped, name, value) |
Guido van Rossum | ca3f6c8 | 1994-10-06 14:08:53 +0000 | [diff] [blame] | 3137 | def __delattr__(self, name): |
| 3138 | delattr(self.wrapped, name) |
| 3139 | |
| 3140 | import sys |
| 3141 | f = Wrapper(sys.stdout) |
| 3142 | f.write('hello world\n') # prints 'hello world' |
| 3143 | \end{verbatim} |
| 3144 | |
Guido van Rossum | 29766b2 | 1994-10-06 15:33:25 +0000 | [diff] [blame] | 3145 | A simpler example of \code{__getattr__} is an attribute that is |
| 3146 | computed each time (or the first time) it it accessed. For instance: |
| 3147 | |
| 3148 | \begin{verbatim} |
| 3149 | from math import pi |
| 3150 | |
| 3151 | class Circle: |
| 3152 | def __init__(self, radius): |
| 3153 | self.radius = radius |
| 3154 | def __getattr__(self, name): |
| 3155 | if name == 'circumference': |
| 3156 | return 2 * pi * self.radius |
| 3157 | if name == 'diameter': |
| 3158 | return 2 * self.radius |
| 3159 | if name == 'area': |
| 3160 | return pi * pow(self.radius, 2) |
| 3161 | raise AttributeError, name |
| 3162 | \end{verbatim} |
| 3163 | |
| 3164 | \subsection{Calling a Class Instance} |
| 3165 | |
| 3166 | If a class defines a method \code{__call__} it is possible to call its |
| 3167 | instances as if they were functions. For example: |
| 3168 | |
| 3169 | \begin{verbatim} |
| 3170 | class PresetSomeArguments: |
| 3171 | def __init__(self, func, *args): |
| 3172 | self.func, self.args = func, args |
| 3173 | def __call__(self, *args): |
| 3174 | return apply(self.func, self.args + args) |
| 3175 | |
| 3176 | f = PresetSomeArguments(pow, 2) # f(i) computes powers of 2 |
| 3177 | for i in range(10): print f(i), # prints 1 2 4 8 16 32 64 128 256 512 |
| 3178 | print # append newline |
| 3179 | \end{verbatim} |
| 3180 | |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3181 | |
| 3182 | \chapter{New in Release 1.2} |
| 3183 | |
| 3184 | |
| 3185 | This chapter describes even more recent additions to the Python |
| 3186 | language and library. |
| 3187 | |
| 3188 | |
| 3189 | \section{New Class Features} |
| 3190 | |
| 3191 | The semantics of \code{__coerce__} have been changed to be more |
| 3192 | reasonable. As an example, the new standard module \code{Complex} |
| 3193 | implements fairly complete complex numbers using this. Additional |
| 3194 | examples of classes with and without \code{__coerce__} methods can be |
| 3195 | found in the \code{Demo/classes} subdirectory, modules \code{Rat} and |
| 3196 | \code{Dates}. |
| 3197 | |
| 3198 | If a class defines no \code{__coerce__} method, this is equivalent to |
| 3199 | the following definition: |
| 3200 | |
| 3201 | \begin{verbatim} |
| 3202 | def __coerce__(self, other): return self, other |
| 3203 | \end{verbatim} |
| 3204 | |
| 3205 | If \code{__coerce__} coerces itself to an object of a different type, |
| 3206 | the operation is carried out using that type --- in release 1.1, this |
| 3207 | would cause an error. |
| 3208 | |
| 3209 | Comparisons involving class instances now invoke \code{__coerce__} |
| 3210 | exactly as if \code{cmp(x, y)} were a binary operator like \code{+} |
| 3211 | (except if \code{x} and \code{y} are the same object). |
| 3212 | |
| 3213 | \section{Unix Signal Handling} |
| 3214 | |
Guido van Rossum | 34e1777 | 1996-06-10 19:44:49 +0000 | [diff] [blame] | 3215 | On {\UNIX}, Python now supports signal handling. The module |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3216 | \code{signal} exports functions \code{signal}, \code{pause} and |
Guido van Rossum | 34e1777 | 1996-06-10 19:44:49 +0000 | [diff] [blame] | 3217 | \code{alarm}, which act similar to their {\UNIX} counterparts. The |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3218 | module also exports the conventional names for the various signal |
| 3219 | classes (also usable with \code{os.kill()}) and \code{SIG_IGN} and |
| 3220 | \code{SIG_DFL}. See the section on \code{signal} in the Library |
| 3221 | Reference Manual for more information. |
| 3222 | |
| 3223 | \section{Exceptions Can Be Classes} |
| 3224 | |
| 3225 | User-defined exceptions are no longer limited to being string objects |
| 3226 | --- they can be identified by classes as well. Using this mechanism it |
| 3227 | is possible to create extensible hierarchies of exceptions. |
| 3228 | |
| 3229 | There are two new valid (semantic) forms for the raise statement: |
| 3230 | |
| 3231 | \begin{verbatim} |
| 3232 | raise Class, instance |
| 3233 | |
| 3234 | raise instance |
| 3235 | \end{verbatim} |
| 3236 | |
| 3237 | In the first form, \code{instance} must be an instance of \code{Class} |
| 3238 | or of a class derived from it. The second form is a shorthand for |
| 3239 | |
| 3240 | \begin{verbatim} |
| 3241 | raise instance.__class__, instance |
| 3242 | \end{verbatim} |
| 3243 | |
| 3244 | An except clause may list classes as well as string objects. A class |
| 3245 | in an except clause is compatible with an exception if it is the same |
| 3246 | class or a base class thereof (but not the other way around --- an |
| 3247 | except clause listing a derived class is not compatible with a base |
| 3248 | class). For example, the following code will print B, C, D in that |
| 3249 | order: |
| 3250 | |
| 3251 | \begin{verbatim} |
| 3252 | class B: |
| 3253 | pass |
| 3254 | class C(B): |
| 3255 | pass |
| 3256 | class D(C): |
| 3257 | pass |
| 3258 | |
| 3259 | for c in [B, C, D]: |
| 3260 | try: |
| 3261 | raise c() |
| 3262 | except D: |
| 3263 | print "D" |
| 3264 | except C: |
| 3265 | print "C" |
| 3266 | except B: |
| 3267 | print "B" |
| 3268 | \end{verbatim} |
| 3269 | |
| 3270 | Note that if the except clauses were reversed (with ``\code{except B}'' |
| 3271 | first), it would have printed B, B, B --- the first matching except |
| 3272 | clause is triggered. |
| 3273 | |
| 3274 | When an error message is printed for an unhandled exception which is a |
| 3275 | class, the class name is printed, then a colon and a space, and |
| 3276 | finally the instance converted to a string using the built-in function |
| 3277 | \code{str()}. |
| 3278 | |
| 3279 | In this release, the built-in exceptions are still strings. |
| 3280 | |
| 3281 | |
| 3282 | \section{Object Persistency and Object Copying} |
| 3283 | |
| 3284 | Two new modules, \code{pickle} and \code{shelve}, support storage and |
| 3285 | retrieval of (almost) arbitrary Python objects on disk, using the |
| 3286 | \code{dbm} package. A third module, \code{copy}, provides flexible |
Guido van Rossum | bcc9582 | 1995-02-16 16:28:48 +0000 | [diff] [blame] | 3287 | object copying operations. More information on these modules is |
| 3288 | provided in the Library Reference Manual. |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3289 | |
| 3290 | \subsection{Persistent Objects} |
| 3291 | |
| 3292 | The module \code{pickle} provides a general framework for objects to |
| 3293 | disassemble themselves into a stream of bytes and to reassemble such a |
| 3294 | stream back into an object. It copes with reference sharing, |
| 3295 | recursive objects and instances of user-defined classes, but not |
| 3296 | (directly) with objects that have ``magical'' links into the operating |
| 3297 | system such as open files, sockets or windows. |
| 3298 | |
| 3299 | The \code{pickle} module defines a simple protocol whereby |
| 3300 | user-defined classes can control how they are disassembled and |
| 3301 | assembled. The method \code{__getinitargs__()}, if defined, returns |
| 3302 | the argument list for the constructor to be used at assembly time (by |
| 3303 | default the constructor is called without arguments). The methods |
| 3304 | \code{__getstate__()} and \code{__setstate__()} are used to pass |
| 3305 | additional state from disassembly to assembly; by default the |
| 3306 | instance's \code{__dict__} is passed and restored. |
| 3307 | |
| 3308 | Note that \code{pickle} does not open or close any files --- it can be |
| 3309 | used equally well for moving objects around on a network or store them |
| 3310 | in a database. For ease of debugging, and the inevitable occasional |
| 3311 | manual patch-up, the constructed byte streams consist of printable |
Guido van Rossum | 47b4c0f | 1995-03-15 11:25:32 +0000 | [diff] [blame] | 3312 | \ASCII{} characters only (though it's not designed to be pretty). |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3313 | |
| 3314 | The module \code{shelve} provides a simple model for storing objects |
| 3315 | on files. The operation \code{shelve.open(filename)} returns a |
| 3316 | ``shelf'', which is a simple persistent database with a |
| 3317 | dictionary-like interface. Database keys are strings, objects stored |
| 3318 | in the database can be anything that \code{pickle} will handle. |
| 3319 | |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3320 | \subsection{Copying Objects} |
| 3321 | |
| 3322 | The module \code{copy} exports two functions: \code{copy()} and |
| 3323 | \code{deepcopy()}. The \code{copy()} function returns a ``shallow'' |
| 3324 | copy of an object; \code{deepcopy()} returns a ``deep'' copy. The |
| 3325 | difference between shallow and deep copying is only relevant for |
| 3326 | compound objects (objects that contain other objects, like lists or |
| 3327 | class instances): |
| 3328 | |
| 3329 | \begin{itemize} |
| 3330 | |
| 3331 | \item |
| 3332 | A shallow copy constructs a new compound object and then (to the |
| 3333 | extent possible) inserts {\em the same objects} into in that the |
| 3334 | original contains. |
| 3335 | |
| 3336 | \item |
| 3337 | A deep copy constructs a new compound object and then, recursively, |
| 3338 | inserts {\em copies} into it of the objects found in the original. |
| 3339 | |
| 3340 | \end{itemize} |
| 3341 | |
| 3342 | Both functions have the same restrictions and use the same protocols |
| 3343 | as \code{pickle} --- user-defined classes can control how they are |
| 3344 | copied by providing methods named \code{__getinitargs__()}, |
| 3345 | \code{__getstate__()} and \code{__setstate__()}. |
| 3346 | |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3347 | |
| 3348 | \section{Documentation Strings} |
| 3349 | |
| 3350 | A variety of objects now have a new attribute, \code{__doc__}, which |
| 3351 | is supposed to contain a documentation string (if no documentation is |
| 3352 | present, the attribute is \code{None}). New syntax, compatible with |
| 3353 | the old interpreter, allows for convenient initialization of the |
| 3354 | \code{__doc__} attribute of modules, classes and functions by placing |
| 3355 | a string literal by itself as the first statement in the suite. It |
| 3356 | must be a literal --- an expression yielding a string object is not |
| 3357 | accepted as a documentation string, since future tools may need to |
| 3358 | derive documentation from source by parsing. |
| 3359 | |
| 3360 | Here is a hypothetical, amply documented module called \code{Spam}: |
| 3361 | |
| 3362 | \begin{verbatim} |
| 3363 | """Spam operations. |
| 3364 | |
| 3365 | This module exports two classes, a function and an exception: |
| 3366 | |
| 3367 | class Spam: full Spam functionality --- three can sizes |
| 3368 | class SpamLight: limited Spam functionality --- only one can size |
| 3369 | |
| 3370 | def open(filename): open a file and return a corresponding Spam or |
| 3371 | SpamLight object |
| 3372 | |
| 3373 | GoneOff: exception raised for errors; should never happen |
| 3374 | |
| 3375 | Note that it is always possible to convert a SpamLight object to a |
| 3376 | Spam object by a simple method call, but that the reverse operation is |
| 3377 | generally costly and may fail for a number of reasons. |
| 3378 | """ |
| 3379 | |
| 3380 | class SpamLight: |
| 3381 | """Limited spam functionality. |
| 3382 | |
| 3383 | Supports a single can size, no flavor, and only hard disks. |
| 3384 | """ |
| 3385 | |
| 3386 | def __init__(self, size=12): |
| 3387 | """Construct a new SpamLight instance. |
| 3388 | |
| 3389 | Argument is the can size. |
| 3390 | """ |
| 3391 | # etc. |
| 3392 | |
| 3393 | # etc. |
| 3394 | |
| 3395 | class Spam(SpamLight): |
| 3396 | """Full spam functionality. |
| 3397 | |
| 3398 | Supports three can sizes, two flavor varieties, and all floppy |
| 3399 | disk formats still supported by current hardware. |
| 3400 | """ |
| 3401 | |
| 3402 | def __init__(self, size1=8, size2=12, size3=20): |
| 3403 | """Construct a new Spam instance. |
| 3404 | |
| 3405 | Arguments are up to three can sizes. |
| 3406 | """ |
| 3407 | # etc. |
| 3408 | |
| 3409 | # etc. |
| 3410 | |
| 3411 | def open(filename = "/dev/null"): |
| 3412 | """Open a can of Spam. |
| 3413 | |
| 3414 | Argument must be an existing file. |
| 3415 | """ |
| 3416 | # etc. |
| 3417 | |
| 3418 | class GoneOff: |
| 3419 | """Class used for Spam exceptions. |
| 3420 | |
| 3421 | There shouldn't be any. |
| 3422 | """ |
| 3423 | pass |
| 3424 | \end{verbatim} |
| 3425 | |
| 3426 | After executing ``\code{import Spam}'', the following expressions |
| 3427 | return the various documentation strings from the module: |
| 3428 | |
| 3429 | \begin{verbatim} |
| 3430 | Spam.__doc__ |
| 3431 | Spam.SpamLight.__doc__ |
| 3432 | Spam.SpamLight.__init__.__doc__ |
| 3433 | Spam.Spam.__doc__ |
| 3434 | Spam.Spam.__init__.__doc__ |
| 3435 | Spam.open.__doc__ |
| 3436 | Spam.GoneOff.__doc__ |
| 3437 | \end{verbatim} |
| 3438 | |
| 3439 | There are emerging conventions about the content and formatting of |
| 3440 | documentation strings. |
| 3441 | |
| 3442 | The first line should always be a short, concise summary of the |
| 3443 | object's purpose. For brevity, it should not explicitly state the |
| 3444 | object's name or type, since these are available by other means |
| 3445 | (except if the name happens to be a verb describing a function's |
| 3446 | operation). This line should begin with a capital letter and end with |
| 3447 | a period. |
| 3448 | |
| 3449 | If there are more lines in the documentation string, the second line |
| 3450 | should be blank, visually separating the summary from the rest of the |
| 3451 | description. The following lines should be one of more of paragraphs |
| 3452 | describing the objects calling conventions, its side effects, etc. |
| 3453 | |
| 3454 | Some people like to copy the Emacs convention of using UPPER CASE for |
| 3455 | function parameters --- this often saves a few words or lines. |
| 3456 | |
| 3457 | The Python parser does not strip indentation from multi-line string |
| 3458 | literals in Python, so tools that process documentation have to strip |
| 3459 | indentation. This is done using the following convention. The first |
| 3460 | non-blank line {\em after} the first line of the string determines the |
| 3461 | amount of indentation for the entire documentation string. (We can't |
| 3462 | use the first line since it is generally adjacent to the string's |
| 3463 | opening quotes so its indentation is not apparent in the string |
| 3464 | literal.) Whitespace ``equivalent'' to this indentation is then |
| 3465 | stripped from the start of all lines of the string. Lines that are |
| 3466 | indented less should not occur, but if they occur all their leading |
| 3467 | whitespace should be stripped. Equivalence of whitespace should be |
| 3468 | tested after expansion of tabs (to 8 spaces, normally). |
| 3469 | |
| 3470 | In this release, few of the built-in or standard functions and modules |
| 3471 | have documentation strings. |
| 3472 | |
| 3473 | |
| 3474 | \section{Customizing Import and Built-Ins} |
| 3475 | |
| 3476 | In preparation for a ``restricted execution mode'' which will be |
| 3477 | usable to run code received from an untrusted source (such as a WWW |
| 3478 | server or client), the mechanism by which modules are imported has |
| 3479 | been redesigned. It is now possible to provide your own function |
| 3480 | \code{__import__} which is called whenever an \code{import} statement |
| 3481 | is executed. There's a built-in function \code{__import__} which |
| 3482 | provides the default implementation, but more interesting, the various |
| 3483 | steps it takes are available separately from the new built-in module |
| 3484 | \code{imp}. (See the section on \code{imp} in the Library Reference |
Guido van Rossum | abfa2ca | 1995-07-07 22:57:02 +0000 | [diff] [blame] | 3485 | Manual for more information on this module -- it also contains a |
| 3486 | complete example of how to write your own \code{__import__} function.) |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3487 | |
| 3488 | When you do \code{dir()} in a fresh interactive interpreter you will |
| 3489 | see another ``secret'' object that's present in every module: |
| 3490 | \code{__builtins__}. This is either a dictionary or a module |
| 3491 | containing the set of built-in objects used by functions defined in |
| 3492 | current module. Although normally all modules are initialized with a |
| 3493 | reference to the same dictionary, it is now possible to use a |
| 3494 | different set of built-ins on a per-module basis. Together with the |
| 3495 | fact that the \code{import} statement uses the \code{__import__} |
| 3496 | function it finds in the importing modules' dictionary of built-ins, |
| 3497 | this forms the basis for a future restricted execution mode. |
| 3498 | |
| 3499 | |
| 3500 | \section{Python and the World-Wide Web} |
| 3501 | |
| 3502 | There is a growing number of modules available for writing WWW tools. |
| 3503 | The previous release already sported modules \code{gopherlib}, |
Guido van Rossum | bcc9582 | 1995-02-16 16:28:48 +0000 | [diff] [blame] | 3504 | \code{ftplib}, \code{httplib} and \code{urllib} (which unifies the |
| 3505 | other three) for accessing data through the commonest WWW protocols. |
| 3506 | This release also provides \code{cgi}, to ease the writing of |
| 3507 | server-side scripts that use the Common Gateway Interface protocol, |
| 3508 | supported by most WWW servers. The module \code{urlparse} provides |
| 3509 | precise parsing of a URL string into its components (address scheme, |
| 3510 | network location, path, parameters, query, and fragment identifier). |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3511 | |
Guido van Rossum | bcc9582 | 1995-02-16 16:28:48 +0000 | [diff] [blame] | 3512 | A rudimentary, parser for HTML files is available in the module |
| 3513 | \code{htmllib}. It currently supports a subset of HTML 1.0 (if you |
| 3514 | bring it up to date, I'd love to receive your fixes!). Unfortunately |
| 3515 | Python seems to be too slow for real-time parsing and formatting of |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 3516 | HTML such as required by interactive WWW browsers --- but it's good |
| 3517 | enough to write a ``robot'' (an automated WWW browser that searches |
| 3518 | the web for information). |
Guido van Rossum | 194e57c | 1995-02-15 15:51:38 +0000 | [diff] [blame] | 3519 | |
| 3520 | |
| 3521 | \section{Miscellaneous} |
| 3522 | |
| 3523 | \begin{itemize} |
| 3524 | |
| 3525 | \item |
| 3526 | The \code{socket} module now exports all the needed constants used for |
| 3527 | socket operations, such as \code{SO_BROADCAST}. |
| 3528 | |
| 3529 | \item |
| 3530 | The functions \code{popen()} and \code{fdopen()} in the \code{os} |
| 3531 | module now follow the pattern of the built-in function \code{open()}: |
| 3532 | the default mode argument is \code{'r'} and the optional third |
| 3533 | argument specifies the buffer size, where \code{0} means unbuffered, |
| 3534 | \code{1} means line-buffered, and any larger number means the size of |
| 3535 | the buffer in bytes. |
| 3536 | |
| 3537 | \end{itemize} |
| 3538 | |
| 3539 | |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3540 | \chapter{New in Release 1.3} |
| 3541 | |
| 3542 | |
| 3543 | This chapter describes yet more recent additions to the Python |
| 3544 | language and library. |
| 3545 | |
| 3546 | |
Guido van Rossum | 9beefa2 | 1995-10-08 00:38:51 +0000 | [diff] [blame] | 3547 | \section{Keyword Arguments} |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3548 | |
| 3549 | Functions and methods written in Python can now be called using |
| 3550 | keyword arguments of the form \code{\var{keyword} = \var{value}}. For |
| 3551 | instance, the following function: |
| 3552 | |
| 3553 | \begin{verbatim} |
| 3554 | def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'): |
| 3555 | print "-- This parrot wouldn't", action, |
| 3556 | print "if you put", voltage, "Volts through it." |
| 3557 | print "-- Lovely plumage, the", type |
| 3558 | print "-- It's", state, "!" |
| 3559 | \end{verbatim} |
| 3560 | |
| 3561 | could be called in any of the following ways: |
| 3562 | |
| 3563 | \begin{verbatim} |
| 3564 | parrot(1000) |
| 3565 | parrot(action = 'VOOOOOM', voltage = 1000000) |
| 3566 | parrot('a thousand', state = 'pushing up the daisies') |
| 3567 | parrot('a million', 'bereft of life', 'jump') |
| 3568 | \end{verbatim} |
| 3569 | |
| 3570 | but the following calls would all be invalid: |
| 3571 | |
| 3572 | \begin{verbatim} |
| 3573 | parrot() # required argument missing |
| 3574 | parrot(voltage=5.0, 'dead') # non-keyword argument following keyword |
| 3575 | parrot(110, voltage=220) # duplicate value for argument |
| 3576 | parrot(actor='John Cleese') # unknown keyword |
| 3577 | \end{verbatim} |
| 3578 | |
| 3579 | In general, an argument list must have the form: zero or more |
| 3580 | positional arguments followed by zero or more keyword arguments, where |
| 3581 | the keywords must be chosen from the formal parameter names. It's not |
| 3582 | important whether a formal parameter has a default value or not. No |
| 3583 | argument must receive a value more than once -- formal parameter names |
| 3584 | corresponding to positional arguments cannot be used as keywords in |
| 3585 | the same calls. |
| 3586 | |
| 3587 | Note that no special syntax is required to allow a function to be |
| 3588 | called with keyword arguments. The additional costs incurred by |
| 3589 | keyword arguments are only present when a call uses them. |
| 3590 | |
| 3591 | (As far as I know, these rules are exactly the same as used by |
| 3592 | Modula-3, even if they are enforced by totally different means. This |
| 3593 | is intentional.) |
| 3594 | |
| 3595 | When a final formal parameter of the form \code{**\var{name}} is |
| 3596 | present, it receives a dictionary containing all keyword arguments |
| 3597 | whose keyword doesn't correspond to a formal parameter. This may be |
| 3598 | combined with a formal parameter of the form \code{*\var{name}} which |
| 3599 | receives a tuple containing the positional arguments beyond the formal |
| 3600 | parameter list. (\code{*\var{name}} must occur before |
| 3601 | \code{**\var{name}}.) For example, if we define a function like this: |
| 3602 | |
| 3603 | \begin{verbatim} |
| 3604 | def cheeseshop(kind, *arguments, **keywords): |
| 3605 | print "-- Do you have any", kind, '?' |
| 3606 | print "-- I'm sorry, we're all out of", kind |
| 3607 | for arg in arguments: print arg |
| 3608 | print '-'*40 |
| 3609 | for kw in keywords.keys(): print kw, ':', keywords[kw] |
| 3610 | \end{verbatim} |
| 3611 | |
| 3612 | It could be called like this: |
| 3613 | |
| 3614 | \begin{verbatim} |
| 3615 | cheeseshop('Limburger', "It's very runny, sir.", |
| 3616 | "It's really very, VERY runny, sir.", |
| 3617 | client='John Cleese', |
| 3618 | shopkeeper='Michael Palin', |
| 3619 | sketch='Cheese Shop Sketch') |
| 3620 | \end{verbatim} |
| 3621 | |
| 3622 | and of course it would print: |
| 3623 | |
| 3624 | \begin{verbatim} |
| 3625 | -- Do you have any Limburger ? |
| 3626 | -- I'm sorry, we're all out of Limburger |
| 3627 | It's very runny, sir. |
| 3628 | It's really very, VERY runny, sir. |
| 3629 | ---------------------------------------- |
| 3630 | client : John Cleese |
| 3631 | shopkeeper : Michael Palin |
| 3632 | sketch : Cheese Shop Sketch |
| 3633 | \end{verbatim} |
| 3634 | |
Guido van Rossum | 691d4ec | 1995-10-08 01:14:57 +0000 | [diff] [blame] | 3635 | Consequences of this change include: |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3636 | |
| 3637 | \begin{itemize} |
| 3638 | |
| 3639 | \item |
Guido van Rossum | 691d4ec | 1995-10-08 01:14:57 +0000 | [diff] [blame] | 3640 | The built-in function \code{apply()} now has an optional third |
| 3641 | argument, which is a dictionary specifying any keyword arguments to be |
| 3642 | passed. For example, |
| 3643 | \begin{verbatim} |
| 3644 | apply(parrot, (), {'voltage': 20, 'action': 'voomm'}) |
| 3645 | \end{verbatim} |
| 3646 | is equivalent to |
| 3647 | \begin{verbatim} |
| 3648 | parrot(voltage=20, action='voomm') |
| 3649 | \end{verbatim} |
| 3650 | |
| 3651 | \item |
| 3652 | There is also a mechanism for functions and methods defined in an |
| 3653 | extension module (i.e., implemented in C or C++) to receive a |
| 3654 | dictionary of their keyword arguments. By default, such functions do |
| 3655 | not accept keyword arguments, since the argument names are not |
| 3656 | available to the interpreter. |
| 3657 | |
| 3658 | \item |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3659 | In the effort of implementing keyword arguments, function and |
| 3660 | especially method calls have been sped up significantly -- for a |
| 3661 | method with ten formal parameters, the call overhead has been cut in |
| 3662 | half; for a function with one formal parameters, the overhead has been |
| 3663 | reduced by a third. |
| 3664 | |
| 3665 | \item |
| 3666 | The format of \code{.pyc} files has changed (again). |
| 3667 | |
Guido van Rossum | 9beefa2 | 1995-10-08 00:38:51 +0000 | [diff] [blame] | 3668 | \item |
| 3669 | The \code{access} statement has been disabled. The syntax is still |
| 3670 | recognized but no code is generated for it. (There were some |
| 3671 | unpleasant interactions with changes for keyword arguments, and my |
| 3672 | plan is to get rid of \code{access} altogether in favor of a different |
| 3673 | approach.) |
| 3674 | |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3675 | \end{itemize} |
| 3676 | |
Guido van Rossum | 9beefa2 | 1995-10-08 00:38:51 +0000 | [diff] [blame] | 3677 | \section{Changes to the WWW and Internet tools} |
| 3678 | |
| 3679 | \begin{itemize} |
| 3680 | |
| 3681 | \item |
| 3682 | The \code{htmllib} module has been rewritten in an incompatible |
| 3683 | fashion. The new version is considerably more complete (HTML 2.0 |
| 3684 | except forms, but including all ISO-8859-1 entity definitions), and |
| 3685 | easy to use. Small changes to \code{sgmllib} have also been made, to |
| 3686 | better match the tokenization of HTML as recognized by other web |
| 3687 | tools. |
| 3688 | |
| 3689 | \item |
| 3690 | A new module \code{formatter} has been added, for use with the new |
| 3691 | \code{htmllib} module. |
| 3692 | |
| 3693 | \item |
| 3694 | The \code{urllib}and \code{httplib} modules have been changed somewhat |
| 3695 | to allow overriding unknown URL types and to support authentication. |
| 3696 | They now use \code{mimetools.Message} instead of \code{rfc822.Message} |
| 3697 | to parse headers. The \code{endrequest()} method has been removed |
| 3698 | from the HTTP class since it breaks the interaction with some servers. |
| 3699 | |
| 3700 | \item |
| 3701 | The \code{rfc822.Message} class has been changed to allow a flag to be |
| 3702 | passed in that says that the file is unseekable. |
| 3703 | |
| 3704 | \item |
| 3705 | The \code{ftplib} module has been fixed to be (hopefully) more robust |
| 3706 | on Linux. |
| 3707 | |
| 3708 | \item |
| 3709 | Several new operations that are optionally supported by servers have |
| 3710 | been added to \code{nntplib}: \code{xover}, \code{xgtitle}, |
| 3711 | \code{xpath} and \code{date}. % thanks to Kevan Heydon |
| 3712 | |
| 3713 | \end{itemize} |
| 3714 | |
| 3715 | \section{Other Language Changes} |
| 3716 | |
| 3717 | \begin{itemize} |
| 3718 | |
| 3719 | \item |
| 3720 | The \code{raise} statement now takes an optional argument which |
| 3721 | specifies the traceback to be used when printing the exception's stack |
| 3722 | trace. This must be a traceback object, such as found in |
| 3723 | \code{sys.exc_traceback}. When omitted or given as \code{None}, the |
| 3724 | old behavior (to generate a stack trace entry for the current stack |
| 3725 | frame) is used. |
| 3726 | |
| 3727 | \item |
| 3728 | The tokenizer is now more tolerant of alien whitespace. Control-L in |
| 3729 | the leading whitespace of a line resets the column number to zero, |
| 3730 | while Control-R just before the end of the line is ignored. |
| 3731 | |
| 3732 | \end{itemize} |
| 3733 | |
| 3734 | \section{Changes to Built-in Operations} |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3735 | |
| 3736 | \begin{itemize} |
| 3737 | |
| 3738 | \item |
| 3739 | For file objects, \code{\var{f}.read(0)} and |
| 3740 | \code{\var{f}.readline(0)} now return an empty string rather than |
| 3741 | reading an unlimited number of bytes. For the latter, omit the |
| 3742 | argument altogether or pass a negative value. |
| 3743 | |
| 3744 | \item |
| 3745 | A new system variable, \code{sys.platform}, has been added. It |
| 3746 | specifies the current platform, e.g. \code{sunos5} or \code{linux1}. |
| 3747 | |
| 3748 | \item |
| 3749 | The built-in functions \code{input()} and \code{raw_input()} now use |
| 3750 | the GNU readline library when it has been configured (formerly, only |
| 3751 | interactive input to the interpreter itself was read using GNU |
| 3752 | readline). The GNU readline library provides elaborate line editing |
| 3753 | and history. The Python debugger (\code{pdb}) is the first |
| 3754 | beneficiary of this change. |
| 3755 | |
| 3756 | \item |
| 3757 | Two new built-in functions, \code{globals()} and \code{locals()}, |
| 3758 | provide access to dictionaries containming current global and local |
| 3759 | variables, respectively. (These augment rather than replace |
| 3760 | \code{vars()}, which returns the current local variables when called |
| 3761 | without an argument, and a module's global variables when called with |
| 3762 | an argument of type module.) |
| 3763 | |
| 3764 | \item |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3765 | The built-in function \code{compile()} now takes a third possible |
| 3766 | value for the kind of code to be compiled: specifying \code{'single'} |
| 3767 | generates code for a single interactive statement, which prints the |
| 3768 | output of expression statements that evaluate to something else than |
| 3769 | \code{None}. |
| 3770 | |
Guido van Rossum | 9beefa2 | 1995-10-08 00:38:51 +0000 | [diff] [blame] | 3771 | \end{itemize} |
| 3772 | |
| 3773 | \section{Library Changes} |
| 3774 | |
| 3775 | \begin{itemize} |
| 3776 | |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3777 | \item |
Guido van Rossum | 691d4ec | 1995-10-08 01:14:57 +0000 | [diff] [blame] | 3778 | There are new module \code{ni} and \code{ihooks} that support |
| 3779 | importing modules with hierarchical names such as \code{A.B.C}. This |
| 3780 | is enabled by writing \code{import ni; ni.ni()} at the very top of the |
| 3781 | main program. These modules are amply documented in the Python |
| 3782 | source. |
| 3783 | |
| 3784 | \item |
| 3785 | The module \code{rexec} has been rewritten (incompatibly) to define a |
| 3786 | class and to use \code{ihooks}. |
| 3787 | |
| 3788 | \item |
Guido van Rossum | 9beefa2 | 1995-10-08 00:38:51 +0000 | [diff] [blame] | 3789 | The \code{string.split()} and \code{string.splitfields()} functions |
| 3790 | are now the same function (the presence or absence of the second |
| 3791 | argument determines which operation is invoked); similar for |
| 3792 | \code{string.join()} and \code{string.joinfields()}. |
| 3793 | |
| 3794 | \item |
| 3795 | The \code{Tkinter} module and its helper \code{Dialog} have been |
| 3796 | revamped to use keyword arguments. Tk 4.0 is now the standard. A new |
| 3797 | module \code{FileDialog} has been added which implements standard file |
| 3798 | selection dialogs. |
| 3799 | |
| 3800 | \item |
| 3801 | The optional built-in modules \code{dbm} and \code{gdbm} are more |
| 3802 | coordinated --- their \code{open()} functions now take the same values |
| 3803 | for their \var{flag} argument, and the \var{flag} and \var{mode} |
| 3804 | argument have default values (to open the database for reading only, |
| 3805 | and to create the database with mode \code{0666} minuse the umask, |
| 3806 | respectively). The memory leaks have finally been fixed. |
| 3807 | |
| 3808 | \item |
| 3809 | A new dbm-like module, \code{bsddb}, has been added, which uses the |
| 3810 | BSD DB package's hash method. % thanks to David Ely |
| 3811 | |
| 3812 | \item |
| 3813 | A portable (though slow) dbm-clone, implemented in Python, has been |
| 3814 | added for systems where none of the above is provided. It is aptly |
| 3815 | dubbed \code{dumbdbm}. |
| 3816 | |
| 3817 | \item |
| 3818 | The module \code{anydbm} provides a unified interface to \code{bsddb}, |
| 3819 | \code{gdbm}, \code{dbm}, and \code{dumbdbm}, choosing the first one |
| 3820 | available. |
| 3821 | |
| 3822 | \item |
| 3823 | A new extension module, \code{binascii}, provides a variety of |
| 3824 | operations for conversion of text-encoded binary data. |
| 3825 | |
| 3826 | \item |
| 3827 | There are three new or rewritten companion modules implemented in |
| 3828 | Python that can encode and decode the most common such formats: |
| 3829 | \code{uu} (uuencode), \code{base64} and \code{binhex}. |
| 3830 | |
| 3831 | \item |
| 3832 | A module to handle the MIME encoding quoted-printable has also been |
| 3833 | added: \code{quopri}. |
| 3834 | |
Guido van Rossum | aa93ca8 | 1995-10-11 17:47:45 +0000 | [diff] [blame] | 3835 | \item |
| 3836 | The parser module (which provides an interface to the Python parser's |
| 3837 | abstract syntax trees) has been rewritten (incompatibly) by Fred |
| 3838 | Drake. It now lets you change the parse tree and compile the result! |
| 3839 | |
Guido van Rossum | bf032a9 | 1995-10-11 19:28:39 +0000 | [diff] [blame] | 3840 | \item |
| 3841 | The \code{syslog} module has been upgraded and documented. |
| 3842 | % thanks to Steve Clift |
| 3843 | |
Guido van Rossum | 9beefa2 | 1995-10-08 00:38:51 +0000 | [diff] [blame] | 3844 | \end{itemize} |
| 3845 | |
| 3846 | \section{Other Changes} |
| 3847 | |
| 3848 | \begin{itemize} |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3849 | |
| 3850 | \item |
| 3851 | The dynamic module loader recognizes the fact that different filenames |
| 3852 | point to the same shared library and loads the library only once, so |
| 3853 | you can have a single shared library that defines multiple modules. |
| 3854 | (SunOS / SVR4 style shared libraries only.) |
| 3855 | |
| 3856 | \item |
| 3857 | Jim Fulton's ``abstract object interface'' has been incorporated into |
| 3858 | the run-time API. For more detailes, read the files |
| 3859 | \code{Include/abstract.h} and \code{Objects/abstract.c}. |
| 3860 | |
| 3861 | \item |
Guido van Rossum | 9beefa2 | 1995-10-08 00:38:51 +0000 | [diff] [blame] | 3862 | The Macintosh version is much more robust now. |
| 3863 | |
| 3864 | \item |
Guido van Rossum | eafe32a | 1995-08-10 14:18:10 +0000 | [diff] [blame] | 3865 | Numerous things I have forgotten or that are so obscure no-one will |
| 3866 | notice them anyway :-) |
| 3867 | |
| 3868 | \end{itemize} |
| 3869 | |
Guido van Rossum | 97662c8 | 1996-08-23 15:35:47 +0000 | [diff] [blame] | 3870 | |
| 3871 | \chapter{New in Release 1.4} |
| 3872 | |
| 3873 | |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 3874 | This chapter describes the major additions to the Python language and |
| 3875 | library in version 1.4. Many minor changes are not listed here; |
| 3876 | it is recommended to read the file \code{Misc/NEWS} in the Python |
| 3877 | source distribution for a complete listing of all changes, however |
| 3878 | small. |
Guido van Rossum | 97662c8 | 1996-08-23 15:35:47 +0000 | [diff] [blame] | 3879 | |
| 3880 | \begin{itemize} |
| 3881 | |
| 3882 | \item |
| 3883 | Power operator. \code{x**y} is equivalent to \code{pow(x, y)}. |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 3884 | This operator binds more tightly than \code{*}, \code{/} or \code{\%}, |
| 3885 | and binds from right to left when repeated or combined with unary |
| 3886 | operators. For example, \code{x**y**z} is equivalent to |
| 3887 | \code{x**(y**z)}, and \code{-x**y} is \code{-(x**y)}. |
Guido van Rossum | 97662c8 | 1996-08-23 15:35:47 +0000 | [diff] [blame] | 3888 | |
| 3889 | \item |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 3890 | Complex numbers. Imaginary literals are writen with a \code{'j'} |
| 3891 | suffix (\code{'J'} is allowed as well.) Complex numbers with a nonzero |
| 3892 | real component are written as \code{(\var{real}+\var{imag}j)}. You |
| 3893 | can also use the new built-in function \code{complex()} which takes |
| 3894 | one or two arguments: \code{complex(x)} is equivalent to \code{x + |
| 3895 | 0j}, and \code{complex(x, y)} is \code{x + y*0j}. |
| 3896 | |
| 3897 | The usual arithmetic operators on complex numbers are supported, so |
| 3898 | that e.g. \code{1j**2} equals \code{complex(-1.0)}. |
| 3899 | |
| 3900 | To extract the real and imaginary part from a complex number \code{z}, |
| 3901 | use \code{z.real} and \code{z.imag}. The conversion functions to |
| 3902 | floating point and integer (\code{float()}, \code{int()} and |
| 3903 | \code{long()}) don't work for complex numbers -- there is no one |
| 3904 | obvious way to convert a complex number to a real number. Use |
| 3905 | \code{abs(z)} to get its magnitude (as a float) or \code{z.real} to |
| 3906 | get its real part. |
| 3907 | |
| 3908 | Module \code{cmath} provides versions of all math functions that take |
| 3909 | complex arguments and return complex results. (Module \code{math} |
| 3910 | only supports real numbers, so that \code{math.sqrt(-1)} still raises |
| 3911 | a \code{ValueError} exception. Numerical experts agree that this is |
| 3912 | the way it shold be.) |
Guido van Rossum | 97662c8 | 1996-08-23 15:35:47 +0000 | [diff] [blame] | 3913 | |
| 3914 | \item |
| 3915 | New indexing syntax. It is now possible to use a tuple as an indexing |
| 3916 | expression for a mapping object without parenthesizing it, |
| 3917 | e.g. \code{x[1, 2, 3]}. |
| 3918 | |
| 3919 | \item |
| 3920 | New slicing syntax. In support of the Numerical Python extension |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 3921 | (distributed independently), slice indices of the form |
Guido van Rossum | 97662c8 | 1996-08-23 15:35:47 +0000 | [diff] [blame] | 3922 | \code{x[lo:hi:stride]} are possible, multiple slice indices separated by |
| 3923 | commas are allowed, and an index position may be replaced by ellipses, |
| 3924 | as follows: \code{x[a, ..., z]}. There's also a new built-in function |
| 3925 | \code{slice(lo, hi, stride)} and a new built-in object |
| 3926 | \code{Ellipses}, which yield the same effect without using special |
| 3927 | syntax. None of the standard sequence types support indexing with |
| 3928 | slice objects or ellipses yet. Note that when any of these extensions |
| 3929 | are used, the mapping interface for indexing will be used. |
| 3930 | |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 3931 | When a user-defined class instance is sliced using this extended slice |
| 3932 | notation, its \code{__getitem__} method is invoked -- the |
| 3933 | \code{__getslice__} method is only invoked when a single old-style |
| 3934 | slice is used, i.e. \code{x[lo:hi]}, with possible omission or |
| 3935 | \code{lo} and/or \code{hi}. Some examples: |
| 3936 | |
| 3937 | \begin{verbatim} |
| 3938 | x[1:2:-3] --> slice(1, 2, -3) |
| 3939 | x[-1:2:] --> slice(-1, 2, None) |
| 3940 | x[::] --> slice(None, None, None) |
| 3941 | x[1, 2:3] --> (1, slice(2, 3, None)) |
| 3942 | x[1:2, 3:4] --> (slice(1, 2, None), slice(3, 4, None)) |
| 3943 | x[1:2, ..., 3:4] --> (slice(1, 2, None), Ellipses, slice(3, 4, None)) |
| 3944 | \end{verbatim} |
| 3945 | |
| 3946 | For more help with this you are referred to the matrix-sig. |
| 3947 | |
Guido van Rossum | 97662c8 | 1996-08-23 15:35:47 +0000 | [diff] [blame] | 3948 | \item |
Guido van Rossum | 5812488 | 1996-10-08 17:29:56 +0000 | [diff] [blame] | 3949 | The \code{access} statement is now truly gone; \code{access} is no |
| 3950 | longer a reserved word. This saves a few cycles here and there. |
| 3951 | |
| 3952 | \item |
| 3953 | There is now limited support for class-private identifiers. Any |
| 3954 | identifier of the form \code{__spam} (two leading underscores, no two |
| 3955 | trailing underscores) is now textually replaced with |
| 3956 | \code{_classname__spam}, where \code{classname} is the current class |
| 3957 | name with leading underscore(s) stripped. This munging is done |
| 3958 | without regard of the syntactic position of the identifier, so it can |
| 3959 | be used to define class-private instance and class variables, methods, |
| 3960 | as well as globals, and even class-private instance variables on |
| 3961 | instances of {\em other} classes. Truncation may occur when the |
| 3962 | munged name would be longer than 255 characters. Outside classes, no |
| 3963 | munging occurs. |
| 3964 | |
| 3965 | Name munging is mostly intended to give classes an easy way to define |
| 3966 | ``private'' instance variables and methods, without having to worry |
| 3967 | about instance variables defined by derived classes, or mucking with |
| 3968 | instance variables by code outside the class. Note that the munging |
| 3969 | rules are designed mostly to avoid accidents; it still is possible for |
| 3970 | a ``determined soul'' to access or modify a variable that's considered |
| 3971 | private. This can even be useful, e.g. for the debugger, and that's |
| 3972 | one reason why this loophole is not closed. (Buglet: accidental |
| 3973 | derivation of a class with the same name as the base class makes |
| 3974 | accidental use of private variables of the base class possible.) |
| 3975 | |
| 3976 | Notice that code passed to \code{exec}, \code{eval()} or |
| 3977 | \code{evalfile()} does not consider the classname of the invoking |
| 3978 | class to be the current class; this is similar to the effect of the |
| 3979 | \code{global} statement, the effect of which is likewise restricted to |
| 3980 | code that is byte-compiled together. |
| 3981 | |
| 3982 | \item |
| 3983 | Syntax errors detected by the code generation phase of the Python |
| 3984 | bytecode compiler now include a line number. The line number is |
| 3985 | appended in parentheses. It is suppressed if the error occurs |
| 3986 | in line 1 (this usually happens in interactive use). |
| 3987 | |
| 3988 | \item |
| 3989 | Unrecognized keyword arguments now raise a \code{TypeError} exception |
| 3990 | rather than \code{KeyError}. |
| 3991 | |
| 3992 | \item |
| 3993 | A warning is written to sys.stderr when a \code{__del__} method raises |
| 3994 | an exception. Formerly, such exceptions were completely ignored. |
| 3995 | The new behavior, while needed in order to debug failing |
| 3996 | \code{__del__} methods, is occasionally annoying, because if affects |
| 3997 | the program's standard error stream. It honors assignments to |
| 3998 | \code{sys.stderr}, so it can be redirected from within a program if |
| 3999 | desired. |
| 4000 | |
| 4001 | \item |
| 4002 | New built-in function \code{list()} converts any sequence to a new list. |
| 4003 | Note that when the argument is a list, the return value is a fresh |
| 4004 | copy, similar to what would be returned by \code{a[:]}. |
| 4005 | |
| 4006 | \item |
| 4007 | New built-in module \code{operator}. XXX |
| 4008 | |
| 4009 | \item |
| 4010 | New built-in module \code{errno}. XXX |
| 4011 | |
| 4012 | \item |
| 4013 | Rewritten \code{cgi} module. XXX |
| 4014 | |
| 4015 | \item |
| 4016 | Improved restricted execution module (\code{rexec}). New module |
| 4017 | \code{Bastion}. XXX |
| 4018 | |
| 4019 | \item |
| 4020 | New string operations: lstrip(), rstrip(), capitalize(), capwords(), |
| 4021 | translate(), maketrans(); extended string operation: split(s, sep, |
| 4022 | maxsep). XXX |
| 4023 | |
| 4024 | \item |
| 4025 | New regsub operations: capwords(), splitx(), and split(s, sep, maxsep). |
| 4026 | XXX |
| 4027 | |
| 4028 | \item |
| 4029 | Module files pdb.py and profile.py can now be invoked as scripts to |
| 4030 | debug c.q. profile other scripts easily. |
| 4031 | |
| 4032 | \item |
| 4033 | The \code{os} module now supports the \code{putenv()} function on |
| 4034 | systems where it is provided in the C library (Windows NT and most |
| 4035 | Unix versions). The call \code{os.putenv('PATH', '/bin:/usr/bin')} |
| 4036 | sets the environment variable \code{PATH} to the string |
| 4037 | \code{'/bin:/usr/bin'}. Such changes to the environment affect |
| 4038 | subprocesses started with \code{os.system()}, \code{os.popen()} or |
| 4039 | \code{os.fork()} and \code{os.execv()}. When \code{putenv()} is |
| 4040 | supported, assignments to items in \code{os.environ} are automatically |
| 4041 | translated into corresponding calls to \code{os.putenv()}; however, |
| 4042 | calls to \code{os.putenv()} don't update \code{os.environ}, so it is |
| 4043 | actually preferable to assign to items of \code{os.environ}. For this |
| 4044 | purpose, the type of \code{os.environ} is changed to a subclass of |
| 4045 | \code{UserDict.UserDict} when \code{os.putenv()} is supported. |
| 4046 | (Buglet: \code{os.execve()} still requires a real dictionary.) |
| 4047 | |
| 4048 | \item |
| 4049 | New functions in the os module: mkfifo, plock, remove (== unlink), |
| 4050 | and ftruncate. More functions are also available under NT. XXX |
| 4051 | |
| 4052 | \item |
| 4053 | New function in the fcntl module: flock. XXX |
| 4054 | |
| 4055 | \item |
| 4056 | The first item of the module search path, \code{sys.path}, is the |
| 4057 | directory containing the script that was used to invoke the Python |
| 4058 | interpreter. If the script directory is not available (e.g. if the |
| 4059 | interpreter is invoked interactively or if the script is read from |
| 4060 | standard input), \code{sys.path[0]} is the empty string, which directs |
| 4061 | Python to search modules in the current directory first. Notice that |
| 4062 | the script directory is inserted {\em before} the entries inserted as |
| 4063 | a result of \code{\$PYTHONPATH}. There is no longer an entry for the |
| 4064 | current directory later in the path (unless explicitly set by |
| 4065 | \code{\$PYTHONPATH}). |
| 4066 | |
| 4067 | \item |
| 4068 | Some more configuration information is now available to Python |
| 4069 | programs. The variable \code{sys.prefix} gives the site-specific |
| 4070 | directory prefix where the platform independent Python files are |
| 4071 | installed; by default, this is the string \code{"/usr/local"}. The |
| 4072 | main collection of Python library modules is installed in the |
| 4073 | directory \code{sys.prefix+"/lib/python"+sys.version[:3]} while the |
| 4074 | platform independent header files (all except \code{config.h}) are |
| 4075 | stored in \code{sys.prefix+"/include/python"+sys.version[:3]}. |
| 4076 | |
| 4077 | Similarly, the variable \code{sys.exec_prefix} gives the site-specific |
| 4078 | directory prefix where the platform {\em de}pendent Python files are |
| 4079 | installed; by default, this is also \code{"/usr/local"}. |
| 4080 | Specifically, all configuration files (e.g. the \code{config.h} |
| 4081 | header file) are installed in the directory |
| 4082 | \code{sys.exec_prefix+"/lib/python"+sys.version[:3]+"/config"}, |
| 4083 | and shared library modules are installed in |
| 4084 | \code{sys.exec_prefix+"/lib/python"+sys.version[:3]+"/sharedmodules"}. |
| 4085 | |
| 4086 | On non-Unix systems, these variables are meaningless. |
| 4087 | |
| 4088 | \item |
| 4089 | You can now discover from which file (if any) a module was loaded by |
| 4090 | inspecting its \code{__file__} attribute. This attribute is not |
| 4091 | present for built-in or frozen modules. It points to the shared |
| 4092 | library file for dynamically loaded modules. (Buglet: this may be a |
| 4093 | relative path and is stored in the \code{.pyc} file on compilation. |
| 4094 | If you manipulate the current directory with \code{os.chdir()} or move |
| 4095 | \code{.pyc} files around, the value may be incorrect.) |
| 4096 | |
| 4097 | \item |
| 4098 | While sites are strongly discouraged from modifying the standard |
| 4099 | Python library (e.g. by adding site-specific modules or functions), |
| 4100 | there is now a standard way to invoke site-specific features. The |
| 4101 | standard module \code{site}, when imported, appends two site-specific |
| 4102 | directories to the end of \code{sys.path}: |
| 4103 | \code{\$prefix/lib/site-python} and |
| 4104 | \code{\$exec_prefix/lib/site-python}, where \code{\$prefix} and |
| 4105 | \code{\$exec_prefix} are the directories \code{sys.prefix} and |
| 4106 | \code{sys.exec_prefix} mentioned above. |
| 4107 | |
| 4108 | \item |
| 4109 | XXX |
Guido van Rossum | 97662c8 | 1996-08-23 15:35:47 +0000 | [diff] [blame] | 4110 | |
| 4111 | \end{itemize} |
| 4112 | |
| 4113 | |
| 4114 | |
| 4115 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 4116 | \end{document} |