annotate Doc/tut.tex @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 847445aeab2d
children 9ac9ea848d7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
808
0cff36b110fe Moved a4wide option to myformat.sty
Guido van Rossum <guido@python.org>
parents: 797
diff changeset
1 \documentstyle[twoside,11pt,myformat]{report}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3 \title{Python Tutorial}
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
4
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
5 \input{boilerplate}
1412
00f1e8864dcc * {lib,ref,ext,tut}.tex: added explicit \date{<date> \\ <release>}.
Guido van Rossum <guido@python.org>
parents: 1402
diff changeset
6
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 \begin{document}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 \pagenumbering{roman}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 \maketitle
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
13 \input{copyright}
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
14
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 \begin{abstract}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 \noindent
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
18 Python is a simple, yet powerful programming language that bridges the
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
19 gap between C and shell programming, and is thus ideally suited for
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
20 ``throw-away programming''
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
21 and rapid prototyping. Its syntax is put
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
22 together from constructs borrowed from a variety of other languages;
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
23 most prominent are influences from ABC, C, Modula-3 and Icon.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
25 The Python interpreter is easily extended with new functions and data
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
26 types implemented in C. Python is also suitable as an extension
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
27 language for highly customizable C applications such as editors or
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
28 window managers.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
30 Python is available for various operating systems, amongst which
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
31 several flavors of {\UNIX}, the Apple Macintosh, MS-DOS, Windows
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
32 (3.1(1), '95 and NT flavors), OS/2, and others.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
34 This tutorial introduces the reader informally to the basic concepts
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
35 and features of the Python language and system. It helps to have a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
36 Python interpreter handy for hands-on experience, but as the examples
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
37 are self-contained, the tutorial can be read off-line as well.
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
38
452
ae1a555e65fa The Reference Manual is now official.
Guido van Rossum <guido@python.org>
parents: 440
diff changeset
39 For a description of standard objects and modules, see the {\em Python
ae1a555e65fa The Reference Manual is now official.
Guido van Rossum <guido@python.org>
parents: 440
diff changeset
40 Library Reference} document. The {\em Python Reference Manual} gives
ae1a555e65fa The Reference Manual is now official.
Guido van Rossum <guido@python.org>
parents: 440
diff changeset
41 a more formal definition of the language.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 \end{abstract}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 \pagebreak
670
2cddbbea9bfb Set parskip to 0 in the table of content.
Guido van Rossum <guido@python.org>
parents: 563
diff changeset
46 {
2cddbbea9bfb Set parskip to 0 in the table of content.
Guido van Rossum <guido@python.org>
parents: 563
diff changeset
47 \parskip = 0mm
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 \tableofcontents
670
2cddbbea9bfb Set parskip to 0 in the table of content.
Guido van Rossum <guido@python.org>
parents: 563
diff changeset
49 }
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 \pagebreak
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 \pagenumbering{arabic}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
55
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
56 \chapter{Whetting Your Appetite}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
58 \section{Disclaimer}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
59
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
60 Now that there are several books out on Python, this tutorial has lost
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
61 its role as the only introduction to Python for most new users. It
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
62 takes time to keep a document like this up to date in the face of
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
63 additions to the language, and I simply don't have enough time to do a
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
64 good job. Therefore, this version of the tutorial is almost unchanged
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
65 since the previous release. This doesn't mean that the tutorial is
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
66 out of date --- all the examples still work exactly as before. There
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
67 are simply some new areas of the language that aren't covered.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
68
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
69 To make up for this, there are some chapters at the end cover
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
70 important changes in recent Python releases, and these are up to date
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
71 with the current release.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
72
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
73 \section{Introduction}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
74
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
75 If you ever wrote a large shell script, you probably know this
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
76 feeling: you'd love to add yet another feature, but it's already so
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
77 slow, and so big, and so complicated; or the feature involves a system
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
78 call or other function that is only accessible from C \ldots Usually
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
79 the problem at hand isn't serious enough to warrant rewriting the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
80 script in C; perhaps because the problem requires variable-length
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
81 strings or other data types (like sorted lists of file names) that are
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
82 easy in the shell but lots of work to implement in C; or perhaps just
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
83 because you're not sufficiently familiar with C.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
85 In such cases, Python may be just the language for you. Python is
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
86 simple to use, but it is a real programming language, offering much
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
87 more structure and support for large programs than the shell has. On
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
88 the other hand, it also offers much more error checking than C, and,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
89 being a {\em very-high-level language}, it has high-level data types
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
90 built in, such as flexible arrays and dictionaries that would cost you
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
91 days to implement efficiently in C. Because of its more general data
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
92 types Python is applicable to a much larger problem domain than {\em
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
93 Awk} or even {\em Perl}, yet many things are at least as easy in
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
94 Python as in those languages.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
96 Python allows you to split up your program in modules that can be
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
97 reused in other Python programs. It comes with a large collection of
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
98 standard modules that you can use as the basis of your programs --- or
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
99 as examples to start learning to program in Python. There are also
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
100 built-in modules that provide things like file I/O, system calls,
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
101 sockets, and even a generic interface to window systems (STDWIN).
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
103 Python is an interpreted language, which can save you considerable time
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 during program development because no compilation and linking is
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
105 necessary. The interpreter can be used interactively, which makes it
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
106 easy to experiment with features of the language, to write throw-away
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
107 programs, or to test functions during bottom-up program development.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 It is also a handy desk calculator.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
110 Python allows writing very compact and readable programs. Programs
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
111 written in Python are typically much shorter than equivalent C
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
112 programs, for several reasons:
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
113 \begin{itemize}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
114 \item
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
115 the high-level data types allow you to express complex operations in a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
116 single statement;
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
117 \item
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
118 statement grouping is done by indentation instead of begin/end
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
119 brackets;
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
120 \item
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
121 no variable or argument declarations are necessary.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
122 \end{itemize}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
124 Python is {\em extensible}: if you know how to program in C it is easy
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
125 to add a new built-in
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
126 function or
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
127 module to the interpreter, either to
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
128 perform critical operations at maximum speed, or to link Python
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
129 programs to libraries that may only be available in binary form (such
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
130 as a vendor-specific graphics library). Once you are really hooked,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
131 you can link the Python interpreter into an application written in C
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
132 and use it as an extension or command language for that application.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
133
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
134 By the way, the language is named after the BBC show ``Monty
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
135 Python's Flying Circus'' and has nothing to do with nasty reptiles...
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
137 \section{Where From Here}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
139 Now that you are all excited about Python, you'll want to examine it
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
140 in some more detail. Since the best way to learn a language is
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
141 using it, you are invited here to do so.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
142
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
143 In the next chapter, the mechanics of using the interpreter are
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
144 explained. This is rather mundane information, but essential for
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
145 trying out the examples shown later.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
146
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
147 The rest of the tutorial introduces various features of the Python
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
148 language and system though examples, beginning with simple
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
149 expressions, statements and data types, through functions and modules,
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
150 and finally touching upon advanced concepts like exceptions
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
151 and user-defined classes.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
152
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
153 When you're through with the tutorial (or just getting bored), you
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
154 should read the Library Reference, which gives complete (though terse)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
155 reference material about built-in and standard types, functions and
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
156 modules that can save you a lot of time when writing Python programs.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
157
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
158
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
159 \chapter{Using the Python Interpreter}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
160
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
161 \section{Invoking the Interpreter}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
162
1030
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
163 The Python interpreter is usually installed as {\tt /usr/local/bin/python}
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
164 on those machines where it is available; putting {\tt /usr/local/bin} in
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
165 your {\UNIX} shell's search path makes it possible to start it by
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 typing the command
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
167
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
168 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 python
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
170 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
171 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
172 to the shell. Since the choice of the directory where the interpreter
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
173 lives is an installation option, other places are possible; check with
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
174 your local Python guru or system administrator. (E.g., {\tt
1030
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
175 /usr/local/python} is a popular alternative location.)
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
176
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
177 The interpreter operates somewhat like the {\UNIX} shell: when called
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
178 with standard input connected to a tty device, it reads and executes
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
179 commands interactively; when called with a file name argument or with
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
180 a file as standard input, it reads and executes a {\em script} from
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
181 that file.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
183 A third way of starting the interpreter is
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
184 ``{\tt python -c command [arg] ...}'', which
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
185 executes the statement(s) in {\tt command}, analogous to the shell's
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
186 {\tt -c} option. Since Python statements often contain spaces or other
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
187 characters that are special to the shell, it is best to quote {\tt
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
188 command} in its entirety with double quotes.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
189
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
190 Note that there is a difference between ``{\tt python file}'' and
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
191 ``{\tt python $<$file}''. In the latter case, input requests from the
697
7c06bf15fd2a _ can now be a normal character.
Guido van Rossum <guido@python.org>
parents: 670
diff changeset
192 program, such as calls to {\tt input()} and {\tt raw_input()}, are
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
193 satisfied from {\em file}. Since this file has already been read
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
194 until the end by the parser before the program starts executing, the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
195 program will encounter EOF immediately. In the former case (which is
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
196 usually what you want) they are satisfied from whatever file or device
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
197 is connected to standard input of the Python interpreter.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
198
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
199 When a script file is used, it is sometimes useful to be able to run
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
200 the script and enter interactive mode afterwards. This can be done by
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
201 passing {\tt -i} before the script. (This does not work if the script
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
202 is read from standard input, for the same reason as explained in the
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
203 previous paragraph.)
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
204
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
205 \subsection{Argument Passing}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
206
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
207 When known to the interpreter, the script name and additional
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
208 arguments thereafter are passed to the script in the variable {\tt
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
209 sys.argv}, which is a list of strings. Its length is at least one;
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
210 when no script and no arguments are given, {\tt sys.argv[0]} is an
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
211 empty string. When the script name is given as {\tt '-'} (meaning
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
212 standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
213 command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
214 found after {\tt -c command} are not consumed by the Python
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
215 interpreter's option processing but left in {\tt sys.argv} for the
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
216 command to handle.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
217
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
218 \subsection{Interactive Mode}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
283
ed0162558929 Mention "-c command" option.
Guido van Rossum <guido@python.org>
parents: 271
diff changeset
220 When commands are read from a tty, the interpreter is said to be in
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
221 {\em interactive\ mode}. In this mode it prompts for the next command
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
222 with the {\em primary\ prompt}, usually three greater-than signs ({\tt
3081
eada248676de move uncomfortable backslash
Guido van Rossum <guido@python.org>
parents: 2942
diff changeset
223 >>>}); for continuation lines it prompts with the
eada248676de move uncomfortable backslash
Guido van Rossum <guido@python.org>
parents: 2942
diff changeset
224 {\em secondary\ prompt},
3659
825bd8c11b1c Note that EOF is ^Z on DOS. Spell Unix as \UNIX in some more places.
Guido van Rossum <guido@python.org>
parents: 3210
diff changeset
225 by default three dots ({\tt ...}). Typing an EOF character
825bd8c11b1c Note that EOF is ^Z on DOS. Spell Unix as \UNIX in some more places.
Guido van Rossum <guido@python.org>
parents: 3210
diff changeset
226 (Control-D on {\UNIX}, Control-Z on DOS or Windows)
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
227 at the primary prompt causes the interpreter to exit with a zero exit
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
228 status.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
230 The interpreter prints a welcome message stating its version number
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
231 and a copyright notice before printing the first prompt, e.g.:
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
232
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
233 \bcode\begin{verbatim}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
234 python
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
235 Python 1.4 (Oct 25 1996) [GCC 2.7.2]
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
236 Copyright 1991-1996 Stichting Mathematisch Centrum, Amsterdam
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
237 >>>
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
238 \end{verbatim}\ecode
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
239
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
240 \section{The Interpreter and its Environment}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
241
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
242 \subsection{Error Handling}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
243
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
244 When an error occurs, the interpreter prints an error
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
245 message and a stack trace. In interactive mode, it then returns to
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
246 the primary prompt; when input came from a file, it exits with a
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
247 nonzero exit status after printing
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
248 the stack trace. (Exceptions handled by an {\tt except} clause in a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
249 {\tt try} statement are not errors in this context.) Some errors are
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
250 unconditionally fatal and cause an exit with a nonzero exit; this
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
251 applies to internal inconsistencies and some cases of running out of
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
252 memory. All error messages are written to the standard error stream;
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
253 normal output from the executed commands is written to standard
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
254 output.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
256 Typing the interrupt character (usually Control-C or DEL) to the
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
257 primary or secondary prompt cancels the input and returns to the
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
258 primary prompt.%
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
259 \footnote{
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
260 A problem with the GNU Readline package may prevent this.
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
261 }
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
262 Typing an interrupt while a command is executing raises the {\tt
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
263 KeyboardInterrupt} exception, which may be handled by a {\tt try}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 statement.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
266 \subsection{The Module Search Path}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
267
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
268 When a module named {\tt spam} is imported, the interpreter searches
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
269 for a file named {\tt spam.py} in the current directory,
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
270 and then in the list of directories specified by
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
271 the environment variable {\tt PYTHONPATH}. This has the same syntax as
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
272 the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
1030
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
273 directory names. When {\tt PYTHONPATH} is not set, or when the file
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
274 is not found there, the search continues in an installation-dependent
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
275 default path, usually {\tt .:/usr/local/lib/python}.
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
276
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
277 Actually, modules are searched in the list of directories given by the
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
278 variable {\tt sys.path} which is initialized from the directory
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
279 containing the input script (or the current directory), {\tt
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
280 PYTHONPATH} and the installation-dependent default. This allows
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
281 Python programs that know what they're doing to modify or replace the
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
282 module search path. See the section on Standard Modules later.
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
283
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
284 \subsection{``Compiled'' Python files}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
286 As an important speed-up of the start-up time for short programs that
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
287 use a lot of standard modules, if a file called {\tt spam.pyc} exists
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
288 in the directory where {\tt spam.py} is found, this is assumed to
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
289 contain an already-``compiled'' version of the module {\tt spam}. The
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
290 modification time of the version of {\tt spam.py} used to create {\tt
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
291 spam.pyc} is recorded in {\tt spam.pyc}, and the file is ignored if
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
292 these don't match.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
293
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
294 Normally, you don't need to do anything to create the {\tt spam.pyc} file.
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
295 Whenever {\tt spam.py} is successfully compiled, an attempt is made to
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
296 write the compiled version to {\tt spam.pyc}. It is not an error if
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
297 this attempt fails; if for any reason the file is not written
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
298 completely, the resulting {\tt spam.pyc} file will be recognized as
3659
825bd8c11b1c Note that EOF is ^Z on DOS. Spell Unix as \UNIX in some more places.
Guido van Rossum <guido@python.org>
parents: 3210
diff changeset
299 invalid and thus ignored later. The contents of the {\tt spam.pyc}
825bd8c11b1c Note that EOF is ^Z on DOS. Spell Unix as \UNIX in some more places.
Guido van Rossum <guido@python.org>
parents: 3210
diff changeset
300 file is platform independent, so a Python module directory can be
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
301 shared by machines of different architectures. (Tip for experts:
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
302 the module {\tt compileall} creates {\tt .pyc} files for all modules.)
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
303
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
304 \subsection{Executable Python scripts}
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
305
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
306 On BSD'ish {\UNIX} systems, Python scripts can be made directly
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
307 executable, like shell scripts, by putting the line
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
308
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
309 \bcode\begin{verbatim}
1030
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
310 #! /usr/local/bin/python
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
311 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
312 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 (assuming that's the name of the interpreter) at the beginning of the
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
314 script and giving the file an executable mode. The {\tt \#!} must be
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
315 the first two characters of the file.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
1030
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
317 \subsection{The Interactive Startup File}
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
318
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
319 When you use Python interactively, it is frequently handy to have some
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
320 standard commands executed every time the interpreter is started. You
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
321 can do this by setting an environment variable named {\tt
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
322 PYTHONSTARTUP} to the name of a file containing your start-up
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
323 commands. This is similar to the {\tt .profile} feature of the UNIX
1030
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
324 shells.
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
325
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
326 This file is only read in interactive sessions, not when Python reads
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
327 commands from a script, and not when {\tt /dev/tty} is given as the
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
328 explicit source of commands (which otherwise behaves like an
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
329 interactive session). It is executed in the same name space where
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
330 interactive commands are executed, so that objects that it defines or
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
331 imports can be used without qualification in the interactive session.
1040
0ee59a14e542 Added hint about changing the prompt in $PYTHONSTARTUP
Guido van Rossum <guido@python.org>
parents: 1030
diff changeset
332 You can also change the prompts {\tt sys.ps1} and {\tt sys.ps2} in
0ee59a14e542 Added hint about changing the prompt in $PYTHONSTARTUP
Guido van Rossum <guido@python.org>
parents: 1030
diff changeset
333 this file.
1030
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
334
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
335 If you want to read an additional start-up file from the current
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
336 directory, you can program this in the global start-up file, e.g.
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
337 \verb\execfile('.pythonrc')\. If you want to use the startup file
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
338 in a script, you must write this explicitly in the script, e.g.
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
339 \verb\import os;\ \verb\execfile(os.environ['PYTHONSTARTUP'])\.
9f0fb114c1e5 Changed references to /usr/local into /usr/local/bin.
Guido van Rossum <guido@python.org>
parents: 952
diff changeset
340
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
341 \section{Interactive Input Editing and History Substitution}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
343 Some versions of the Python interpreter support editing of the current
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
344 input line and history substitution, similar to facilities found in
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
345 the Korn shell and the GNU Bash shell. This is implemented using the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
346 {\em GNU\ Readline} library, which supports Emacs-style and vi-style
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
347 editing. This library has its own documentation which I won't
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
348 duplicate here; however, the basics are easily explained.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
349
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
350 Perhaps the quickest check to see whether command line editing is
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
351 supported is typing Control-P to the first Python prompt you get. If
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
352 it beeps, you have command line editing. If nothing appears to
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
353 happen, or if \verb/^P/ is echoed, you can skip the rest of this
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
354 section.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
355
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
356 \subsection{Line Editing}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
357
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
358 If supported, input line editing is active whenever the interpreter
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
359 prints a primary or secondary prompt. The current line can be edited
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
360 using the conventional Emacs control characters. The most important
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
361 of these are: C-A (Control-A) moves the cursor to the beginning of the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
362 line, C-E to the end, C-B moves it one position to the left, C-F to
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
363 the right. Backspace erases the character to the left of the cursor,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
364 C-D the character to its right. C-K kills (erases) the rest of the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
365 line to the right of the cursor, C-Y yanks back the last killed
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
366 string. C-underscore undoes the last change you made; it can be
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
367 repeated for cumulative effect.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
369 \subsection{History Substitution}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
370
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
371 History substitution works as follows. All non-empty input lines
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
372 issued are saved in a history buffer, and when a new prompt is given
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
373 you are positioned on a new line at the bottom of this buffer. C-P
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
374 moves one line up (back) in the history buffer, C-N moves one down.
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
375 Any line in the history buffer can be edited; an asterisk appears in
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
376 front of the prompt to mark a line as modified. Pressing the Return
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
377 key passes the current line to the interpreter. C-R starts an
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
378 incremental reverse search; C-S starts a forward search.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
379
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
380 \subsection{Key Bindings}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
381
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 The key bindings and some other parameters of the Readline library can
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 be customized by placing commands in an initialization file called
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
384 {\tt \$HOME/.inputrc}. Key bindings have the form
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
385
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
386 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 key-name: function-name
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
388 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
389 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
390 or
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
391
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
392 \bcode\begin{verbatim}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
393 "string": function-name
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
394 \end{verbatim}\ecode
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
395 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 and options can be set with
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
397
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
398 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 set option-name value
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
400 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
401 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
402 For example:
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
403
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
404 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 # I prefer vi-style editing:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 set editing-mode vi
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 # Edit using a single line:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 set horizontal-scroll-mode On
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 # Rebind some keys:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 Meta-h: backward-kill-word
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
411 "\C-u": universal-argument
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
412 "\C-x\C-r": re-read-init-file
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
413 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
414 %
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
415 Note that the default binding for TAB in Python is to insert a TAB
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
416 instead of Readline's default filename completion function. If you
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
417 insist, you can override this by putting
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
418
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
419 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 TAB: complete
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
421 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
422 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
423 in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
424 indented continuation lines...)
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
425
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
426 \subsection{Commentary}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
427
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
428 This facility is an enormous step forward compared to previous
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
429 versions of the interpreter; however, some wishes are left: It would
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
430 be nice if the proper indentation were suggested on continuation lines
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
431 (the parser knows if an indent token is required next). The
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
432 completion mechanism might use the interpreter's symbol table. A
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
433 command to check (or even suggest) matching parentheses, quotes etc.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
434 would also be useful.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
435
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
436
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
437 \chapter{An Informal Introduction to Python}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
438
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 In the following examples, input and output are distinguished by the
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
440 presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
441 the example, you must type everything after the prompt, when the
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
442 prompt appears; lines that do not begin with a prompt are output from
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
443 the interpreter.%
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
444 \footnote{
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
445 I'd prefer to use different fonts to distinguish input
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
446 from output, but the amount of LaTeX hacking that would require
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
447 is currently beyond my ability.
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
448 }
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
449 Note that a secondary prompt on a line by itself in an example means
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
450 you must type a blank line; this is used to end a multi-line command.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
451
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
452 \section{Using Python as a Calculator}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
453
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
454 Let's try some simple Python commands. Start the interpreter and wait
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
455 for the primary prompt, {\tt >>>}. (It shouldn't take long.)
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
456
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
457 \subsection{Numbers}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
458
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
459 The interpreter acts as a simple calculator: you can type an
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
460 expression at it and it will write the value. Expression syntax is
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
461 straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
462 work just like in most other languages (e.g., Pascal or C); parentheses
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
463 can be used for grouping. For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
464
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
465 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 >>> 2+2
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 4
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
468 >>> # This is a comment
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
469 ... 2+2
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
470 4
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
471 >>> 2+2 # and a comment on the same line as code
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
472 4
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
473 >>> (50-5*6)/4
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
474 5
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
475 >>> # Integer division returns the floor:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
476 ... 7/3
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 2
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
478 >>> 7/-3
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
479 -3
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
481 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
482 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
483 Like in C, the equal sign ({\tt =}) is used to assign a value to a
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
484 variable. The value of an assignment is not written:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
485
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
486 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 >>> width = 20
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 >>> height = 5*9
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 >>> width * height
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 900
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
492 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
493 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
494 A value can be assigned to several variables simultaneously:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
495
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
496 \bcode\begin{verbatim}
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
497 >>> x = y = z = 0 # Zero x, y and z
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
498 >>> x
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
499 0
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
500 >>> y
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
501 0
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
502 >>> z
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
503 0
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
504 >>>
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
505 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
506 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
507 There is full support for floating point; operators with mixed type
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
508 operands convert the integer operand to floating point:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
509
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
510 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
511 >>> 4 * 2.5 / 3.3
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 3.0303030303
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
513 >>> 7.0 / 2
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
514 3.5
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
516 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
517
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
518 \subsection{Strings}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
519
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
520 Besides numbers, Python can also manipulate strings, enclosed in
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
521 single quotes or double quotes:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
522
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
523 \bcode\begin{verbatim}
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
524 >>> 'spam eggs'
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
525 'spam eggs'
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 >>> 'doesn\'t'
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
527 "doesn't"
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
528 >>> "doesn't"
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
529 "doesn't"
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
530 >>> '"Yes," he said.'
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
531 '"Yes," he said.'
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
532 >>> "\"Yes,\" he said."
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
533 '"Yes," he said.'
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
534 >>> '"Isn\'t," she said.'
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
535 '"Isn\'t," she said.'
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
537 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
538 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
539 Strings are written the same way as they are typed for input: inside
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
540 quotes and with quotes and other funny characters escaped by backslashes,
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
541 to show the precise value. The string is enclosed in double quotes if
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
542 the string contains a single quote and no double quotes, else it's
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
543 enclosed in single quotes. (The {\tt print} statement, described later,
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
544 can be used to write strings without quotes or escapes.)
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
545
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
546 Strings can be concatenated (glued together) with the {\tt +}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
547 operator, and repeated with {\tt *}:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
548
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
549 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 >>> word = 'Help' + 'A'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 >>> word
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 'HelpA'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 >>> '<' + word*5 + '>'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 '<HelpAHelpAHelpAHelpAHelpA>'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
556 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
557 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
558 Strings can be subscripted (indexed); like in C, the first character of
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
559 a string has subscript (index) 0.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
560
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 There is no separate character type; a character is simply a string of
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
562 size one. Like in Icon, substrings can be specified with the {\em
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
563 slice} notation: two indices separated by a colon.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
564
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
565 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 >>> word[4]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 'A'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 >>> word[0:2]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 'He'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 >>> word[2:4]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 'lp'
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
572 >>>
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
573 \end{verbatim}\ecode
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
574 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
575 Slice indices have useful defaults; an omitted first index defaults to
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
576 zero, an omitted second index defaults to the size of the string being
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
577 sliced.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
578
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
579 \bcode\begin{verbatim}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
580 >>> word[:2] # The first two characters
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 'He'
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
582 >>> word[2:] # All but the first two characters
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 'lpA'
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
584 >>>
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
585 \end{verbatim}\ecode
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
586 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
587 Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
588 equals \verb\s\.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
589
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
590 \bcode\begin{verbatim}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
591 >>> word[:2] + word[2:]
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
592 'HelpA'
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 >>> word[:3] + word[3:]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 'HelpA'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
596 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
597 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
598 Degenerate slice indices are handled gracefully: an index that is too
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
599 large is replaced by the string size, an upper bound smaller than the
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
600 lower bound returns an empty string.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
601
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
602 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 >>> word[1:100]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 'elpA'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 >>> word[10:]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 ''
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 >>> word[2:1]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 ''
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
610 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
611 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
612 Indices may be negative numbers, to start counting from the right.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
613 For example:
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
614
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
615 \bcode\begin{verbatim}
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
616 >>> word[-1] # The last character
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
617 'A'
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
618 >>> word[-2] # The last-but-one character
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
619 'p'
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
620 >>> word[-2:] # The last two characters
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 'pA'
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
622 >>> word[:-2] # All but the last two characters
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 'Hel'
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
624 >>>
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
625 \end{verbatim}\ecode
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
626 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
627 But note that -0 is really the same as 0, so it does not count from
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
628 the right!
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
629
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
630 \bcode\begin{verbatim}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
631 >>> word[-0] # (since -0 equals 0)
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
632 'H'
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
633 >>>
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
634 \end{verbatim}\ecode
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
635 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
636 Out-of-range negative slice indices are truncated, but don't try this
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
637 for single-element (non-slice) indices:
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
638
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
639 \bcode\begin{verbatim}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
640 >>> word[-100:]
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 'HelpA'
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
642 >>> word[-10] # error
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
643 Traceback (innermost last):
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
644 File "<stdin>", line 1
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
645 IndexError: string index out of range
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
647 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
648 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 The best way to remember how slices work is to think of the indices as
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
650 pointing {\em between} characters, with the left edge of the first
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
651 character numbered 0. Then the right edge of the last character of a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
652 string of {\tt n} characters has index {\tt n}, for example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
653
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
654 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 +---+---+---+---+---+
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 | H | e | l | p | A |
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 +---+---+---+---+---+
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 0 1 2 3 4 5
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 -5 -4 -3 -2 -1
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
660 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
661 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
662 The first row of numbers gives the position of the indices 0...5 in
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
663 the string; the second row gives the corresponding negative indices.
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
664 The slice from \verb\i\ to \verb\j\ consists of all characters between
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
665 the edges labeled \verb\i\ and \verb\j\, respectively.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
666
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
667 For nonnegative indices, the length of a slice is the difference of
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
668 the indices, if both are within bounds, e.g., the length of
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
669 \verb\word[1:3]\ is 2.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
670
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
671 The built-in function {\tt len()} returns the length of a string:
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
672
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
673 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 >>> s = 'supercalifragilisticexpialidocious'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 >>> len(s)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 34
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
678 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
679
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
680 \subsection{Lists}
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
681
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
682 Python knows a number of {\em compound} data types, used to group
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
683 together other values. The most versatile is the {\em list}, which
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
684 can be written as a list of comma-separated values (items) between
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
685 square brackets. List items need not all have the same type.
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
686
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
687 \bcode\begin{verbatim}
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
688 >>> a = ['spam', 'eggs', 100, 1234]
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 >>> a
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
690 ['spam', 'eggs', 100, 1234]
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
692 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
693 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
694 Like string indices, list indices start at 0, and lists can be sliced,
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
695 concatenated and so on:
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
696
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
697 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 >>> a[0]
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
699 'spam'
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 >>> a[3]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 1234
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
702 >>> a[-2]
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
703 100
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
704 >>> a[1:-1]
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
705 ['eggs', 100]
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
706 >>> a[:2] + ['bacon', 2*2]
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
707 ['spam', 'eggs', 'bacon', 4]
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
708 >>> 3*a[:3] + ['Boe!']
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
709 ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
711 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
712 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
713 Unlike strings, which are {\em immutable}, it is possible to change
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
714 individual elements of a list:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
715
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
716 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 >>> a
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
718 ['spam', 'eggs', 100, 1234]
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 >>> a[2] = a[2] + 23
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 >>> a
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
721 ['spam', 'eggs', 123, 1234]
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
723 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
724 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
725 Assignment to slices is also possible, and this can even change the size
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 of the list:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
727
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
728 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 >>> # Replace some items:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
730 ... a[0:2] = [1, 12]
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 >>> a
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 [1, 12, 123, 1234]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 >>> # Remove some:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
734 ... a[0:2] = []
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 >>> a
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 [123, 1234]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 >>> # Insert some:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
738 ... a[1:1] = ['bletch', 'xyzzy']
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 >>> a
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 [123, 'bletch', 'xyzzy', 1234]
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
741 >>> a[:0] = a # Insert (a copy of) itself at the beginning
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
742 >>> a
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
743 [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
745 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
746 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 The built-in function {\tt len()} also applies to lists:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
748
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
749 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 >>> len(a)
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
751 8
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
753 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
754 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
755 It is possible to nest lists (create lists containing other lists),
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
756 for example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
757
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
758 \bcode\begin{verbatim}
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
759 >>> q = [2, 3]
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
760 >>> p = [1, q, 4]
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
761 >>> len(p)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
762 3
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
763 >>> p[1]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
764 [2, 3]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
765 >>> p[1][0]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
766 2
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
767 >>> p[1].append('xtra') # See section 5.1
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
768 >>> p
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
769 [1, [2, 3, 'xtra'], 4]
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
770 >>> q
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
771 [2, 3, 'xtra']
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
772 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
773 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
774 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
775 Note that in the last example, {\tt p[1]} and {\tt q} really refer to
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
776 the same object! We'll come back to {\em object semantics} later.
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
777
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
778 \section{First Steps Towards Programming}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
780 Of course, we can use Python for more complicated tasks than adding
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
781 two and two together. For instance, we can write an initial
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
782 subsequence of the {\em Fibonacci} series as follows:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
783
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
784 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 >>> # Fibonacci series:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
786 ... # the sum of two elements defines the next
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
787 ... a, b = 0, 1
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
788 >>> while b < 10:
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
789 ... print b
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
790 ... a, b = b, a+b
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 ...
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
792 1
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
793 1
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 2
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
795 3
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 5
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
797 8
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
799 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
800 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 This example introduces several new features.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
802
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
803 \begin{itemize}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
804
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 \item
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
806 The first line contains a {\em multiple assignment}: the variables
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
807 {\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
808 last line this is used again, demonstrating that the expressions on
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
809 the right-hand side are all evaluated first before any of the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
810 assignments take place.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
811
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
812 \item
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
813 The {\tt while} loop executes as long as the condition (here: {\tt b <
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
814 10}) remains true. In Python, like in C, any non-zero integer value is
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
815 true; zero is false. The condition may also be a string or list value,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
816 in fact any sequence; anything with a non-zero length is true, empty
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
817 sequences are false. The test used in the example is a simple
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
818 comparison. The standard comparison operators are written the same as
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
819 in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
820
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 \item
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
822 The {\em body} of the loop is {\em indented}: indentation is Python's
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
823 way of grouping statements. Python does not (yet!) provide an
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
824 intelligent input line editing facility, so you have to type a tab or
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
825 space(s) for each indented line. In practice you will prepare more
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
826 complicated input for Python with a text editor; most text editors have
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
827 an auto-indent facility. When a compound statement is entered
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
828 interactively, it must be followed by a blank line to indicate
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
829 completion (since the parser cannot guess when you have typed the last
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
830 line).
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
831
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
832 \item
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
833 The {\tt print} statement writes the value of the expression(s) it is
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
834 given. It differs from just writing the expression you want to write
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
835 (as we did earlier in the calculator examples) in the way it handles
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
836 multiple expressions and strings. Strings are printed without quotes,
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
837 and a space is inserted between items, so you can format things nicely,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
838 like this:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
839
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
840 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
841 >>> i = 256*256
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 >>> print 'The value of i is', i
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
843 The value of i is 65536
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
844 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
845 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
846 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
847 A trailing comma avoids the newline after the output:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
848
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
849 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 >>> a, b = 0, 1
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 >>> while b < 1000:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
852 ... print b,
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
853 ... a, b = b, a+b
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 ...
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
855 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
857 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
858 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
859 Note that the interpreter inserts a newline before it prints the next
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
860 prompt if the last line was not completed.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
861
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
862 \end{itemize}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
863
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
864
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
865 \chapter{More Control Flow Tools}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
866
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
867 Besides the {\tt while} statement just introduced, Python knows the
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
868 usual control flow statements known from other languages, with some
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
869 twists.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
870
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
871 \section{If Statements}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
872
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 Perhaps the most well-known statement type is the {\tt if} statement.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
875
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
876 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
877 >>> if x < 0:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 ... x = 0
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
879 ... print 'Negative changed to zero'
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
880 ... elif x == 0:
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
881 ... print 'Zero'
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
882 ... elif x == 1:
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
883 ... print 'Single'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
884 ... else:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
885 ... print 'More'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
886 ...
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
887 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
888 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
889 There can be zero or more {\tt elif} parts, and the {\tt else} part is
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
890 optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
891 useful to avoid excessive indentation. An {\tt if...elif...elif...}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
892 sequence is a substitute for the {\em switch} or {\em case} statements
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
893 found in other languages.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
894
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
895 \section{For Statements}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
896
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
897 The {\tt for} statement in Python differs a bit from what you may be
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
898 used to in C or Pascal. Rather than always iterating over an
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
899 arithmetic progression of numbers (like in Pascal), or leaving the user
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
900 completely free in the iteration test and step (as C), Python's {\tt
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
901 for} statement iterates over the items of any sequence (e.g., a list
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
902 or a string), in the order that they appear in the sequence. For
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
903 example (no pun intended):
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
904
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
905 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
906 >>> # Measure some strings:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
907 ... a = ['cat', 'window', 'defenestrate']
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
908 >>> for x in a:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
909 ... print x, len(x)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
910 ...
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
911 cat 3
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
912 window 6
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
913 defenestrate 12
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
914 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
915 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
916 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
917 It is not safe to modify the sequence being iterated over in the loop
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
918 (this can only happen for mutable sequence types, i.e., lists). If
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
919 you need to modify the list you are iterating over, e.g., duplicate
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
920 selected items, you must iterate over a copy. The slice notation
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
921 makes this particularly convenient:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
922
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
923 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
924 >>> for x in a[:]: # make a slice copy of the entire list
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
925 ... if len(x) > 6: a.insert(0, x)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
926 ...
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
927 >>> a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
928 ['defenestrate', 'cat', 'window', 'defenestrate']
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
929 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
930 \end{verbatim}\ecode
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
931
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
932 \section{The {\tt range()} Function}
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
933
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
934 If you do need to iterate over a sequence of numbers, the built-in
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
935 function {\tt range()} comes in handy. It generates lists containing
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
936 arithmetic progressions, e.g.:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
937
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
938 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
939 >>> range(10)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
940 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
942 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
943 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
944 The given end point is never part of the generated list; {\tt range(10)}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
945 generates a list of 10 values, exactly the legal indices for items of a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
946 sequence of length 10. It is possible to let the range start at another
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
947 number, or to specify a different increment (even negative):
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
948
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
949 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 >>> range(5, 10)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
951 [5, 6, 7, 8, 9]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
952 >>> range(0, 10, 3)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
953 [0, 3, 6, 9]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
954 >>> range(-10, -100, -30)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
955 [-10, -40, -70]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
956 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
957 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
958 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
959 To iterate over the indices of a sequence, combine {\tt range()} and
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
960 {\tt len()} as follows:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
961
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
962 \bcode\begin{verbatim}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
963 >>> a = ['Mary', 'had', 'a', 'little', 'lamb']
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
964 >>> for i in range(len(a)):
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
965 ... print i, a[i]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 ...
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 0 Mary
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
968 1 had
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
969 2 a
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 3 little
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
971 4 lamb
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
973 \end{verbatim}\ecode
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
974
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
975 \section{Break and Continue Statements, and Else Clauses on Loops}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
976
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
977 The {\tt break} statement, like in C, breaks out of the smallest
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
978 enclosing {\tt for} or {\tt while} loop.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
979
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
980 The {\tt continue} statement, also borrowed from C, continues with the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
981 next iteration of the loop.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
982
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
983 Loop statements may have an {\tt else} clause; it is executed when the
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
984 loop terminates through exhaustion of the list (with {\tt for}) or when
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
985 the condition becomes false (with {\tt while}), but not when the loop is
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
986 terminated by a {\tt break} statement. This is exemplified by the
2013
Guido van Rossum <guido@python.org>
parents: 1947
diff changeset
987 following loop, which searches for prime numbers:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
988
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
989 \bcode\begin{verbatim}
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
990 >>> for n in range(2, 10):
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
991 ... for x in range(2, n):
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
992 ... if n % x == 0:
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
993 ... print n, 'equals', x, '*', n/x
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
994 ... break
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
995 ... else:
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
996 ... print n, 'is a prime number'
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 ...
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
998 2 is a prime number
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
999 3 is a prime number
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1000 4 equals 2 * 2
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1001 5 is a prime number
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1002 6 equals 2 * 3
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1003 7 is a prime number
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1004 8 equals 2 * 4
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1005 9 equals 3 * 3
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1007 \end{verbatim}\ecode
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1009 \section{Pass Statements}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1010
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1011 The {\tt pass} statement does nothing.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 It can be used when a statement is required syntactically but the
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013 program requires no action.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1015
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1016 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017 >>> while 1:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018 ... pass # Busy-wait for keyboard interrupt
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019 ...
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1020 \end{verbatim}\ecode
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1021
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1022 \section{Defining Functions}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024 We can create a function that writes the Fibonacci series to an
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 arbitrary boundary:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1026
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1027 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1028 >>> def fib(n): # write Fibonacci series up to n
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1029 ... a, b = 0, 1
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
1030 ... while b < n:
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 ... print b,
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032 ... a, b = b, a+b
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 ...
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034 >>> # Now call the function we just defined:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1035 ... fib(2000)
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1037 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1038 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1039 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1040 The keyword {\tt def} introduces a function {\em definition}. It must
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1041 be followed by the function name and the parenthesized list of formal
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1042 parameters. The statements that form the body of the function starts at
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1043 the next line, indented by a tab stop.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1044
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1045 The {\em execution} of a function introduces a new symbol table used
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1046 for the local variables of the function. More precisely, all variable
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1047 assignments in a function store the value in the local symbol table;
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1048 whereas
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1049 variable references first look in the local symbol table, then
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1050 in the global symbol table, and then in the table of built-in names.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1051 Thus,
2013
Guido van Rossum <guido@python.org>
parents: 1947
diff changeset
1052 global variables cannot be directly assigned a value within a
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1053 function (unless named in a {\tt global} statement), although
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1054 they may be referenced.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1055
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056 The actual parameters (arguments) to a function call are introduced in
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1057 the local symbol table of the called function when it is called; thus,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1058 arguments are passed using {\em call\ by\ value}.%
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 \footnote{
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1060 Actually, {\em call by object reference} would be a better
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1061 description, since if a mutable object is passed, the caller
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1062 will see any changes the callee makes to it (e.g., items
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1063 inserted into a list).
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1064 }
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 When a function calls another function, a new local symbol table is
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1066 created for that call.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1068 A function definition introduces the function name in the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1069 current
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1070 symbol table. The value
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1071 of the function name
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1072 has a type that is recognized by the interpreter as a user-defined
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1073 function. This value can be assigned to another name which can then
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1074 also be used as a function. This serves as a general renaming
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1075 mechanism:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1076
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1077 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1078 >>> fib
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1079 <function object at 10042ed0>
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1080 >>> f = fib
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1081 >>> f(100)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1082 1 1 2 3 5 8 13 21 34 55 89
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1083 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1084 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1085 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1086 You might object that {\tt fib} is not a function but a procedure. In
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1087 Python, like in C, procedures are just functions that don't return a
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1088 value. In fact, technically speaking, procedures do return a value,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1089 albeit a rather boring one. This value is called {\tt None} (it's a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1090 built-in name). Writing the value {\tt None} is normally suppressed by
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1091 the interpreter if it would be the only value written. You can see it
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1092 if you really want to:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1093
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1094 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 >>> print fib(0)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1096 None
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1098 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1099 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 It is simple to write a function that returns a list of the numbers of
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101 the Fibonacci series, instead of printing it:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1102
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1103 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104 >>> def fib2(n): # return Fibonacci series up to n
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1105 ... result = []
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1106 ... a, b = 0, 1
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
1107 ... while b < n:
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1108 ... result.append(b) # see below
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1109 ... a, b = b, a+b
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1110 ... return result
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111 ...
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1112 >>> f100 = fib2(100) # call it
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1113 >>> f100 # write the result
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1114 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1115 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1116 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1117 %
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
1118 This example, as usual, demonstrates some new Python features:
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1119
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1120 \begin{itemize}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1121
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1122 \item
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1123 The {\tt return} statement returns with a value from a function. {\tt
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1124 return} without an expression argument is used to return from the middle
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1125 of a procedure (falling off the end also returns from a procedure), in
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1126 which case the {\tt None} value is returned.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1127
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 \item
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1129 The statement {\tt result.append(b)} calls a {\em method} of the list
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1130 object {\tt result}. A method is a function that `belongs' to an
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1131 object and is named {\tt obj.methodname}, where {\tt obj} is some
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1132 object (this may be an expression), and {\tt methodname} is the name
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1133 of a method that is defined by the object's type. Different types
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1134 define different methods. Methods of different types may have the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1135 same name without causing ambiguity. (It is possible to define your
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1136 own object types and methods, using {\em classes}, as discussed later
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1137 in this tutorial.)
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1138 The method {\tt append} shown in the example, is defined for
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1139 list objects; it adds a new element at the end of the list. In this
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1140 example
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1141 it is equivalent to {\tt result = result + [b]}, but more efficient.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1142
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1143 \end{itemize}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1144
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
1145
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1146 \chapter{Odds and Ends}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1147
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1148 This chapter describes some things you've learned about already in
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1149 more detail, and adds some new things as well.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1150
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1151 \section{More on Lists}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1152
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1153 The list data type has some more methods. Here are all of the methods
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1154 of lists objects:
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1155
121
0c25faf4a6a4 Small changes. Use description instead of list env.
Guido van Rossum <guido@python.org>
parents: 112
diff changeset
1156 \begin{description}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1157
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1158 \item[{\tt insert(i, x)}]
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1159 Insert an item at a given position. The first argument is the index of
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1160 the element before which to insert, so {\tt a.insert(0, x)} inserts at
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1161 the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1162 {\tt a.append(x)}.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1163
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1164 \item[{\tt append(x)}]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1165 Equivalent to {\tt a.insert(len(a), x)}.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1166
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1167 \item[{\tt index(x)}]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1168 Return the index in the list of the first item whose value is {\tt x}.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1169 It is an error if there is no such item.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1170
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1171 \item[{\tt remove(x)}]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1172 Remove the first item from the list whose value is {\tt x}.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1173 It is an error if there is no such item.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1174
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1175 \item[{\tt sort()}]
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1176 Sort the items of the list, in place.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1177
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1178 \item[{\tt reverse()}]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1179 Reverse the elements of the list, in place.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1180
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1181 \item[{\tt count(x)}]
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1182 Return the number of times {\tt x} appears in the list.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1183
121
0c25faf4a6a4 Small changes. Use description instead of list env.
Guido van Rossum <guido@python.org>
parents: 112
diff changeset
1184 \end{description}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1185
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1186 An example that uses all list methods:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1187
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1188 \bcode\begin{verbatim}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1189 >>> a = [66.6, 333, 333, 1, 1234.5]
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1190 >>> print a.count(333), a.count(66.6), a.count('x')
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1191 2 1 0
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 >>> a.insert(2, -1)
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1193 >>> a.append(333)
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194 >>> a
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1195 [66.6, 333, -1, 333, 1, 1234.5, 333]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1196 >>> a.index(333)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1197 1
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1198 >>> a.remove(333)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1199 >>> a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1200 [66.6, -1, 333, 1, 1234.5, 333]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1201 >>> a.reverse()
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1202 >>> a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1203 [333, 1234.5, 1, 333, -1, 66.6]
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204 >>> a.sort()
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205 >>> a
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1206 [-1, 1, 66.6, 333, 333, 1234.5]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1207 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1208 \end{verbatim}\ecode
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1209
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1210 \section{The {\tt del} statement}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1211
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1212 There is a way to remove an item from a list given its index instead
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1213 of its value: the {\tt del} statement. This can also be used to
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1214 remove slices from a list (which we did earlier by assignment of an
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1215 empty list to the slice). For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1216
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1217 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1218 >>> a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1219 [-1, 1, 66.6, 333, 333, 1234.5]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1220 >>> del a[0]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1221 >>> a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1222 [1, 66.6, 333, 333, 1234.5]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1223 >>> del a[2:4]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1224 >>> a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1225 [1, 66.6, 1234.5]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1226 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1227 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1228 %
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1229 {\tt del} can also be used to delete entire variables:
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1230
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1231 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1232 >>> del a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1233 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1234 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1235 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1236 Referencing the name {\tt a} hereafter is an error (at least until
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1237 another value is assigned to it). We'll find other uses for {\tt del}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1238 later.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1239
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1240 \section{Tuples and Sequences}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1241
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1242 We saw that lists and strings have many common properties, e.g.,
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1243 indexing and slicing operations. They are two examples of {\em
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1244 sequence} data types. Since Python is an evolving language, other
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1245 sequence data types may be added. There is also another standard
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1246 sequence data type: the {\em tuple}.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1247
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1248 A tuple consists of a number of values separated by commas, for
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1249 instance:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1250
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1251 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1252 >>> t = 12345, 54321, 'hello!'
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1253 >>> t[0]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1254 12345
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1255 >>> t
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1256 (12345, 54321, 'hello!')
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1257 >>> # Tuples may be nested:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1258 ... u = t, (1, 2, 3, 4, 5)
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1259 >>> u
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1260 ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1261 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1262 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1263 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1264 As you see, on output tuples are alway enclosed in parentheses, so
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1265 that nested tuples are interpreted correctly; they may be input with
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1266 or without surrounding parentheses, although often parentheses are
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1267 necessary anyway (if the tuple is part of a larger expression).
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1268
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1269 Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1270 from a database, etc. Tuples, like strings, are immutable: it is not
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1271 possible to assign to the individual items of a tuple (you can
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1272 simulate much of the same effect with slicing and concatenation,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1273 though).
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1274
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1275 A special problem is the construction of tuples containing 0 or 1
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1276 items: the syntax has some extra quirks to accommodate these. Empty
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1277 tuples are constructed by an empty pair of parentheses; a tuple with
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1278 one item is constructed by following a value with a comma
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1279 (it is not sufficient to enclose a single value in parentheses).
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1280 Ugly, but effective. For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1281
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1282 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1283 >>> empty = ()
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1284 >>> singleton = 'hello', # <-- note trailing comma
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1285 >>> len(empty)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1286 0
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1287 >>> len(singleton)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1288 1
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1289 >>> singleton
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1290 ('hello',)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1291 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1292 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1293 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1294 The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1295 tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1296 are packed together in a tuple. The reverse operation is also
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1297 possible, e.g.:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1298
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1299 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1300 >>> x, y, z = t
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1301 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1302 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1303 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1304 This is called, appropriately enough, {\em tuple unpacking}. Tuple
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1305 unpacking requires that the list of variables on the left has the same
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1306 number of elements as the length of the tuple. Note that multiple
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1307 assignment is really just a combination of tuple packing and tuple
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1308 unpacking!
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1309
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1310 Occasionally, the corresponding operation on lists is useful: {\em list
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1311 unpacking}. This is supported by enclosing the list of variables in
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1312 square brackets:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1313
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1314 \bcode\begin{verbatim}
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
1315 >>> a = ['spam', 'eggs', 100, 1234]
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1316 >>> [a1, a2, a3, a4] = a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1317 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1318 \end{verbatim}\ecode
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1319
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1320 \section{Dictionaries}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1321
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1322 Another useful data type built into Python is the {\em dictionary}.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1323 Dictionaries are sometimes found in other languages as ``associative
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1324 memories'' or ``associative arrays''. Unlike sequences, which are
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1325 indexed by a range of numbers, dictionaries are indexed by {\em keys},
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1326 which are strings (the use of non-string values as keys
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1327 is supported, but beyond the scope of this tutorial).
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1328 It is best to think of a dictionary as an unordered set of
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1329 {\em key:value} pairs, with the requirement that the keys are unique
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1330 (within one dictionary).
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1331 A pair of braces creates an empty dictionary: \verb/{}/.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1332 Placing a comma-separated list of key:value pairs within the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1333 braces adds initial key:value pairs to the dictionary; this is also the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1334 way dictionaries are written on output.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1335
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1336 The main operations on a dictionary are storing a value with some key
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1337 and extracting the value given the key. It is also possible to delete
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1338 a key:value pair
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1339 with {\tt del}.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1340 If you store using a key that is already in use, the old value
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1341 associated with that key is forgotten. It is an error to extract a
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1342 value using a non-existent key.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1343
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1344 The {\tt keys()} method of a dictionary object returns a list of all the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1345 keys used in the dictionary, in random order (if you want it sorted,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1346 just apply the {\tt sort()} method to the list of keys). To check
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1347 whether a single key is in the dictionary, use the \verb/has_key()/
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1348 method of the dictionary.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1349
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1350 Here is a small example using a dictionary:
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1351
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1352 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1353 >>> tel = {'jack': 4098, 'sape': 4139}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1354 >>> tel['guido'] = 4127
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1355 >>> tel
440
e2db3341d686 Fixed dictionary example to use commas instead of semicolons.
Guido van Rossum <guido@python.org>
parents: 342
diff changeset
1356 {'sape': 4139, 'guido': 4127, 'jack': 4098}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1357 >>> tel['jack']
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1358 4098
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1359 >>> del tel['sape']
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1360 >>> tel['irv'] = 4127
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1361 >>> tel
440
e2db3341d686 Fixed dictionary example to use commas instead of semicolons.
Guido van Rossum <guido@python.org>
parents: 342
diff changeset
1362 {'guido': 4127, 'irv': 4127, 'jack': 4098}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1363 >>> tel.keys()
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1364 ['guido', 'irv', 'jack']
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1365 >>> tel.has_key('guido')
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1366 1
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1367 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1368 \end{verbatim}\ecode
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1369
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1370 \section{More on Conditions}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1371
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1372 The conditions used in {\tt while} and {\tt if} statements above can
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1373 contain other operators besides comparisons.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1374
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1375 The comparison operators {\tt in} and {\tt not in} check whether a value
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1376 occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1377 is not} compare whether two objects are really the same object; this
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1378 only matters for mutable objects like lists. All comparison operators
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1379 have the same priority, which is lower than that of all numerical
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1380 operators.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1381
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
1382 Comparisons can be chained: e.g., {\tt a < b == c} tests whether {\tt a}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1383 is less than {\tt b} and moreover {\tt b} equals {\tt c}.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1384
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1385 Comparisons may be combined by the Boolean operators {\tt and} and {\tt
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1386 or}, and the outcome of a comparison (or of any other Boolean
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1387 expression) may be negated with {\tt not}. These all have lower
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1388 priorities than comparison operators again; between them, {\tt not} has
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1389 the highest priority, and {\tt or} the lowest, so that
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1390 {\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1391 course, parentheses can be used to express the desired composition.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1392
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1393 The Boolean operators {\tt and} and {\tt or} are so-called {\em
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1394 shortcut} operators: their arguments are evaluated from left to right,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1395 and evaluation stops as soon as the outcome is determined. E.g., if
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1396 {\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1397 does not evaluate the expression C. In general, the return value of a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1398 shortcut operator, when used as a general value and not as a Boolean, is
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1399 the last evaluated argument.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1400
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1401 It is possible to assign the result of a comparison or other Boolean
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1402 expression to a variable. For example,
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1403
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1404 \bcode\begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1405 >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1406 >>> non_null = string1 or string2 or string3
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1407 >>> non_null
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1408 'Trondheim'
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1409 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1410 \end{verbatim}\ecode
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1411 %
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1412 Note that in Python, unlike C, assignment cannot occur inside expressions.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1413
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1414 \section{Comparing Sequences and Other Types}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1415
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1416 Sequence objects may be compared to other objects with the same
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1417 sequence type. The comparison uses {\em lexicographical} ordering:
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1418 first the first two items are compared, and if they differ this
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1419 determines the outcome of the comparison; if they are equal, the next
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1420 two items are compared, and so on, until either sequence is exhausted.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1421 If two items to be compared are themselves sequences of the same type,
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1422 the lexicographical comparison is carried out recursively. If all
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1423 items of two sequences compare equal, the sequences are considered
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1424 equal. If one sequence is an initial subsequence of the other, the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1425 shorted sequence is the smaller one. Lexicographical ordering for
2550
0f6ff45bd816 replace ASCII by macro call
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
1426 strings uses the \ASCII{} ordering for individual characters. Some
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1427 examples of comparisons between sequences with the same types:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1428
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1429 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1430 (1, 2, 3) < (1, 2, 4)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1431 [1, 2, 3] < [1, 2, 4]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1432 'ABC' < 'C' < 'Pascal' < 'Python'
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1433 (1, 2, 3, 4) < (1, 2, 4)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1434 (1, 2) < (1, 2, -1)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1435 (1, 2, 3) = (1.0, 2.0, 3.0)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1436 (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1437 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1438 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1439 Note that comparing objects of different types is legal. The outcome
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1440 is deterministic but arbitrary: the types are ordered by their name.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1441 Thus, a list is always smaller than a string, a string is always
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1442 smaller than a tuple, etc. Mixed numeric types are compared according
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1443 to their numeric value, so 0 equals 0.0, etc.%
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1444 \footnote{
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1445 The rules for comparing objects of different types should
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1446 not be relied upon; they may change in a future version of
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1447 the language.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1448 }
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1449
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
1450
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1451 \chapter{Modules}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1452
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
1453 If you quit from the Python interpreter and enter it again, the
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1454 definitions you have made (functions and variables) are lost.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1455 Therefore, if you want to write a somewhat longer program, you are
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1456 better off using a text editor to prepare the input for the interpreter
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
1457 and running it with that file as input instead. This is known as creating a
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1458 {\em script}. As your program gets longer, you may want to split it
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1459 into several files for easier maintenance. You may also want to use a
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1460 handy function that you've written in several programs without copying
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1461 its definition into each program.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1462
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
1463 To support this, Python has a way to put definitions in a file and use
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1464 them in a script or in an interactive instance of the interpreter.
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1465 Such a file is called a {\em module}; definitions from a module can be
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1466 {\em imported} into other modules or into the {\em main} module (the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1467 collection of variables that you have access to in a script
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1468 executed at the top level
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1469 and in calculator mode).
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1470
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1471 A module is a file containing Python definitions and statements. The
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1472 file name is the module name with the suffix {\tt .py} appended. Within
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1473 a module, the module's name (as a string) is available as the value of
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1474 the global variable {\tt __name__}. For instance, use your favorite text
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1475 editor to create a file called {\tt fibo.py} in the current directory
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1476 with the following contents:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1477
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1478 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1479 # Fibonacci numbers module
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1480
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1481 def fib(n): # write Fibonacci series up to n
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1482 a, b = 0, 1
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
1483 while b < n:
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1484 print b,
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1485 a, b = b, a+b
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1486
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1487 def fib2(n): # return Fibonacci series up to n
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1488 result = []
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1489 a, b = 0, 1
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
1490 while b < n:
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1491 result.append(b)
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1492 a, b = b, a+b
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1493 return result
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1494 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1495 %
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
1496 Now enter the Python interpreter and import this module with the
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1497 following command:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1498
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1499 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1500 >>> import fibo
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1501 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1502 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1503 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1504 This does not enter the names of the functions defined in
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1505 {\tt fibo}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1506 directly in the current symbol table; it only enters the module name
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1507 {\tt fibo}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1508 there.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1509 Using the module name you can access the functions:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1510
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1511 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1512 >>> fibo.fib(1000)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1513 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1514 >>> fibo.fib2(100)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1515 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1516 >>> fibo.__name__
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1517 'fibo'
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1518 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1519 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1520 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1521 If you intend to use a function often you can assign it to a local name:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1522
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1523 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1524 >>> fib = fibo.fib
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1525 >>> fib(500)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1526 1 1 2 3 5 8 13 21 34 55 89 144 233 377
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1527 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1528 \end{verbatim}\ecode
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1529
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1530 \section{More on Modules}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1531
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1532 A module can contain executable statements as well as function
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1533 definitions.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1534 These statements are intended to initialize the module.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1535 They are executed only the
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1536 {\em first}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1537 time the module is imported somewhere.%
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1538 \footnote{
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1539 In fact function definitions are also `statements' that are
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1540 `executed'; the execution enters the function name in the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1541 module's global symbol table.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1542 }
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1543
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1544 Each module has its own private symbol table, which is used as the
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1545 global symbol table by all functions defined in the module.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1546 Thus, the author of a module can use global variables in the module
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1547 without worrying about accidental clashes with a user's global
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1548 variables.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1549 On the other hand, if you know what you are doing you can touch a
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1550 module's global variables with the same notation used to refer to its
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1551 functions,
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1552 {\tt modname.itemname}.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1553
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1554 Modules can import other modules.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1555 It is customary but not required to place all
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1556 {\tt import}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1557 statements at the beginning of a module (or script, for that matter).
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1558 The imported module names are placed in the importing module's global
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1559 symbol table.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1560
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1561 There is a variant of the
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1562 {\tt import}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1563 statement that imports names from a module directly into the importing
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1564 module's symbol table.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1565 For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1566
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1567 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1568 >>> from fibo import fib, fib2
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1569 >>> fib(500)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1570 1 1 2 3 5 8 13 21 34 55 89 144 233 377
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1571 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1572 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1573 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1574 This does not introduce the module name from which the imports are taken
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1575 in the local symbol table (so in the example, {\tt fibo} is not
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1576 defined).
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1577
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1578 There is even a variant to import all names that a module defines:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1579
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1580 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1581 >>> from fibo import *
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1582 >>> fib(500)
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1583 1 1 2 3 5 8 13 21 34 55 89 144 233 377
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1584 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1585 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1586 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1587 This imports all names except those beginning with an underscore
697
7c06bf15fd2a _ can now be a normal character.
Guido van Rossum <guido@python.org>
parents: 670
diff changeset
1588 ({\tt _}).
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1589
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1590 \section{Standard Modules}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1591
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
1592 Python comes with a library of standard modules, described in a separate
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1593 document (Python Library Reference). Some modules are built into the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1594 interpreter; these provide access to operations that are not part of the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1595 core of the language but are nevertheless built in, either for
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1596 efficiency or to provide access to operating system primitives such as
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1597 system calls. The set of such modules is a configuration option; e.g.,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1598 the {\tt amoeba} module is only provided on systems that somehow support
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1599 Amoeba primitives. One particular module deserves some attention: {\tt
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1600 sys}, which is built into every Python interpreter. The variables {\tt
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1601 sys.ps1} and {\tt sys.ps2} define the strings used as primary and
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1602 secondary prompts:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1603
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1604 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1605 >>> import sys
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1606 >>> sys.ps1
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1607 '>>> '
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1608 >>> sys.ps2
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1609 '... '
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1610 >>> sys.ps1 = 'C> '
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1611 C> print 'Yuck!'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1612 Yuck!
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1613 C>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1614 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1615 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1616 These two variables are only defined if the interpreter is in
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1617 interactive mode.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1618
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1619 The variable
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1620 {\tt sys.path}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1621 is a list of strings that determine the interpreter's search path for
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1622 modules.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1623 It is initialized to a default path taken from the environment variable
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1624 {\tt PYTHONPATH},
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1625 or from a built-in default if
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1626 {\tt PYTHONPATH}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1627 is not set.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1628 You can modify it using standard list operations, e.g.:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1629
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1630 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1631 >>> import sys
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1632 >>> sys.path.append('/ufs/guido/lib/python')
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1633 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1634 \end{verbatim}\ecode
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1635
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1636 \section{The {\tt dir()} function}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1637
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1638 The built-in function {\tt dir} is used to find out which names a module
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1639 defines. It returns a sorted list of strings:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1640
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1641 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1642 >>> import fibo, sys
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1643 >>> dir(fibo)
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1644 ['__name__', 'fib', 'fib2']
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1645 >>> dir(sys)
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1646 ['__name__', 'argv', 'builtin_module_names', 'copyright', 'exit',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1647 'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1648 'stderr', 'stdin', 'stdout', 'version']
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1649 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1650 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1651 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1652 Without arguments, {\tt dir()} lists the names you have defined currently:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1653
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1654 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1655 >>> a = [1, 2, 3, 4, 5]
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1656 >>> import fibo, sys
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1657 >>> fib = fibo.fib
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1658 >>> dir()
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1659 ['__name__', 'a', 'fib', 'fibo', 'sys']
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1660 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1661 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1662 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1663 Note that it lists all types of names: variables, modules, functions, etc.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1664
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1665 {\tt dir()} does not list the names of built-in functions and variables.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1666 If you want a list of those, they are defined in the standard module
1374
89d7d5e136ad * lib3.tex (module string): added rindex().
Guido van Rossum <guido@python.org>
parents: 1283
diff changeset
1667 {\tt __builtin__}:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1668
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1669 \bcode\begin{verbatim}
1374
89d7d5e136ad * lib3.tex (module string): added rindex().
Guido van Rossum <guido@python.org>
parents: 1283
diff changeset
1670 >>> import __builtin__
89d7d5e136ad * lib3.tex (module string): added rindex().
Guido van Rossum <guido@python.org>
parents: 1283
diff changeset
1671 >>> dir(__builtin__)
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1672 ['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1673 'ImportError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1674 'MemoryError', 'NameError', 'None', 'OverflowError', 'RuntimeError',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1675 'SyntaxError', 'SystemError', 'SystemExit', 'TypeError', 'ValueError',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1676 'ZeroDivisionError', '__name__', 'abs', 'apply', 'chr', 'cmp', 'coerce',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1677 'compile', 'dir', 'divmod', 'eval', 'execfile', 'filter', 'float',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1678 'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'len', 'long',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1679 'map', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1680 'reduce', 'reload', 'repr', 'round', 'setattr', 'str', 'type', 'xrange']
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1681 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1682 \end{verbatim}\ecode
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1683
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
1684
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1685 \chapter{Output Formatting}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1686
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1687 So far we've encountered two ways of writing values: {\em expression
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1688 statements} and the {\tt print} statement. (A third way is using the
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1689 {\tt write} method of file objects; the standard output file can be
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1690 referenced as {\tt sys.stdout}. See the Library Reference for more
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1691 information on this.)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1692
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1693 Often you'll want more control over the formatting of your output than
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1694 simply printing space-separated values. The key to nice formatting in
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1695 Python is to do all the string handling yourself; using string slicing
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1696 and concatenation operations you can create any lay-out you can imagine.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1697 The standard module {\tt string} contains some useful operations for
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1698 padding strings to a given column width; these will be discussed shortly.
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1699 Finally, the \code{\%} operator (modulo) with a string left argument
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1700 interprets this string as a C sprintf format string to be applied to the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1701 right argument, and returns the string resulting from this formatting
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1702 operation.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1703
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1704 One question remains, of course: how do you convert values to strings?
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1705 Luckily, Python has a way to convert any value to a string: just write
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1706 the value between reverse quotes (\verb/``/). Some examples:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1707
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1708 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1709 >>> x = 10 * 3.14
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1710 >>> y = 200*200
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1711 >>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1712 >>> print s
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1713 The value of x is 31.4, and y is 40000...
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1714 >>> # Reverse quotes work on other types besides numbers:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1715 ... p = [x, y]
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1716 >>> ps = `p`
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1717 >>> ps
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1718 '[31.4, 40000]'
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1719 >>> # Converting a string adds string quotes and backslashes:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1720 ... hello = 'hello, world\n'
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1721 >>> hellos = `hello`
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1722 >>> print hellos
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1723 'hello, world\012'
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1724 >>> # The argument of reverse quotes may be a tuple:
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
1725 ... `x, y, ('spam', 'eggs')`
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
1726 "(31.4, 40000, ('spam', 'eggs'))"
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1727 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1728 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1729 %
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1730 Here are two ways to write a table of squares and cubes:
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1731
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1732 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1733 >>> import string
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1734 >>> for x in range(1, 11):
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1735 ... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1736 ... # Note trailing comma on previous line
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1737 ... print string.rjust(`x*x*x`, 4)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1738 ...
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1739 1 1 1
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1740 2 4 8
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1741 3 9 27
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1742 4 16 64
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1743 5 25 125
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1744 6 36 216
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1745 7 49 343
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1746 8 64 512
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1747 9 81 729
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1748 10 100 1000
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1749 >>> for x in range(1,11):
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1750 ... print '%2d %3d %4d' % (x, x*x, x*x*x)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1751 ...
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1752 1 1 1
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1753 2 4 8
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1754 3 9 27
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1755 4 16 64
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1756 5 25 125
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1757 6 36 216
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1758 7 49 343
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1759 8 64 512
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1760 9 81 729
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1761 10 100 1000
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1762 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1763 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1764 %
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1765 (Note that one space between each column was added by the way {\tt print}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1766 works: it always adds spaces between its arguments.)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1767
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1768 This example demonstrates the function {\tt string.rjust()}, which
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1769 right-justifies a string in a field of a given width by padding it with
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1770 spaces on the left. There are similar functions {\tt string.ljust()}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1771 and {\tt string.center()}. These functions do not write anything, they
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1772 just return a new string. If the input string is too long, they don't
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1773 truncate it, but return it unchanged; this will mess up your column
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1774 lay-out but that's usually better than the alternative, which would be
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1775 lying about a value. (If you really want truncation you can always add
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1776 a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1777
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1778 There is another function, {\tt string.zfill}, which pads a numeric
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1779 string on the left with zeros. It understands about plus and minus
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1780 signs:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1781
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1782 \bcode\begin{verbatim}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1783 >>> string.zfill('12', 5)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1784 '00012'
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1785 >>> string.zfill('-3.14', 7)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1786 '-003.14'
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1787 >>> string.zfill('3.14159265359', 5)
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1788 '3.14159265359'
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1789 >>>
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1790 \end{verbatim}\ecode
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1791
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
1792
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1793 \chapter{Errors and Exceptions}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1794
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1795 Until now error messages haven't been more than mentioned, but if you
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1796 have tried out the examples you have probably seen some. There are
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1797 (at least) two distinguishable kinds of errors: {\em syntax\ errors}
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1798 and {\em exceptions}.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1799
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1800 \section{Syntax Errors}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1801
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1802 Syntax errors, also known as parsing errors, are perhaps the most common
271
0bf02e1b7f51 *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 221
diff changeset
1803 kind of complaint you get while you are still learning Python:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1804
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1805 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1806 >>> while 1 print 'Hello world'
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1807 File "<stdin>", line 1
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1808 while 1 print 'Hello world'
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1809 ^
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1810 SyntaxError: invalid syntax
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1811 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1812 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1813 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1814 The parser repeats the offending line and displays a little `arrow'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1815 pointing at the earliest point in the line where the error was detected.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1816 The error is caused by (or at least detected at) the token
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1817 {\em preceding}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1818 the arrow: in the example, the error is detected at the keyword
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1819 {\tt print}, since a colon ({\tt :}) is missing before it.
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1820 File name and line number are printed so you know where to look in case
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1821 the input came from a script.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1822
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1823 \section{Exceptions}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1824
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1825 Even if a statement or expression is syntactically correct, it may
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1826 cause an error when an attempt is made to execute it.
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1827 Errors detected during execution are called {\em exceptions} and are
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1828 not unconditionally fatal: you will soon learn how to handle them in
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1829 Python programs. Most exceptions are not handled by programs,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1830 however, and result in error messages as shown here:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1831
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1832 \bcode\small\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1833 >>> 10 * (1/0)
1426
c887a1f30525 * lib2.tex (sys): updated sys.std{in,out,err} docs. Added
Guido van Rossum <guido@python.org>
parents: 1412
diff changeset
1834 Traceback (innermost last):
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1835 File "<stdin>", line 1
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1836 ZeroDivisionError: integer division or modulo
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
1837 >>> 4 + spam*3
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1838 Traceback (innermost last):
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1839 File "<stdin>", line 1
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
1840 NameError: spam
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1841 >>> '2' + 2
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1842 Traceback (innermost last):
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1843 File "<stdin>", line 1
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1844 TypeError: illegal argument type for built-in operation
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1845 >>>
4509
9ca59424020e (tut.tex): Added matching \normalsize after \small font control command
Fred Drake <fdrake@acm.org>
parents: 4491
diff changeset
1846 \end{verbatim}\normalsize\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1847 %
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1848 The last line of the error message indicates what happened.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1849 Exceptions come in different types, and the type is printed as part of
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1850 the message: the types in the example are
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1851 {\tt ZeroDivisionError},
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1852 {\tt NameError}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1853 and
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1854 {\tt TypeError}.
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1855 The string printed as the exception type is the name of the built-in
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1856 name for the exception that occurred. This is true for all built-in
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1857 exceptions, but need not be true for user-defined exceptions (although
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1858 it is a useful convention).
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1859 Standard exception names are built-in identifiers (not reserved
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1860 keywords).
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1861
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1862 The rest of the line is a detail whose interpretation depends on the
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1863 exception type; its meaning is dependent on the exception type.
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1864
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1865 The preceding part of the error message shows the context where the
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1866 exception happened, in the form of a stack backtrace.
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1867 In general it contains a stack backtrace listing source lines; however,
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1868 it will not display lines read from standard input.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1869
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
1870 The Python Library Reference Manual lists the built-in exceptions and
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1871 their meanings.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1872
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1873 \section{Handling Exceptions}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1874
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1875 It is possible to write programs that handle selected exceptions.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1876 Look at the following example, which prints a table of inverses of
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1877 some floating point numbers:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1878
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1879 \bcode\begin{verbatim}
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1880 >>> numbers = [0.3333, 2.5, 0, 10]
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1881 >>> for x in numbers:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1882 ... print x,
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1883 ... try:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1884 ... print 1.0 / x
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1885 ... except ZeroDivisionError:
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1886 ... print '*** has no inverse ***'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1887 ...
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1888 0.3333 3.00030003
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1889 2.5 0.4
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1890 0 *** has no inverse ***
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1891 10 0.1
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1892 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1893 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1894 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1895 The {\tt try} statement works as follows.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1896 \begin{itemize}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1897 \item
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1898 First, the
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1899 {\em try\ clause}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1900 (the statement(s) between the {\tt try} and {\tt except} keywords) is
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1901 executed.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1902 \item
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1903 If no exception occurs, the
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1904 {\em except\ clause}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1905 is skipped and execution of the {\tt try} statement is finished.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1906 \item
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1907 If an exception occurs during execution of the try clause,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1908 the rest of the clause is skipped. Then if
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1909 its type matches the exception named after the {\tt except} keyword,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1910 the rest of the try clause is skipped, the except clause is executed,
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1911 and then execution continues after the {\tt try} statement.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1912 \item
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1913 If an exception occurs which does not match the exception named in the
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1914 except clause, it is passed on to outer try statements; if no handler is
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1915 found, it is an
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1916 {\em unhandled\ exception}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1917 and execution stops with a message as shown above.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1918 \end{itemize}
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1919 A {\tt try} statement may have more than one except clause, to specify
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1920 handlers for different exceptions.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1921 At most one handler will be executed.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1922 Handlers only handle exceptions that occur in the corresponding try
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1923 clause, not in other handlers of the same {\tt try} statement.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1924 An except clause may name multiple exceptions as a parenthesized list,
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1925 e.g.:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1926
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1927 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1928 ... except (RuntimeError, TypeError, NameError):
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1929 ... pass
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1930 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1931 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1932 The last except clause may omit the exception name(s), to serve as a
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1933 wildcard.
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1934 Use this with extreme caution, since it is easy to mask a real
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1935 programming error in this way!
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1936
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1937 When an exception occurs, it may have an associated value, also known as
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1938 the exceptions's
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1939 {\em argument}.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1940 The presence and type of the argument depend on the exception type.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1941 For exception types which have an argument, the except clause may
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1942 specify a variable after the exception name (or list) to receive the
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1943 argument's value, as follows:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1944
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1945 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1946 >>> try:
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
1947 ... spam()
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1948 ... except NameError, x:
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1949 ... print 'name', x, 'undefined'
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1950 ...
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
1951 name spam undefined
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1952 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1953 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1954 %
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1955 If an exception has an argument, it is printed as the last part
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1956 (`detail') of the message for unhandled exceptions.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1957
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1958 Exception handlers don't just handle exceptions if they occur
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1959 immediately in the try clause, but also if they occur inside functions
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1960 that are called (even indirectly) in the try clause.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1961 For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1962
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1963 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1964 >>> def this_fails():
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1965 ... x = 1/0
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1966 ...
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1967 >>> try:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1968 ... this_fails()
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1969 ... except ZeroDivisionError, detail:
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1970 ... print 'Handling run-time error:', detail
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1971 ...
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1972 Handling run-time error: integer division or modulo
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1973 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1974 \end{verbatim}\ecode
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1975
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1976 \section{Raising Exceptions}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1977
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1978 The {\tt raise} statement allows the programmer to force a specified
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1979 exception to occur.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1980 For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1981
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1982 \bcode\begin{verbatim}
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1983 >>> raise NameError, 'HiThere'
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
1984 Traceback (innermost last):
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
1985 File "<stdin>", line 1
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
1986 NameError: HiThere
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1987 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1988 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1989 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1990 The first argument to {\tt raise} names the exception to be raised.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1991 The optional second argument specifies the exception's argument.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1992
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
1993 \section{User-defined Exceptions}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1994
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1995 Programs may name their own exceptions by assigning a string to a
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1996 variable.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1997 For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
1998
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
1999 \bcode\begin{verbatim}
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
2000 >>> my_exc = 'my_exc'
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2001 >>> try:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2002 ... raise my_exc, 2*2
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2003 ... except my_exc, val:
221
edda9aad3e8f Minor improvements. Removed footnote about CWI pathnames.
Guido van Rossum <guido@python.org>
parents: 149
diff changeset
2004 ... print 'My exception occurred, value:', val
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2005 ...
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2006 My exception occurred, value: 4
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2007 >>> raise my_exc, 1
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2008 Traceback (innermost last):
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2009 File "<stdin>", line 1
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
2010 my_exc: 1
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2011 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
2012 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
2013 %
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2014 Many standard modules use this to report errors that may occur in
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2015 functions they define.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2016
342
f3795ac146ce Too much to describe changed...
Guido van Rossum <guido@python.org>
parents: 283
diff changeset
2017 \section{Defining Clean-up Actions}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2018
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2019 The {\tt try} statement has another optional clause which is intended to
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2020 define clean-up actions that must be executed under all circumstances.
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2021 For example:
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
2022
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
2023 \bcode\begin{verbatim}
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2024 >>> try:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2025 ... raise KeyboardInterrupt
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2026 ... finally:
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2027 ... print 'Goodbye, world!'
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2028 ...
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2029 Goodbye, world!
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2030 Traceback (innermost last):
128
3bed76cdac0c Most silly things fixed.
Guido van Rossum <guido@python.org>
parents: 121
diff changeset
2031 File "<stdin>", line 2
1283
9181da406978 Lots of small changes collected over months...
Guido van Rossum <guido@python.org>
parents: 1100
diff changeset
2032 KeyboardInterrupt
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2033 >>>
132
65edde022d71 Changed code environment into \bcode and \ecode macros.
Guido van Rossum <guido@python.org>
parents: 128
diff changeset
2034 \end{verbatim}\ecode
563
73e266bf7ac6 Cosmetic changes; added more subsections to chapter 2; new syntax (==).
Guido van Rossum <guido@python.org>
parents: 452
diff changeset
2035 %
933
c432738cf69a Fixed descr of try/finally
Guido van Rossum <guido@python.org>
parents: 929
diff changeset
2036 A {\tt finally} clause is executed whether or not an exception has
c432738cf69a Fixed descr of try/finally
Guido van Rossum <guido@python.org>
parents: 929
diff changeset
2037 occurred in the {\tt try} clause. When an exception has occurred, it
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2038 is re-raised after the {\tt finally} clause is executed. The
933
c432738cf69a Fixed descr of try/finally
Guido van Rossum <guido@python.org>
parents: 929
diff changeset
2039 {\tt finally} clause is also executed ``on the way out'' when the
c432738cf69a Fixed descr of try/finally
Guido van Rossum <guido@python.org>
parents: 929
diff changeset
2040 {\tt try} statement is left via a {\tt break} or {\tt return}
c432738cf69a Fixed descr of try/finally
Guido van Rossum <guido@python.org>
parents: 929
diff changeset
2041 statement.
c432738cf69a Fixed descr of try/finally
Guido van Rossum <guido@python.org>
parents: 929
diff changeset
2042
c432738cf69a Fixed descr of try/finally
Guido van Rossum <guido@python.org>
parents: 929
diff changeset
2043 A {\tt try} statement must either have one or more {\tt except}
c432738cf69a Fixed descr of try/finally
Guido van Rossum <guido@python.org>
parents: 929
diff changeset
2044 clauses or one {\tt finally} clause, but not both.
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2045
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2046
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2047 \chapter{Classes}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2048
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2049 Python's class mechanism adds classes to the language with a minimum
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2050 of new syntax and semantics. It is a mixture of the class mechanisms
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2051 found in \Cpp{} and Modula-3. As is true for modules, classes in Python
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2052 do not put an absolute barrier between definition and user, but rather
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2053 rely on the politeness of the user not to ``break into the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2054 definition.'' The most important features of classes are retained
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2055 with full power, however: the class inheritance mechanism allows
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2056 multiple base classes, a derived class can override any methods of its
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2057 base class(es), a method can call the method of a base class with the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2058 same name. Objects can contain an arbitrary amount of private data.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2059
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2060 In \Cpp{} terminology, all class members (including the data members) are
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2061 {\em public}, and all member functions are {\em virtual}. There are
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2062 no special constructors or destructors. As in Modula-3, there are no
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2063 shorthands for referencing the object's members from its methods: the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2064 method function is declared with an explicit first argument
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2065 representing the object, which is provided implicitly by the call. As
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2066 in Smalltalk, classes themselves are objects, albeit in the wider
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2067 sense of the word: in Python, all data types are objects. This
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2068 provides semantics for importing and renaming. But, just like in \Cpp{}
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2069 or Modula-3, built-in types cannot be used as base classes for
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2070 extension by the user. Also, like in \Cpp{} but unlike in Modula-3, most
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2071 built-in operators with special syntax (arithmetic operators,
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2072 subscripting etc.) can be redefined for class members.
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2073
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2074
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2075 \section{A word about terminology}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2076
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2077 Lacking universally accepted terminology to talk about classes, I'll
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2078 make occasional use of Smalltalk and \Cpp{} terms. (I'd use Modula-3
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2079 terms, since its object-oriented semantics are closer to those of
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2080 Python than \Cpp{}, but I expect that few readers have heard of it...)
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2081
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2082 I also have to warn you that there's a terminological pitfall for
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2083 object-oriented readers: the word ``object'' in Python does not
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2084 necessarily mean a class instance. Like \Cpp{} and Modula-3, and unlike
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2085 Smalltalk, not all types in Python are classes: the basic built-in
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2086 types like integers and lists aren't, and even somewhat more exotic
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2087 types like files aren't. However, {\em all} Python types share a little
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2088 bit of common semantics that is best described by using the word
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2089 object.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2090
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2091 Objects have individuality, and multiple names (in multiple scopes)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2092 can be bound to the same object. This is known as aliasing in other
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2093 languages. This is usually not appreciated on a first glance at
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2094 Python, and can be safely ignored when dealing with immutable basic
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2095 types (numbers, strings, tuples). However, aliasing has an
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2096 (intended!) effect on the semantics of Python code involving mutable
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2097 objects such as lists, dictionaries, and most types representing
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2098 entities outside the program (files, windows, etc.). This is usually
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2099 used to the benefit of the program, since aliases behave like pointers
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2100 in some respects. For example, passing an object is cheap since only
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2101 a pointer is passed by the implementation; and if a function modifies
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2102 an object passed as an argument, the caller will see the change --- this
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2103 obviates the need for two different argument passing mechanisms as in
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2104 Pascal.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2105
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2106
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2107 \section{Python scopes and name spaces}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2108
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2109 Before introducing classes, I first have to tell you something about
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2110 Python's scope rules. Class definitions play some neat tricks with
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2111 name spaces, and you need to know how scopes and name spaces work to
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2112 fully understand what's going on. Incidentally, knowledge about this
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2113 subject is useful for any advanced Python programmer.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2114
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2115 Let's begin with some definitions.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2116
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2117 A {\em name space} is a mapping from names to objects. Most name
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2118 spaces are currently implemented as Python dictionaries, but that's
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2119 normally not noticeable in any way (except for performance), and it
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2120 may change in the future. Examples of name spaces are: the set of
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2121 built-in names (functions such as \verb\abs()\, and built-in exception
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2122 names); the global names in a module; and the local names in a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2123 function invocation. In a sense the set of attributes of an object
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
2124 also form a name space. The important thing to know about name
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2125 spaces is that there is absolutely no relation between names in
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2126 different name spaces; for instance, two different modules may both
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2127 define a function ``maximize'' without confusion --- users of the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2128 modules must prefix it with the module name.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2129
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2130 By the way, I use the word {\em attribute} for any name following a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2131 dot --- for example, in the expression \verb\z.real\, \verb\real\ is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2132 an attribute of the object \verb\z\. Strictly speaking, references to
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2133 names in modules are attribute references: in the expression
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2134 \verb\modname.funcname\, \verb\modname\ is a module object and
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2135 \verb\funcname\ is an attribute of it. In this case there happens to
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2136 be a straightforward mapping between the module's attributes and the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2137 global names defined in the module: they share the same name space!%
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2138 \footnote{
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2139 Except for one thing. Module objects have a secret read-only
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2140 attribute called {\tt __dict__} which returns the dictionary
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2141 used to implement the module's name space; the name
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2142 {\tt __dict__} is an attribute but not a global name.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2143 Obviously, using this violates the abstraction of name space
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2144 implementation, and should be restricted to things like
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2145 post-mortem debuggers...
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2146 }
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2147
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2148 Attributes may be read-only or writable. In the latter case,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2149 assignment to attributes is possible. Module attributes are writable:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2150 you can write \verb\modname.the_answer = 42\. Writable attributes may
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2151 also be deleted with the del statement, e.g.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2152 \verb\del modname.the_answer\.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2153
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2154 Name spaces are created at different moments and have different
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2155 lifetimes. The name space containing the built-in names is created
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2156 when the Python interpreter starts up, and is never deleted. The
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2157 global name space for a module is created when the module definition
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2158 is read in; normally, module name spaces also last until the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2159 interpreter quits. The statements executed by the top-level
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2160 invocation of the interpreter, either read from a script file or
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2161 interactively, are considered part of a module called \verb\__main__\,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2162 so they have their own global name space. (The built-in names
1374
89d7d5e136ad * lib3.tex (module string): added rindex().
Guido van Rossum <guido@python.org>
parents: 1283
diff changeset
2163 actually also live in a module; this is called \verb\__builtin__\.)
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2164
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2165 The local name space for a function is created when the function is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2166 called, and deleted when the function returns or raises an exception
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2167 that is not handled within the function. (Actually, forgetting would
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2168 be a better way to describe what actually happens.) Of course,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2169 recursive invocations each have their own local name space.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2170
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2171 A {\em scope} is a textual region of a Python program where a name space
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2172 is directly accessible. ``Directly accessible'' here means that an
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2173 unqualified reference to a name attempts to find the name in the name
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2174 space.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2175
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2176 Although scopes are determined statically, they are used dynamically.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2177 At any time during execution, exactly three nested scopes are in use
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2178 (i.e., exactly three name spaces are directly accessible): the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2179 innermost scope, which is searched first, contains the local names,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2180 the middle scope, searched next, contains the current module's global
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2181 names, and the outermost scope (searched last) is the name space
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2182 containing built-in names.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2183
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2184 Usually, the local scope references the local names of the (textually)
2647
9b7e6d9a528d typos, layout and other small things
Guido van Rossum <guido@python.org>
parents: 2553
diff changeset
2185 current function. Outside of functions, the local scope references
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2186 the same name space as the global scope: the module's name space.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2187 Class definitions place yet another name space in the local scope.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2188
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2189 It is important to realize that scopes are determined textually: the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2190 global scope of a function defined in a module is that module's name
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2191 space, no matter from where or by what alias the function is called.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2192 On the other hand, the actual search for names is done dynamically, at
2647
9b7e6d9a528d typos, layout and other small things
Guido van Rossum <guido@python.org>
parents: 2553
diff changeset
2193 run time --- however, the language definition is evolving towards
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2194 static name resolution, at ``compile'' time, so don't rely on dynamic
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2195 name resolution! (In fact, local variables are already determined
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2196 statically.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2197
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2198 A special quirk of Python is that assignments always go into the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2199 innermost scope. Assignments do not copy data --- they just
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2200 bind names to objects. The same is true for deletions: the statement
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2201 \verb\del x\ removes the binding of x from the name space referenced by the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2202 local scope. In fact, all operations that introduce new names use the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2203 local scope: in particular, import statements and function definitions
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2204 bind the module or function name in the local scope. (The
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2205 \verb\global\ statement can be used to indicate that particular
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2206 variables live in the global scope.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2207
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2208
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2209 \section{A first look at classes}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2210
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2211 Classes introduce a little bit of new syntax, three new object types,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2212 and some new semantics.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2213
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2214
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2215 \subsection{Class definition syntax}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2216
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2217 The simplest form of class definition looks like this:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2218
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2219 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2220 class ClassName:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2221 <statement-1>
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2222 .
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2223 .
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2224 .
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2225 <statement-N>
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2226 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2227
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2228 Class definitions, like function definitions (\verb\def\ statements)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2229 must be executed before they have any effect. (You could conceivably
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2230 place a class definition in a branch of an \verb\if\ statement, or
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2231 inside a function.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2232
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2233 In practice, the statements inside a class definition will usually be
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2234 function definitions, but other statements are allowed, and sometimes
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2235 useful --- we'll come back to this later. The function definitions
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2236 inside a class normally have a peculiar form of argument list,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2237 dictated by the calling conventions for methods --- again, this is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2238 explained later.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2239
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2240 When a class definition is entered, a new name space is created, and
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2241 used as the local scope --- thus, all assignments to local variables
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2242 go into this new name space. In particular, function definitions bind
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2243 the name of the new function here.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2244
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2245 When a class definition is left normally (via the end), a {\em class
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2246 object} is created. This is basically a wrapper around the contents
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2247 of the name space created by the class definition; we'll learn more
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2248 about class objects in the next section. The original local scope
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2249 (the one in effect just before the class definitions was entered) is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2250 reinstated, and the class object is bound here to class name given in
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2251 the class definition header (ClassName in the example).
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2252
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2253
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2254 \subsection{Class objects}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2255
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2256 Class objects support two kinds of operations: attribute references
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2257 and instantiation.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2258
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2259 {\em Attribute references} use the standard syntax used for all
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2260 attribute references in Python: \verb\obj.name\. Valid attribute
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2261 names are all the names that were in the class's name space when the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2262 class object was created. So, if the class definition looked like
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2263 this:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2264
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2265 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2266 class MyClass:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2267 i = 12345
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2268 def f(x):
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2269 return 'hello world'
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2270 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2271
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2272 then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2273 references, returning an integer and a function object, respectively.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2274 Class attributes can also be assigned to, so you can change the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2275 value of \verb\MyClass.i\ by assignment.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2276
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2277 Class {\em instantiation} uses function notation. Just pretend that
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2278 the class object is a parameterless function that returns a new
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2279 instance of the class. For example, (assuming the above class):
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2280
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2281 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2282 x = MyClass()
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2283 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2284
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2285 creates a new {\em instance} of the class and assigns this object to
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2286 the local variable \verb\x\.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2287
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2288
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2289 \subsection{Instance objects}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2290
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2291 Now what can we do with instance objects? The only operations
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2292 understood by instance objects are attribute references. There are
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2293 two kinds of valid attribute names.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2294
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2295 The first I'll call {\em data attributes}. These correspond to
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2296 ``instance variables'' in Smalltalk, and to ``data members'' in \Cpp{}.
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2297 Data attributes need not be declared; like local variables, they
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2298 spring into existence when they are first assigned to. For example,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2299 if \verb\x\ in the instance of \verb\MyClass\ created above, the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2300 following piece of code will print the value 16, without leaving a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2301 trace:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2302
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2303 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2304 x.counter = 1
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2305 while x.counter < 10:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2306 x.counter = x.counter * 2
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2307 print x.counter
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2308 del x.counter
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2309 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2310
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2311 The second kind of attribute references understood by instance objects
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2312 are {\em methods}. A method is a function that ``belongs to'' an
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2313 object. (In Python, the term method is not unique to class instances:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2314 other object types can have methods as well, e.g., list objects have
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2315 methods called append, insert, remove, sort, and so on. However,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2316 below, we'll use the term method exclusively to mean methods of class
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2317 instance objects, unless explicitly stated otherwise.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2318
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2319 Valid method names of an instance object depend on its class. By
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2320 definition, all attributes of a class that are (user-defined) function
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2321 objects define corresponding methods of its instances. So in our
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2322 example, \verb\x.f\ is a valid method reference, since
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2323 \verb\MyClass.f\ is a function, but \verb\x.i\ is not, since
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2324 \verb\MyClass.i\ is not. But \verb\x.f\ is not the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2325 same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2326 function object.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2327
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2328
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2329 \subsection{Method objects}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2330
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2331 Usually, a method is called immediately, e.g.:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2332
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2333 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2334 x.f()
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2335 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2336
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2337 In our example, this will return the string \verb\'hello world'\.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2338 However, it is not necessary to call a method right away: \verb\x.f\
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2339 is a method object, and can be stored away and called at a later
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2340 moment, for example:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2341
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2342 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2343 xf = x.f
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2344 while 1:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2345 print xf()
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2346 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2347
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2348 will continue to print \verb\hello world\ until the end of time.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2349
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2350 What exactly happens when a method is called? You may have noticed
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2351 that \verb\x.f()\ was called without an argument above, even though
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2352 the function definition for \verb\f\ specified an argument. What
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2353 happened to the argument? Surely Python raises an exception when a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2354 function that requires an argument is called without any --- even if
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2355 the argument isn't actually used...
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2356
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2357 Actually, you may have guessed the answer: the special thing about
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2358 methods is that the object is passed as the first argument of the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2359 function. In our example, the call \verb\x.f()\ is exactly equivalent
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2360 to \verb\MyClass.f(x)\. In general, calling a method with a list of
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2361 {\em n} arguments is equivalent to calling the corresponding function
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2362 with an argument list that is created by inserting the method's object
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2363 before the first argument.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2364
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2365 If you still don't understand how methods work, a look at the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2366 implementation can perhaps clarify matters. When an instance
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2367 attribute is referenced that isn't a data attribute, its class is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2368 searched. If the name denotes a valid class attribute that is a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2369 function object, a method object is created by packing (pointers to)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2370 the instance object and the function object just found together in an
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2371 abstract object: this is the method object. When the method object is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2372 called with an argument list, it is unpacked again, a new argument
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2373 list is constructed from the instance object and the original argument
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2374 list, and the function object is called with this new argument list.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2375
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2376
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2377 \section{Random remarks}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2378
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2379
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2380 [These should perhaps be placed more carefully...]
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2381
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2382
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2383 Data attributes override method attributes with the same name; to
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2384 avoid accidental name conflicts, which may cause hard-to-find bugs in
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2385 large programs, it is wise to use some kind of convention that
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2386 minimizes the chance of conflicts, e.g., capitalize method names,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2387 prefix data attribute names with a small unique string (perhaps just
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2388 an underscore), or use verbs for methods and nouns for data attributes.
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2389
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2390
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2391 Data attributes may be referenced by methods as well as by ordinary
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2392 users (``clients'') of an object. In other words, classes are not
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2393 usable to implement pure abstract data types. In fact, nothing in
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2394 Python makes it possible to enforce data hiding --- it is all based
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2395 upon convention. (On the other hand, the Python implementation,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2396 written in C, can completely hide implementation details and control
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2397 access to an object if necessary; this can be used by extensions to
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2398 Python written in C.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2399
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2400
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2401 Clients should use data attributes with care --- clients may mess up
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2402 invariants maintained by the methods by stamping on their data
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2403 attributes. Note that clients may add data attributes of their own to
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2404 an instance object without affecting the validity of the methods, as
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2405 long as name conflicts are avoided --- again, a naming convention can
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2406 save a lot of headaches here.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2407
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2408
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2409 There is no shorthand for referencing data attributes (or other
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2410 methods!) from within methods. I find that this actually increases
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2411 the readability of methods: there is no chance of confusing local
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2412 variables and instance variables when glancing through a method.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2413
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2414
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2415 Conventionally, the first argument of methods is often called
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2416 \verb\self\. This is nothing more than a convention: the name
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2417 \verb\self\ has absolutely no special meaning to Python. (Note,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2418 however, that by not following the convention your code may be less
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2419 readable by other Python programmers, and it is also conceivable that
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2420 a {\em class browser} program be written which relies upon such a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2421 convention.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2422
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2423
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2424 Any function object that is a class attribute defines a method for
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2425 instances of that class. It is not necessary that the function
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2426 definition is textually enclosed in the class definition: assigning a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2427 function object to a local variable in the class is also ok. For
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2428 example:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2429
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2430 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2431 # Function defined outside the class
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2432 def f1(self, x, y):
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2433 return min(x, x+y)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2434
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2435 class C:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2436 f = f1
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2437 def g(self):
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2438 return 'hello world'
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2439 h = g
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2440 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2441
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2442 Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2443 \verb\C\ that refer to function objects, and consequently they are all
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2444 methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2445 to \verb\g\. Note that this practice usually only serves to confuse
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2446 the reader of a program.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2447
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2448
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2449 Methods may call other methods by using method attributes of the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2450 \verb\self\ argument, e.g.:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2451
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2452 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2453 class Bag:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2454 def empty(self):
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2455 self.data = []
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2456 def add(self, x):
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2457 self.data.append(x)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2458 def addtwice(self, x):
952
7d8e51e580d1 Fixed botched lay-out in class example
Guido van Rossum <guido@python.org>
parents: 933
diff changeset
2459 self.add(x)
7d8e51e580d1 Fixed botched lay-out in class example
Guido van Rossum <guido@python.org>
parents: 933
diff changeset
2460 self.add(x)
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2461 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2462
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2463
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2464 The instantiation operation (``calling'' a class object) creates an
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2465 empty object. Many classes like to create objects in a known initial
1935
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
2466 state. Therefore a class may define a special method named
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
2467 \verb\__init__\, like this:
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2468
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2469 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2470 def __init__(self):
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2471 self.empty()
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2472 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2473
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2474 When a class defines an \verb\__init__\ method, class instantiation
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2475 automatically invokes \verb\__init__\ for the newly-created class
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2476 instance. So in the \verb\Bag\ example, a new and initialized instance
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2477 can be obtained by:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2478
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2479 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2480 x = Bag()
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2481 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2482
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2483 Of course, the \verb\__init__\ method may have arguments for greater
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2484 flexibility. In that case, arguments given to the class instantiation
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2485 operator are passed on to \verb\__init__\. For example,
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2486
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2487 \bcode\begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2488 >>> class Complex:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2489 ... def __init__(self, realpart, imagpart):
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2490 ... self.r = realpart
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2491 ... self.i = imagpart
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2492 ...
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2493 >>> x = Complex(3.0,-4.5)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2494 >>> x.r, x.i
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2495 (3.0, -4.5)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2496 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2497 \end{verbatim}\ecode
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2498 %
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2499 Methods may reference global names in the same way as ordinary
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2500 functions. The global scope associated with a method is the module
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2501 containing the class definition. (The class itself is never used as a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2502 global scope!) While one rarely encounters a good reason for using
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2503 global data in a method, there are many legitimate uses of the global
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2504 scope: for one thing, functions and modules imported into the global
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2505 scope can be used by methods, as well as functions and classes defined
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2506 in it. Usually, the class containing the method is itself defined in
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2507 this global scope, and in the next section we'll find some good
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2508 reasons why a method would want to reference its own class!
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2509
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2510
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2511 \section{Inheritance}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2512
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2513 Of course, a language feature would not be worthy of the name ``class''
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2514 without supporting inheritance. The syntax for a derived class
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2515 definition looks as follows:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2516
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2517 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2518 class DerivedClassName(BaseClassName):
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2519 <statement-1>
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2520 .
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2521 .
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2522 .
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2523 <statement-N>
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2524 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2525
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2526 The name \verb\BaseClassName\ must be defined in a scope containing
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2527 the derived class definition. Instead of a base class name, an
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2528 expression is also allowed. This is useful when the base class is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2529 defined in another module, e.g.,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2530
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2531 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2532 class DerivedClassName(modname.BaseClassName):
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2533 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2534
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2535 Execution of a derived class definition proceeds the same as for a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2536 base class. When the class object is constructed, the base class is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2537 remembered. This is used for resolving attribute references: if a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2538 requested attribute is not found in the class, it is searched in the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2539 base class. This rule is applied recursively if the base class itself
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2540 is derived from some other class.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2541
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2542 There's nothing special about instantiation of derived classes:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2543 \verb\DerivedClassName()\ creates a new instance of the class. Method
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2544 references are resolved as follows: the corresponding class attribute
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2545 is searched, descending down the chain of base classes if necessary,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2546 and the method reference is valid if this yields a function object.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2547
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2548 Derived classes may override methods of their base classes. Because
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2549 methods have no special privileges when calling other methods of the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2550 same object, a method of a base class that calls another method
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2551 defined in the same base class, may in fact end up calling a method of
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2552 a derived class that overrides it. (For \Cpp{} programmers: all methods
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2553 in Python are ``virtual functions''.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2554
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2555 An overriding method in a derived class may in fact want to extend
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2556 rather than simply replace the base class method of the same name.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2557 There is a simple way to call the base class method directly: just
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2558 call \verb\BaseClassName.methodname(self, arguments)\. This is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2559 occasionally useful to clients as well. (Note that this only works if
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2560 the base class is defined or imported directly in the global scope.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2561
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2562
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2563 \subsection{Multiple inheritance}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2564
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2565 Python supports a limited form of multiple inheritance as well. A
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2566 class definition with multiple base classes looks as follows:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2567
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2568 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2569 class DerivedClassName(Base1, Base2, Base3):
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2570 <statement-1>
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2571 .
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2572 .
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2573 .
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2574 <statement-N>
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2575 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2576
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2577 The only rule necessary to explain the semantics is the resolution
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2578 rule used for class attribute references. This is depth-first,
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2579 left-to-right. Thus, if an attribute is not found in
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2580 \verb\DerivedClassName\, it is searched in \verb\Base1\, then
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2581 (recursively) in the base classes of \verb\Base1\, and only if it is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2582 not found there, it is searched in \verb\Base2\, and so on.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2583
1100
ff1899dcda05 Incorporated Jan-Hein's changes and texinfo conversion.
Guido van Rossum <guido@python.org>
parents: 1040
diff changeset
2584 (To some people breadth first---searching \verb\Base2\ and
ff1899dcda05 Incorporated Jan-Hein's changes and texinfo conversion.
Guido van Rossum <guido@python.org>
parents: 1040
diff changeset
2585 \verb\Base3\ before the base classes of \verb\Base1\---looks more
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2586 natural. However, this would require you to know whether a particular
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2587 attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2588 one of its base classes before you can figure out the consequences of
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2589 a name conflict with an attribute of \verb\Base2\. The depth-first
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2590 rule makes no differences between direct and inherited attributes of
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2591 \verb\Base1\.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2592
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2593 It is clear that indiscriminate use of multiple inheritance is a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2594 maintenance nightmare, given the reliance in Python on conventions to
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2595 avoid accidental name conflicts. A well-known problem with multiple
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2596 inheritance is a class derived from two classes that happen to have a
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2597 common base class. While it is easy enough to figure out what happens
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2598 in this case (the instance will have a single copy of ``instance
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2599 variables'' or data attributes used by the common base class), it is
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2600 not clear that these semantics are in any way useful.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2601
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2602
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2603 \section{Odds and ends}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2604
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2605 Sometimes it is useful to have a data type similar to the Pascal
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2606 ``record'' or C ``struct'', bundling together a couple of named data
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2607 items. An empty class definition will do nicely, e.g.:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2608
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2609 \begin{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2610 class Employee:
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2611 pass
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2612
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2613 john = Employee() # Create an empty employee record
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2614
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2615 # Fill the fields of the record
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2616 john.name = 'John Doe'
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2617 john.dept = 'computer lab'
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2618 john.salary = 1000
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2619 \end{verbatim}
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2620
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2621
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2622 A piece of Python code that expects a particular abstract data type
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2623 can often be passed a class that emulates the methods of that data
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2624 type instead. For instance, if you have a function that formats some
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2625 data from a file object, you can define a class with methods
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2626 \verb\read()\ and \verb\readline()\ that gets the data from a string
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2627 buffer instead, and pass it as an argument. (Unfortunately, this
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2628 technique has its limitations: a class can't define operations that
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2629 are accessed by special syntax such as sequence subscripting or
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2630 arithmetic operators, and assigning such a ``pseudo-file'' to
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2631 \verb\sys.stdin\ will not cause the interpreter to read further input
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2632 from it.)
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2633
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2634
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2635 Instance method objects have attributes, too: \verb\m.im_self\ is the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2636 object of which the method is an instance, and \verb\m.im_func\ is the
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2637 function object corresponding to the method.
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2638
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
2639
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
2640 \chapter{Recent Additions as of Release 1.1}
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2641
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2642 Python is an evolving language. Since this tutorial was last
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2643 thoroughly revised, several new features have been added to the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2644 language. While ideally I should revise the tutorial to incorporate
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2645 them in the mainline of the text, lack of time currently requires me
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
2646 to take a more modest approach. In this chapter I will briefly list the
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2647 most important improvements to the language and how you can use them
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2648 to your benefit.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2649
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2650 \section{The Last Printed Expression}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2651
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2652 In interactive mode, the last printed expression is assigned to the
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2653 variable \code{_}. This means that when you are using Python as a
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2654 desk calculator, it is somewhat easier to continue calculations, for
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2655 example:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2656
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2657 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2658 >>> tax = 17.5 / 100
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2659 >>> price = 3.50
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2660 >>> price * tax
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2661 0.6125
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2662 >>> price + _
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2663 4.1125
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2664 >>> round(_, 2)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2665 4.11
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2666 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2667 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2668
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2669 For reasons too embarrassing to explain, this variable is implemented
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2670 as a built-in (living in the module \code{__builtin__}), so it should
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2671 be treated as read-only by the user. I.e. don't explicitly assign a
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2672 value to it --- you would create an independent local variable with
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2673 the same name masking the built-in variable with its magic behavior.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2674
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2675 \section{String Literals}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2676
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2677 \subsection{Double Quotes}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2678
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2679 Python can now also use double quotes to surround string literals,
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2680 e.g. \verb\"this doesn't hurt a bit"\. There is no semantic
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2681 difference between strings surrounded by single or double quotes.
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2682
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2683 \subsection{Continuation Of String Literals}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2684
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2685 String literals can span multiple lines by escaping newlines with
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2686 backslashes, e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2687
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2688 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2689 hello = "This is a rather long string containing\n\
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2690 several lines of text just as you would do in C.\n\
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2691 Note that whitespace at the beginning of the line is\
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2692 significant.\n"
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2693 print hello
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2694 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2695
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2696 which would print the following:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2697 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2698 This is a rather long string containing
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2699 several lines of text just as you would do in C.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2700 Note that whitespace at the beginning of the line is significant.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2701 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2702
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2703 \subsection{Triple-quoted strings}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2704
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2705 In some cases, when you need to include really long strings (e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2706 containing several paragraphs of informational text), it is annoying
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2707 that you have to terminate each line with \verb@\n\@, especially if
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2708 you would like to reformat the text occasionally with a powerful text
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2709 editor like Emacs. For such situations, ``triple-quoted'' strings can
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2710 be used, e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2711
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2712 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2713 hello = """
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2714
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2715 This string is bounded by triple double quotes (3 times ").
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2716 Unescaped newlines in the string are retained, though \
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2717 it is still possible\nto use all normal escape sequences.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2718
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2719 Whitespace at the beginning of a line is
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2720 significant. If you need to include three opening quotes
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2721 you have to escape at least one of them, e.g. \""".
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2722
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2723 This string ends in a newline.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2724 """
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2725 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2726
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2727 Triple-quoted strings can be surrounded by three single quotes as
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2728 well, again without semantic difference.
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2729
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2730 \subsection{String Literal Juxtaposition}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2731
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2732 One final twist: you can juxtapose multiple string literals. Two or
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2733 more adjacent string literals (but not arbitrary expressions!)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2734 separated only by whitespace will be concatenated (without intervening
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2735 whitespace) into a single string object at compile time. This makes
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2736 it possible to continue a long string on the next line without
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2737 sacrificing indentation or performance, unlike the use of the string
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2738 concatenation operator \verb\+\ or the continuation of the literal
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2739 itself on the next line (since leading whitespace is significant
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2740 inside all types of string literals). Note that this feature, like
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2741 all string features except triple-quoted strings, is borrowed from
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2742 Standard C.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2743
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2744 \section{The Formatting Operator}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2745
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2746 \subsection{Basic Usage}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2747
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2748 The chapter on output formatting is really out of date: there is now
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2749 an almost complete interface to C-style printf formats. This is done
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2750 by overloading the modulo operator (\verb\%\) for a left operand
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2751 which is a string, e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2752
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2753 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2754 >>> import math
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2755 >>> print 'The value of PI is approximately %5.3f.' % math.pi
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2756 The value of PI is approximately 3.142.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2757 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2758 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2759
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2760 If there is more than one format in the string you pass a tuple as
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2761 right operand, e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2762
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2763 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2764 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2765 >>> for name, phone in table.items():
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2766 ... print '%-10s ==> %10d' % (name, phone)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2767 ...
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2768 Jack ==> 4098
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2769 Dcab ==> 8637678
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2770 Sjoerd ==> 4127
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2771 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2772 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2773
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2774 Most formats work exactly as in C and require that you pass the proper
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2775 type (however, if you don't you get an exception, not a core dump).
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2776 The \verb\%s\ format is more relaxed: if the corresponding argument is
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2777 not a string object, it is converted to string using the \verb\str()\
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2778 built-in function. Using \verb\*\ to pass the width or precision in
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2779 as a separate (integer) argument is supported. The C formats
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2780 \verb\%n\ and \verb\%p\ are not supported.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2781
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2782 \subsection{Referencing Variables By Name}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2783
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2784 If you have a really long format string that you don't want to split
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2785 up, it would be nice if you could reference the variables to be
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2786 formatted by name instead of by position. This can be done by using
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2787 an extension of C formats using the form \verb\%(name)format\, e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2788
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2789 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2790 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2791 >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2792 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2793 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2794 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2795
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2796 This is particularly useful in combination with the new built-in
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2797 \verb\vars()\ function, which returns a dictionary containing all
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2798 local variables.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2799
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2800 \section{Optional Function Arguments}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2801
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2802 It is now possible to define functions with a variable number of
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2803 arguments. There are two forms, which can be combined.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2804
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2805 \subsection{Default Argument Values}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2806
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2807 The most useful form is to specify a default value for one or more
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2808 arguments. This creates a function that can be called with fewer
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2809 arguments than it is defined, e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2810
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2811 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2812 def ask_ok(prompt, retries = 4, complaint = 'Yes or no, please!'):
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2813 while 1:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2814 ok = raw_input(prompt)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2815 if ok in ('y', 'ye', 'yes'): return 1
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2816 if ok in ('n', 'no', 'nop', 'nope'): return 0
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2817 retries = retries - 1
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2818 if retries < 0: raise IOError, 'refusenik user'
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2819 print complaint
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2820 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2821
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2822 This function can be called either like this:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2823 \verb\ask_ok('Do you really want to quit?')\ or like this:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2824 \verb\ask_ok('OK to overwrite the file?', 2)\.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2825
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2826 The default values are evaluated at the point of function definition
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2827 in the {\em defining} scope, so that e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2828
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2829 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2830 i = 5
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2831 def f(arg = i): print arg
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2832 i = 6
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2833 f()
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2834 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2835
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2836 will print \verb\5\.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2837
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2838 \subsection{Arbitrary Argument Lists}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2839
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2840 It is also possible to specify that a function can be called with an
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2841 arbitrary number of arguments. These arguments will be wrapped up in
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2842 a tuple. Before the variable number of arguments, zero or more normal
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2843 arguments may occur, e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2844
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2845 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2846 def fprintf(file, format, *args):
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2847 file.write(format % args)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2848 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2849
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2850 This feature may be combined with the previous, e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2851
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2852 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2853 def but_is_it_useful(required, optional = None, *remains):
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2854 print "I don't know"
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2855 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2856
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2857 \section{Lambda And Functional Programming Tools}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2858
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2859 \subsection{Lambda Forms}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2860
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1751
diff changeset
2861 By popular demand, a few features commonly found in functional
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2862 programming languages and Lisp have been added to Python. With the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2863 \verb\lambda\ keyword, small anonymous functions can be created.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2864 Here's a function that returns the sum of its two arguments:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2865 \verb\lambda a, b: a+b\. Lambda forms can be used wherever function
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2866 objects are required. They are syntactically restricted to a single
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2867 expression. Semantically, they are just syntactic sugar for a normal
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2868 function definition. Like nested function definitions, lambda forms
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2869 cannot reference variables from the containing scope, but this can be
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2870 overcome through the judicious use of default argument values, e.g.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2871
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2872 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2873 def make_incrementor(n):
1852
09de723b2596 * Doc/tut.tex (subsection{Lambda Forms}): fix typo in lambda
Guido van Rossum <guido@python.org>
parents: 1767
diff changeset
2874 return lambda x, incr=n: x+incr
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2875 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2876
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2877 \subsection{Map, Reduce and Filter}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2878
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2879 Three new built-in functions on sequences are good candidate to pass
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2880 lambda forms.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2881
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2882 \subsubsection{Map.}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2883
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2884 \verb\map(function, sequence)\ calls \verb\function(item)\ for each of
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2885 the sequence's items and returns a list of the return values. For
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2886 example, to compute some cubes:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2887
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2888 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2889 >>> map(lambda x: x*x*x, range(1, 11))
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2890 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2891 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2892 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2893
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2894 More than one sequence may be passed; the function must then have as
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2895 many arguments as there are sequences and is called with the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2896 corresponding item from each sequence (or \verb\None\ if some sequence
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2897 is shorter than another). If \verb\None\ is passed for the function,
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2898 a function returning its argument(s) is substituted.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2899
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2900 Combining these two special cases, we see that
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2901 \verb\map(None, list1, list2)\ is a convenient way of turning a pair
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2902 of lists into a list of pairs. For example:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2903
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2904 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2905 >>> seq = range(8)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2906 >>> map(None, seq, map(lambda x: x*x, seq))
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2907 [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2908 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2909 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2910
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2911 \subsubsection{Filter.}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2912
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2913 \verb\filter(function, sequence)\ returns a sequence (of the same
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2914 type, if possible) consisting of those items from the sequence for
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2915 which \verb\function(item)\ is true. For example, to compute some
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2916 primes:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2917
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2918 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2919 >>> filter(lambda x: x%2 != 0 and x%3 != 0, range(2, 25))
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2920 [5, 7, 11, 13, 17, 19, 23]
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2921 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2922 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2923
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2924 \subsubsection{Reduce.}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2925
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2926 \verb\reduce(function, sequence)\ returns a single value constructed
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2927 by calling the (binary) function on the first two items of the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2928 sequence, then on the result and the next item, and so on. For
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2929 example, to compute the sum of the numbers 1 through 10:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2930
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2931 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2932 >>> reduce(lambda x, y: x+y, range(1, 11))
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2933 55
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2934 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2935 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2936
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2937 If there's only one item in the sequence, its value is returned; if
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2938 the sequence is empty, an exception is raised.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2939
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2940 A third argument can be passed to indicate the starting value. In this
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2941 case the starting value is returned for an empty sequence, and the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2942 function is first applied to the starting value and the first sequence
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2943 item, then to the result and the next item, and so on. For example,
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2944
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2945 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2946 >>> def sum(seq):
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2947 ... return reduce(lambda x, y: x+y, seq, 0)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2948 ...
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2949 >>> sum(range(1, 11))
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2950 55
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2951 >>> sum([])
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2952 0
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2953 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2954 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2955
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2956 \section{Continuation Lines Without Backslashes}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2957
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2958 While the general mechanism for continuation of a source line on the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2959 next physical line remains to place a backslash on the end of the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2960 line, expressions inside matched parentheses (or square brackets, or
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2961 curly braces) can now also be continued without using a backslash.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2962 This is particularly useful for calls to functions with many
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2963 arguments, and for initializations of large tables.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2964
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2965 For example:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2966
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2967 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2968 month_names = ['Januari', 'Februari', 'Maart',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2969 'April', 'Mei', 'Juni',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2970 'Juli', 'Augustus', 'September',
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2971 'Oktober', 'November', 'December']
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2972 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2973
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2974 and
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2975
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2976 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2977 CopyInternalHyperLinks(self.context.hyperlinks,
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2978 copy.context.hyperlinks,
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2979 uidremap)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2980 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2981
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2982 \section{Regular Expressions}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2983
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2984 While C's printf-style output formats, transformed into Python, are
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2985 adequate for most output formatting jobs, C's scanf-style input
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2986 formats are not very powerful. Instead of scanf-style input, Python
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2987 offers Emacs-style regular expressions as a powerful input and
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2988 scanning mechanism. Read the corresponding section in the Library
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2989 Reference for a full description.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2990
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2991 \section{Generalized Dictionaries}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2992
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2352
diff changeset
2993 The keys of dictionaries are no longer restricted to strings --- they
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2994 can be any immutable basic type including strings, numbers, tuples, or
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2995 (certain) class instances. (Lists and dictionaries are not acceptable
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2996 as dictionary keys, in order to avoid problems when the object used as
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
2997 a key is modified.)
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2998
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
2999 Dictionaries have two new methods: \verb\d.values()\ returns a list of
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3000 the dictionary's values, and \verb\d.items()\ returns a list of the
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3001 dictionary's (key, value) pairs. Like \verb\d.keys()\, these
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3002 operations are slow for large dictionaries. Examples:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3003
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3004 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3005 >>> d = {100: 'honderd', 1000: 'duizend', 10: 'tien'}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3006 >>> d.keys()
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3007 [100, 10, 1000]
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3008 >>> d.values()
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3009 ['honderd', 'tien', 'duizend']
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3010 >>> d.items()
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3011 [(100, 'honderd'), (10, 'tien'), (1000, 'duizend')]
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3012 >>>
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3013 \end{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3014
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3015 \section{Miscellaneous New Built-in Functions}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3016
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3017 The function \verb\vars()\ returns a dictionary containing the current
1934
f3ae7c17ac08 Moved author and date to boilerplate.tex.
Guido van Rossum <guido@python.org>
parents: 1852
diff changeset
3018 local variables. With a module argument, it returns that module's
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3019 global variables. The old function \verb\dir(x)\ returns
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3020 \verb\vars(x).keys()\.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3021
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3022 The function \verb\round(x)\ returns a floating point number rounded
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3023 to the nearest integer (but still expressed as a floating point
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3024 number). E.g. \verb\round(3.4) == 3.0\ and \verb\round(3.5) == 4.0\.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3025 With a second argument it rounds to the specified number of digits,
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3026 e.g. \verb\round(math.pi, 4) == 3.1416\ or even
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3027 \verb\round(123.4, -2) == 100.0\.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3028
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3029 The function \verb\hash(x)\ returns a hash value for an object.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3030 All object types acceptable as dictionary keys have a hash value (and
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3031 it is this hash value that the dictionary implementation uses).
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3032
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3033 The function \verb\id(x)\ return a unique identifier for an object.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3034 For two objects x and y, \verb\id(x) == id(y)\ if and only if
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3035 \verb\x is y\. (In fact the object's address is used.)
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3036
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3037 The function \verb\hasattr(x, name)\ returns whether an object has an
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3038 attribute with the given name (a string value). The function
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3039 \verb\getattr(x, name)\ returns the object's attribute with the given
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3040 name. The function \verb\setattr(x, name, value)\ assigns a value to
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3041 an object's attribute with the given name. These three functions are
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3042 useful if the attribute names are not known beforehand. Note that
2063
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
3043 \verb\getattr(x, 'spam')\ is equivalent to \verb\x.spam\, and
5ed065943526 spam -> foo (etc.) in examples
Guido van Rossum <guido@python.org>
parents: 2013
diff changeset
3044 \verb\setattr(x, 'spam', y)\ is equivalent to \verb\x.spam = y\. By
1751
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3045 definition, \verb\hasattr(x, name)\ returns true if and only if
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3046 \verb\getattr(x, name)\ returns without raising an exception.
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3047
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3048 \section{Else Clause For Try Statement}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3049
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3050 The \verb\try...except\ statement now has an optional \verb\else\
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3051 clause, which must follow all \verb\except\ clauses. It is useful to
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3052 place code that must be executed if the \verb\try\ clause does not
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3053 raise an exception. For example:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3054
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3055 \begin{verbatim}
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3056 for arg in sys.argv:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3057 try:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3058 f = open(arg, 'r')
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3059 except IOError:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3060 print 'cannot open', arg
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3061 else:
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3062 print arg, 'has', len(f.readlines()), 'lines'
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3063 f.close()
ec25aad43724 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1426
diff changeset
3064 \end{verbatim}
929
ff02b948c9a8 Add chapter on classes (mostly from ../misc/CLASSES).
Guido van Rossum <guido@python.org>
parents: 808
diff changeset
3065
1935
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3066
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3067 \section{New Class Features in Release 1.1}
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3068
2013
Guido van Rossum <guido@python.org>
parents: 1947
diff changeset
3069 Some changes have been made to classes: the operator overloading
1935
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3070 mechanism is more flexible, providing more support for non-numeric use
1937
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3071 of operators (including calling an object as if it were a function),
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3072 and it is possible to trap attribute accesses.
1935
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3073
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3074 \subsection{New Operator Overloading}
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3075
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3076 It is no longer necessary to coerce both sides of an operator to the
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3077 same class or type. A class may still provide a \code{__coerce__}
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3078 method, but this method may return objects of different types or
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3079 classes if it feels like it. If no \code{__coerce__} is defined, any
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3080 argument type or class is acceptable.
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3081
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3082 In order to make it possible to implement binary operators where the
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3083 right-hand side is a class instance but the left-hand side is not,
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3084 without using coercions, right-hand versions of all binary operators
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3085 may be defined. These have an `r' prepended to their name,
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3086 e.g. \code{__radd__}.
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3087
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3088 For example, here's a very simple class for representing times. Times
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3089 are initialized from a number of seconds (like time.time()). Times
2553
f30bc48c417a versions and dates
Guido van Rossum <guido@python.org>
parents: 2550
diff changeset
3090 are printed like this: \code{Wed Mar 15 12:28:48 1995}. Subtracting
1935
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3091 two Times gives their difference in seconds. Adding or subtracting a
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3092 Time and a number gives a new Time. You can't add two times, nor can
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3093 you subtract a Time from a number.
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3094
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3095 \begin{verbatim}
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3096 import time
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3097
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3098 class Time:
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3099 def __init__(self, seconds):
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3100 self.seconds = seconds
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3101 def __repr__(self):
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3102 return time.ctime(self.seconds)
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3103 def __add__(self, x):
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3104 return Time(self.seconds + x)
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3105 __radd__ = __add__ # support for x+t
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3106 def __sub__(self, x):
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3107 if hasattr(x, 'seconds'): # test if x could be a Time
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3108 return self.seconds - x.seconds
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3109 else:
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3110 return self.seconds - x
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3111
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3112 now = Time(time.time())
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3113 tomorrow = 24*3600 + now
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3114 yesterday = now - today
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3115 print tomorrow - yesterday # prints 172800
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3116 \end{verbatim}
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3117
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3118 \subsection{Trapping Attribute Access}
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3119
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3120 You can define three new ``magic'' methods in a class now:
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3121 \code{__getattr__(self, name)}, \code{__setattr__(self, name, value)}
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3122 and \code{__delattr__(self, name)}.
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3123
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3124 The \code{__getattr__} method is called when an attribute access fails,
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2352
diff changeset
3125 i.e. when an attribute access would otherwise raise AttributeError ---
1935
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3126 this is {\em after} the instance's dictionary and its class hierarchy
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3127 have been searched for the named attribute. Note that if this method
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3128 attempts to access any undefined instance attribute it will be called
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3129 recursively!
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3130
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3131 The \code{__setattr__} and \code{__delattr__} methods are called when
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3132 assignment to, respectively deletion of an attribute are attempted.
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3133 They are called {\em instead} of the normal action (which is to insert
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3134 or delete the attribute in the instance dictionary). If either of
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3135 these methods most set or delete any attribute, they can only do so by
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2352
diff changeset
3136 using the instance dictionary directly --- \code{self.__dict__} --- else
1935
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3137 they would be called recursively.
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3138
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3139 For example, here's a near-universal ``Wrapper'' class that passes all
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3140 its attribute accesses to another object. Note how the
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3141 \code{__init__} method inserts the wrapped object in
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3142 \code{self.__dict__} in order to avoid endless recursion
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3143 (\code{__setattr__} would call \code{__getattr__} which would call
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3144 itself recursively).
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3145
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3146 \begin{verbatim}
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3147 class Wrapper:
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3148 def __init__(self, wrapped):
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3149 self.__dict__['wrapped'] = wrapped
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3150 def __getattr__(self, name):
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3151 return getattr(self.wrapped, name)
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3152 def __setattr__(self, name, value):
2013
Guido van Rossum <guido@python.org>
parents: 1947
diff changeset
3153 setattr(self.wrapped, name, value)
1935
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3154 def __delattr__(self, name):
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3155 delattr(self.wrapped, name)
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3156
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3157 import sys
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3158 f = Wrapper(sys.stdout)
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3159 f.write('hello world\n') # prints 'hello world'
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3160 \end{verbatim}
d9a3e30b2e47 Removed description of class.init() method.
Guido van Rossum <guido@python.org>
parents: 1934
diff changeset
3161
1937
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3162 A simpler example of \code{__getattr__} is an attribute that is
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3163 computed each time (or the first time) it it accessed. For instance:
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3164
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3165 \begin{verbatim}
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3166 from math import pi
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3167
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3168 class Circle:
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3169 def __init__(self, radius):
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3170 self.radius = radius
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3171 def __getattr__(self, name):
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3172 if name == 'circumference':
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3173 return 2 * pi * self.radius
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3174 if name == 'diameter':
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3175 return 2 * self.radius
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3176 if name == 'area':
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3177 return pi * pow(self.radius, 2)
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3178 raise AttributeError, name
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3179 \end{verbatim}
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3180
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3181 \subsection{Calling a Class Instance}
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3182
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3183 If a class defines a method \code{__call__} it is possible to call its
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3184 instances as if they were functions. For example:
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3185
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3186 \begin{verbatim}
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3187 class PresetSomeArguments:
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3188 def __init__(self, func, *args):
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3189 self.func, self.args = func, args
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3190 def __call__(self, *args):
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3191 return apply(self.func, self.args + args)
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3192
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3193 f = PresetSomeArguments(pow, 2) # f(i) computes powers of 2
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3194 for i in range(10): print f(i), # prints 1 2 4 8 16 32 64 128 256 512
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3195 print # append newline
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3196 \end{verbatim}
320baeab787a Add simpler __getattr__ example and document __call__
Guido van Rossum <guido@python.org>
parents: 1935
diff changeset
3197
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3198
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3199 \chapter{New in Release 1.2}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3200
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3201
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3202 This chapter describes even more recent additions to the Python
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3203 language and library.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3204
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3205
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3206 \section{New Class Features}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3207
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3208 The semantics of \code{__coerce__} have been changed to be more
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3209 reasonable. As an example, the new standard module \code{Complex}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3210 implements fairly complete complex numbers using this. Additional
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3211 examples of classes with and without \code{__coerce__} methods can be
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3212 found in the \code{Demo/classes} subdirectory, modules \code{Rat} and
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3213 \code{Dates}.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3214
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3215 If a class defines no \code{__coerce__} method, this is equivalent to
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3216 the following definition:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3217
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3218 \begin{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3219 def __coerce__(self, other): return self, other
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3220 \end{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3221
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3222 If \code{__coerce__} coerces itself to an object of a different type,
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3223 the operation is carried out using that type --- in release 1.1, this
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3224 would cause an error.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3225
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3226 Comparisons involving class instances now invoke \code{__coerce__}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3227 exactly as if \code{cmp(x, y)} were a binary operator like \code{+}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3228 (except if \code{x} and \code{y} are the same object).
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3229
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3230 \section{Unix Signal Handling}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3231
3659
825bd8c11b1c Note that EOF is ^Z on DOS. Spell Unix as \UNIX in some more places.
Guido van Rossum <guido@python.org>
parents: 3210
diff changeset
3232 On {\UNIX}, Python now supports signal handling. The module
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3233 \code{signal} exports functions \code{signal}, \code{pause} and
3659
825bd8c11b1c Note that EOF is ^Z on DOS. Spell Unix as \UNIX in some more places.
Guido van Rossum <guido@python.org>
parents: 3210
diff changeset
3234 \code{alarm}, which act similar to their {\UNIX} counterparts. The
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3235 module also exports the conventional names for the various signal
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3236 classes (also usable with \code{os.kill()}) and \code{SIG_IGN} and
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3237 \code{SIG_DFL}. See the section on \code{signal} in the Library
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3238 Reference Manual for more information.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3239
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3240 \section{Exceptions Can Be Classes}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3241
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3242 User-defined exceptions are no longer limited to being string objects
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3243 --- they can be identified by classes as well. Using this mechanism it
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3244 is possible to create extensible hierarchies of exceptions.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3245
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3246 There are two new valid (semantic) forms for the raise statement:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3247
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3248 \begin{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3249 raise Class, instance
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3250
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3251 raise instance
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3252 \end{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3253
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3254 In the first form, \code{instance} must be an instance of \code{Class}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3255 or of a class derived from it. The second form is a shorthand for
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3256
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3257 \begin{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3258 raise instance.__class__, instance
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3259 \end{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3260
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3261 An except clause may list classes as well as string objects. A class
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3262 in an except clause is compatible with an exception if it is the same
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3263 class or a base class thereof (but not the other way around --- an
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3264 except clause listing a derived class is not compatible with a base
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3265 class). For example, the following code will print B, C, D in that
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3266 order:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3267
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3268 \begin{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3269 class B:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3270 pass
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3271 class C(B):
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3272 pass
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3273 class D(C):
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3274 pass
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3275
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3276 for c in [B, C, D]:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3277 try:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3278 raise c()
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3279 except D:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3280 print "D"
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3281 except C:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3282 print "C"
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3283 except B:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3284 print "B"
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3285 \end{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3286
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3287 Note that if the except clauses were reversed (with ``\code{except B}''
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3288 first), it would have printed B, B, B --- the first matching except
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3289 clause is triggered.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3290
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3291 When an error message is printed for an unhandled exception which is a
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3292 class, the class name is printed, then a colon and a space, and
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3293 finally the instance converted to a string using the built-in function
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3294 \code{str()}.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3295
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3296 In this release, the built-in exceptions are still strings.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3297
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3298
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3299 \section{Object Persistency and Object Copying}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3300
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3301 Two new modules, \code{pickle} and \code{shelve}, support storage and
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3302 retrieval of (almost) arbitrary Python objects on disk, using the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3303 \code{dbm} package. A third module, \code{copy}, provides flexible
2352
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3304 object copying operations. More information on these modules is
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3305 provided in the Library Reference Manual.
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3306
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3307 \subsection{Persistent Objects}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3308
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3309 The module \code{pickle} provides a general framework for objects to
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3310 disassemble themselves into a stream of bytes and to reassemble such a
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3311 stream back into an object. It copes with reference sharing,
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3312 recursive objects and instances of user-defined classes, but not
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3313 (directly) with objects that have ``magical'' links into the operating
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3314 system such as open files, sockets or windows.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3315
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3316 The \code{pickle} module defines a simple protocol whereby
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3317 user-defined classes can control how they are disassembled and
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3318 assembled. The method \code{__getinitargs__()}, if defined, returns
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3319 the argument list for the constructor to be used at assembly time (by
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3320 default the constructor is called without arguments). The methods
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3321 \code{__getstate__()} and \code{__setstate__()} are used to pass
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3322 additional state from disassembly to assembly; by default the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3323 instance's \code{__dict__} is passed and restored.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3324
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3325 Note that \code{pickle} does not open or close any files --- it can be
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3326 used equally well for moving objects around on a network or store them
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3327 in a database. For ease of debugging, and the inevitable occasional
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3328 manual patch-up, the constructed byte streams consist of printable
2550
0f6ff45bd816 replace ASCII by macro call
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
3329 \ASCII{} characters only (though it's not designed to be pretty).
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3330
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3331 The module \code{shelve} provides a simple model for storing objects
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3332 on files. The operation \code{shelve.open(filename)} returns a
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3333 ``shelf'', which is a simple persistent database with a
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3334 dictionary-like interface. Database keys are strings, objects stored
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3335 in the database can be anything that \code{pickle} will handle.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3336
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3337 \subsection{Copying Objects}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3338
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3339 The module \code{copy} exports two functions: \code{copy()} and
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3340 \code{deepcopy()}. The \code{copy()} function returns a ``shallow''
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3341 copy of an object; \code{deepcopy()} returns a ``deep'' copy. The
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3342 difference between shallow and deep copying is only relevant for
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3343 compound objects (objects that contain other objects, like lists or
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3344 class instances):
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3345
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3346 \begin{itemize}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3347
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3348 \item
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3349 A shallow copy constructs a new compound object and then (to the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3350 extent possible) inserts {\em the same objects} into in that the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3351 original contains.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3352
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3353 \item
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3354 A deep copy constructs a new compound object and then, recursively,
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3355 inserts {\em copies} into it of the objects found in the original.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3356
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3357 \end{itemize}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3358
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3359 Both functions have the same restrictions and use the same protocols
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3360 as \code{pickle} --- user-defined classes can control how they are
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3361 copied by providing methods named \code{__getinitargs__()},
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3362 \code{__getstate__()} and \code{__setstate__()}.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3363
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3364
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3365 \section{Documentation Strings}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3366
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3367 A variety of objects now have a new attribute, \code{__doc__}, which
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3368 is supposed to contain a documentation string (if no documentation is
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3369 present, the attribute is \code{None}). New syntax, compatible with
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3370 the old interpreter, allows for convenient initialization of the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3371 \code{__doc__} attribute of modules, classes and functions by placing
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3372 a string literal by itself as the first statement in the suite. It
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3373 must be a literal --- an expression yielding a string object is not
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3374 accepted as a documentation string, since future tools may need to
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3375 derive documentation from source by parsing.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3376
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3377 Here is a hypothetical, amply documented module called \code{Spam}:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3378
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3379 \begin{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3380 """Spam operations.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3381
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3382 This module exports two classes, a function and an exception:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3383
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3384 class Spam: full Spam functionality --- three can sizes
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3385 class SpamLight: limited Spam functionality --- only one can size
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3386
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3387 def open(filename): open a file and return a corresponding Spam or
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3388 SpamLight object
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3389
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3390 GoneOff: exception raised for errors; should never happen
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3391
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3392 Note that it is always possible to convert a SpamLight object to a
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3393 Spam object by a simple method call, but that the reverse operation is
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3394 generally costly and may fail for a number of reasons.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3395 """
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3396
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3397 class SpamLight:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3398 """Limited spam functionality.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3399
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3400 Supports a single can size, no flavor, and only hard disks.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3401 """
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3402
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3403 def __init__(self, size=12):
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3404 """Construct a new SpamLight instance.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3405
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3406 Argument is the can size.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3407 """
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3408 # etc.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3409
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3410 # etc.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3411
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3412 class Spam(SpamLight):
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3413 """Full spam functionality.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3414
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3415 Supports three can sizes, two flavor varieties, and all floppy
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3416 disk formats still supported by current hardware.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3417 """
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3418
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3419 def __init__(self, size1=8, size2=12, size3=20):
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3420 """Construct a new Spam instance.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3421
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3422 Arguments are up to three can sizes.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3423 """
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3424 # etc.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3425
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3426 # etc.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3427
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3428 def open(filename = "/dev/null"):
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3429 """Open a can of Spam.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3430
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3431 Argument must be an existing file.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3432 """
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3433 # etc.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3434
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3435 class GoneOff:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3436 """Class used for Spam exceptions.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3437
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3438 There shouldn't be any.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3439 """
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3440 pass
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3441 \end{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3442
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3443 After executing ``\code{import Spam}'', the following expressions
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3444 return the various documentation strings from the module:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3445
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3446 \begin{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3447 Spam.__doc__
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3448 Spam.SpamLight.__doc__
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3449 Spam.SpamLight.__init__.__doc__
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3450 Spam.Spam.__doc__
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3451 Spam.Spam.__init__.__doc__
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3452 Spam.open.__doc__
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3453 Spam.GoneOff.__doc__
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3454 \end{verbatim}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3455
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3456 There are emerging conventions about the content and formatting of
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3457 documentation strings.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3458
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3459 The first line should always be a short, concise summary of the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3460 object's purpose. For brevity, it should not explicitly state the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3461 object's name or type, since these are available by other means
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3462 (except if the name happens to be a verb describing a function's
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3463 operation). This line should begin with a capital letter and end with
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3464 a period.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3465
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3466 If there are more lines in the documentation string, the second line
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3467 should be blank, visually separating the summary from the rest of the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3468 description. The following lines should be one of more of paragraphs
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3469 describing the objects calling conventions, its side effects, etc.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3470
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3471 Some people like to copy the Emacs convention of using UPPER CASE for
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3472 function parameters --- this often saves a few words or lines.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3473
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3474 The Python parser does not strip indentation from multi-line string
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3475 literals in Python, so tools that process documentation have to strip
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3476 indentation. This is done using the following convention. The first
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3477 non-blank line {\em after} the first line of the string determines the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3478 amount of indentation for the entire documentation string. (We can't
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3479 use the first line since it is generally adjacent to the string's
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3480 opening quotes so its indentation is not apparent in the string
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3481 literal.) Whitespace ``equivalent'' to this indentation is then
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3482 stripped from the start of all lines of the string. Lines that are
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3483 indented less should not occur, but if they occur all their leading
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3484 whitespace should be stripped. Equivalence of whitespace should be
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3485 tested after expansion of tabs (to 8 spaces, normally).
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3486
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3487 In this release, few of the built-in or standard functions and modules
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3488 have documentation strings.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3489
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3490
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3491 \section{Customizing Import and Built-Ins}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3492
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3493 In preparation for a ``restricted execution mode'' which will be
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3494 usable to run code received from an untrusted source (such as a WWW
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3495 server or client), the mechanism by which modules are imported has
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3496 been redesigned. It is now possible to provide your own function
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3497 \code{__import__} which is called whenever an \code{import} statement
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3498 is executed. There's a built-in function \code{__import__} which
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3499 provides the default implementation, but more interesting, the various
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3500 steps it takes are available separately from the new built-in module
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3501 \code{imp}. (See the section on \code{imp} in the Library Reference
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3502 Manual for more information on this module --- it also contains a
2815
1e82dd6dfe2e added pointer to libimp.tex for __import__ example
Guido van Rossum <guido@python.org>
parents: 2647
diff changeset
3503 complete example of how to write your own \code{__import__} function.)
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3504
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3505 When you do \code{dir()} in a fresh interactive interpreter you will
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3506 see another ``secret'' object that's present in every module:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3507 \code{__builtins__}. This is either a dictionary or a module
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3508 containing the set of built-in objects used by functions defined in
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3509 current module. Although normally all modules are initialized with a
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3510 reference to the same dictionary, it is now possible to use a
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3511 different set of built-ins on a per-module basis. Together with the
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3512 fact that the \code{import} statement uses the \code{__import__}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3513 function it finds in the importing modules' dictionary of built-ins,
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3514 this forms the basis for a future restricted execution mode.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3515
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3516
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3517 \section{Python and the World-Wide Web}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3518
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3519 There is a growing number of modules available for writing WWW tools.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3520 The previous release already sported modules \code{gopherlib},
2352
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3521 \code{ftplib}, \code{httplib} and \code{urllib} (which unifies the
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3522 other three) for accessing data through the commonest WWW protocols.
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3523 This release also provides \code{cgi}, to ease the writing of
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3524 server-side scripts that use the Common Gateway Interface protocol,
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3525 supported by most WWW servers. The module \code{urlparse} provides
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3526 precise parsing of a URL string into its components (address scheme,
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3527 network location, path, parameters, query, and fragment identifier).
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3528
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3529 A rudimentary, parser for HTML files is available in the module
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3530 \code{htmllib}. It currently supports a subset of HTML 1.0 (if you
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3531 bring it up to date, I'd love to receive your fixes!). Unfortunately
26b1b15fc8ec no need to reference doc strings for documented modules
Guido van Rossum <guido@python.org>
parents: 2344
diff changeset
3532 Python seems to be too slow for real-time parsing and formatting of
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2352
diff changeset
3533 HTML such as required by interactive WWW browsers --- but it's good
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2352
diff changeset
3534 enough to write a ``robot'' (an automated WWW browser that searches
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2352
diff changeset
3535 the web for information).
2344
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3536
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3537
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3538 \section{Miscellaneous}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3539
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3540 \begin{itemize}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3541
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3542 \item
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3543 The \code{socket} module now exports all the needed constants used for
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3544 socket operations, such as \code{SO_BROADCAST}.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3545
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3546 \item
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3547 The functions \code{popen()} and \code{fdopen()} in the \code{os}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3548 module now follow the pattern of the built-in function \code{open()}:
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3549 the default mode argument is \code{'r'} and the optional third
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3550 argument specifies the buffer size, where \code{0} means unbuffered,
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3551 \code{1} means line-buffered, and any larger number means the size of
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3552 the buffer in bytes.
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3553
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3554 \end{itemize}
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3555
279d51a18ae4 added chapter on new things in 1.2
Guido van Rossum <guido@python.org>
parents: 2063
diff changeset
3556
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3557 \chapter{New in Release 1.3}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3558
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3559
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3560 This chapter describes yet more recent additions to the Python
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3561 language and library.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3562
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3563
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3564 \section{Keyword Arguments}
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3565
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3566 Functions and methods written in Python can now be called using
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3567 keyword arguments of the form \code{\var{keyword} = \var{value}}. For
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3568 instance, the following function:
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3569
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3570 \begin{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3571 def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3572 print "-- This parrot wouldn't", action,
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3573 print "if you put", voltage, "Volts through it."
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3574 print "-- Lovely plumage, the", type
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3575 print "-- It's", state, "!"
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3576 \end{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3577
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3578 could be called in any of the following ways:
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3579
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3580 \begin{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3581 parrot(1000)
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3582 parrot(action = 'VOOOOOM', voltage = 1000000)
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3583 parrot('a thousand', state = 'pushing up the daisies')
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3584 parrot('a million', 'bereft of life', 'jump')
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3585 \end{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3586
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3587 but the following calls would all be invalid:
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3588
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3589 \begin{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3590 parrot() # required argument missing
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3591 parrot(voltage=5.0, 'dead') # non-keyword argument following keyword
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3592 parrot(110, voltage=220) # duplicate value for argument
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3593 parrot(actor='John Cleese') # unknown keyword
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3594 \end{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3595
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3596 In general, an argument list must have the form: zero or more
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3597 positional arguments followed by zero or more keyword arguments, where
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3598 the keywords must be chosen from the formal parameter names. It's not
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3599 important whether a formal parameter has a default value or not. No
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3600 argument must receive a value more than once --- formal parameter names
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3601 corresponding to positional arguments cannot be used as keywords in
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3602 the same calls.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3603
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3604 Note that no special syntax is required to allow a function to be
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3605 called with keyword arguments. The additional costs incurred by
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3606 keyword arguments are only present when a call uses them.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3607
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3608 (As far as I know, these rules are exactly the same as used by
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3609 Modula-3, even if they are enforced by totally different means. This
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3610 is intentional.)
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3611
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3612 When a final formal parameter of the form \code{**\var{name}} is
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3613 present, it receives a dictionary containing all keyword arguments
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3614 whose keyword doesn't correspond to a formal parameter. This may be
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3615 combined with a formal parameter of the form \code{*\var{name}} which
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3616 receives a tuple containing the positional arguments beyond the formal
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3617 parameter list. (\code{*\var{name}} must occur before
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3618 \code{**\var{name}}.) For example, if we define a function like this:
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3619
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3620 \begin{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3621 def cheeseshop(kind, *arguments, **keywords):
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3622 print "-- Do you have any", kind, '?'
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3623 print "-- I'm sorry, we're all out of", kind
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3624 for arg in arguments: print arg
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3625 print '-'*40
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3626 for kw in keywords.keys(): print kw, ':', keywords[kw]
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3627 \end{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3628
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3629 It could be called like this:
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3630
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3631 \begin{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3632 cheeseshop('Limburger', "It's very runny, sir.",
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3633 "It's really very, VERY runny, sir.",
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3634 client='John Cleese',
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3635 shopkeeper='Michael Palin',
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3636 sketch='Cheese Shop Sketch')
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3637 \end{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3638
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3639 and of course it would print:
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3640
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3641 \begin{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3642 -- Do you have any Limburger ?
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3643 -- I'm sorry, we're all out of Limburger
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3644 It's very runny, sir.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3645 It's really very, VERY runny, sir.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3646 ----------------------------------------
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3647 client : John Cleese
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3648 shopkeeper : Michael Palin
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3649 sketch : Cheese Shop Sketch
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3650 \end{verbatim}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3651
3173
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3652 Consequences of this change include:
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3653
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3654 \begin{itemize}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3655
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3656 \item
3173
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3657 The built-in function \code{apply()} now has an optional third
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3658 argument, which is a dictionary specifying any keyword arguments to be
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3659 passed. For example,
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3660 \begin{verbatim}
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3661 apply(parrot, (), {'voltage': 20, 'action': 'voomm'})
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3662 \end{verbatim}
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3663 is equivalent to
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3664 \begin{verbatim}
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3665 parrot(voltage=20, action='voomm')
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3666 \end{verbatim}
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3667
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3668 \item
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3669 There is also a mechanism for functions and methods defined in an
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3670 extension module (i.e., implemented in C or C++) to receive a
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3671 dictionary of their keyword arguments. By default, such functions do
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3672 not accept keyword arguments, since the argument names are not
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3673 available to the interpreter.
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3674
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3675 \item
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3676 In the effort of implementing keyword arguments, function and
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3677 especially method calls have been sped up significantly --- for a
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3678 method with ten formal parameters, the call overhead has been cut in
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3679 half; for a function with one formal parameters, the overhead has been
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3680 reduced by a third.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3681
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3682 \item
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3683 The format of \code{.pyc} files has changed (again).
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3684
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3685 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3686 The \code{access} statement has been disabled. The syntax is still
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3687 recognized but no code is generated for it. (There were some
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3688 unpleasant interactions with changes for keyword arguments, and my
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3689 plan is to get rid of \code{access} altogether in favor of a different
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3690 approach.)
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3691
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3692 \end{itemize}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3693
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3694 \section{Changes to the WWW and Internet tools}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3695
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3696 \begin{itemize}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3697
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3698 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3699 The \code{htmllib} module has been rewritten in an incompatible
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3700 fashion. The new version is considerably more complete (HTML 2.0
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3701 except forms, but including all ISO-8859-1 entity definitions), and
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3702 easy to use. Small changes to \code{sgmllib} have also been made, to
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3703 better match the tokenization of HTML as recognized by other web
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3704 tools.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3705
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3706 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3707 A new module \code{formatter} has been added, for use with the new
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3708 \code{htmllib} module.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3709
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3710 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3711 The \code{urllib}and \code{httplib} modules have been changed somewhat
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3712 to allow overriding unknown URL types and to support authentication.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3713 They now use \code{mimetools.Message} instead of \code{rfc822.Message}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3714 to parse headers. The \code{endrequest()} method has been removed
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3715 from the HTTP class since it breaks the interaction with some servers.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3716
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3717 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3718 The \code{rfc822.Message} class has been changed to allow a flag to be
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3719 passed in that says that the file is unseekable.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3720
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3721 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3722 The \code{ftplib} module has been fixed to be (hopefully) more robust
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3723 on Linux.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3724
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3725 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3726 Several new operations that are optionally supported by servers have
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3727 been added to \code{nntplib}: \code{xover}, \code{xgtitle},
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3728 \code{xpath} and \code{date}. % thanks to Kevan Heydon
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3729
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3730 \end{itemize}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3731
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3732 \section{Other Language Changes}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3733
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3734 \begin{itemize}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3735
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3736 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3737 The \code{raise} statement now takes an optional argument which
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3738 specifies the traceback to be used when printing the exception's stack
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3739 trace. This must be a traceback object, such as found in
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3740 \code{sys.exc_traceback}. When omitted or given as \code{None}, the
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3741 old behavior (to generate a stack trace entry for the current stack
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3742 frame) is used.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3743
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3744 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3745 The tokenizer is now more tolerant of alien whitespace. Control-L in
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3746 the leading whitespace of a line resets the column number to zero,
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3747 while Control-R just before the end of the line is ignored.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3748
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3749 \end{itemize}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3750
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3751 \section{Changes to Built-in Operations}
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3752
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3753 \begin{itemize}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3754
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3755 \item
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3756 For file objects, \code{\var{f}.read(0)} and
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3757 \code{\var{f}.readline(0)} now return an empty string rather than
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3758 reading an unlimited number of bytes. For the latter, omit the
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3759 argument altogether or pass a negative value.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3760
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3761 \item
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3762 A new system variable, \code{sys.platform}, has been added. It
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3763 specifies the current platform, e.g. \code{sunos5} or \code{linux1}.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3764
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3765 \item
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3766 The built-in functions \code{input()} and \code{raw_input()} now use
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3767 the GNU readline library when it has been configured (formerly, only
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3768 interactive input to the interpreter itself was read using GNU
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3769 readline). The GNU readline library provides elaborate line editing
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3770 and history. The Python debugger (\code{pdb}) is the first
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3771 beneficiary of this change.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3772
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3773 \item
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3774 Two new built-in functions, \code{globals()} and \code{locals()},
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3775 provide access to dictionaries containming current global and local
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3776 variables, respectively. (These augment rather than replace
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3777 \code{vars()}, which returns the current local variables when called
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3778 without an argument, and a module's global variables when called with
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3779 an argument of type module.)
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3780
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3781 \item
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3782 The built-in function \code{compile()} now takes a third possible
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3783 value for the kind of code to be compiled: specifying \code{'single'}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3784 generates code for a single interactive statement, which prints the
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3785 output of expression statements that evaluate to something else than
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3786 \code{None}.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3787
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3788 \end{itemize}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3789
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3790 \section{Library Changes}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3791
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3792 \begin{itemize}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3793
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3794 \item
3173
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3795 There are new module \code{ni} and \code{ihooks} that support
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3796 importing modules with hierarchical names such as \code{A.B.C}. This
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3797 is enabled by writing \code{import ni; ni.ni()} at the very top of the
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3798 main program. These modules are amply documented in the Python
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3799 source.
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3800
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3801 \item
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3802 The module \code{rexec} has been rewritten (incompatibly) to define a
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3803 class and to use \code{ihooks}.
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3804
6d34ade2e9d4 a fe more things: apply 3rd arg, ni, ihooks, rexec
Guido van Rossum <guido@python.org>
parents: 3164
diff changeset
3805 \item
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3806 The \code{string.split()} and \code{string.splitfields()} functions
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3807 are now the same function (the presence or absence of the second
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3808 argument determines which operation is invoked); similar for
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3809 \code{string.join()} and \code{string.joinfields()}.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3810
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3811 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3812 The \code{Tkinter} module and its helper \code{Dialog} have been
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3813 revamped to use keyword arguments. Tk 4.0 is now the standard. A new
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3814 module \code{FileDialog} has been added which implements standard file
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3815 selection dialogs.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3816
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3817 \item
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3818 The optional built-in modules \code{dbm} and \code{gdbm} are more
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3819 coordinated --- their \code{open()} functions now take the same values
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3820 for their \var{flag} argument, and the \var{flag} and \var{mode}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3821 argument have default values (to open the database for reading only,
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3822 and to create the database with mode \code{0666} minuse the umask,
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3823 respectively). The memory leaks have finally been fixed.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3824
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3825 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3826 A new dbm-like module, \code{bsddb}, has been added, which uses the
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3827 BSD DB package's hash method. % thanks to David Ely
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3828
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3829 \item
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3830 A portable (though slow) dbm-clone, implemented in Python, has been
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3831 added for systems where none of the above is provided. It is aptly
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3832 dubbed \code{dumbdbm}.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3833
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3834 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3835 The module \code{anydbm} provides a unified interface to \code{bsddb},
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3836 \code{gdbm}, \code{dbm}, and \code{dumbdbm}, choosing the first one
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3837 available.
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3838
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3839 \item
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3840 A new extension module, \code{binascii}, provides a variety of
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3841 operations for conversion of text-encoded binary data.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3842
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3843 \item
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3844 There are three new or rewritten companion modules implemented in
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3845 Python that can encode and decode the most common such formats:
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3846 \code{uu} (uuencode), \code{base64} and \code{binhex}.
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3847
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3848 \item
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3849 A module to handle the MIME encoding quoted-printable has also been
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3850 added: \code{quopri}.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3851
3204
d5e76a95b566 mention Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents: 3176
diff changeset
3852 \item
d5e76a95b566 mention Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents: 3176
diff changeset
3853 The parser module (which provides an interface to the Python parser's
d5e76a95b566 mention Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents: 3176
diff changeset
3854 abstract syntax trees) has been rewritten (incompatibly) by Fred
d5e76a95b566 mention Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents: 3176
diff changeset
3855 Drake. It now lets you change the parse tree and compile the result!
d5e76a95b566 mention Fred Drake's parser module
Guido van Rossum <guido@python.org>
parents: 3176
diff changeset
3856
3210
cdebee4a3e3e mention syslog upgrade
Guido van Rossum <guido@python.org>
parents: 3204
diff changeset
3857 \item
cdebee4a3e3e mention syslog upgrade
Guido van Rossum <guido@python.org>
parents: 3204
diff changeset
3858 The \code{syslog} module has been upgraded and documented.
cdebee4a3e3e mention syslog upgrade
Guido van Rossum <guido@python.org>
parents: 3204
diff changeset
3859 % thanks to Steve Clift
cdebee4a3e3e mention syslog upgrade
Guido van Rossum <guido@python.org>
parents: 3204
diff changeset
3860
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3861 \end{itemize}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3862
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3863 \section{Other Changes}
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3864
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3865 \begin{itemize}
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3866
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3867 \item
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3868 The dynamic module loader recognizes the fact that different filenames
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3869 point to the same shared library and loads the library only once, so
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3870 you can have a single shared library that defines multiple modules.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3871 (SunOS / SVR4 style shared libraries only.)
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3872
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3873 \item
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3874 Jim Fulton's ``abstract object interface'' has been incorporated into
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3875 the run-time API. For more detailes, read the files
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3876 \code{Include/abstract.h} and \code{Objects/abstract.c}.
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3877
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3878 \item
3164
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3879 The Macintosh version is much more robust now.
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3880
95e80b6b8f2e describe more 1.3 changes
Guido van Rossum <guido@python.org>
parents: 3081
diff changeset
3881 \item
2942
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3882 Numerous things I have forgotten or that are so obscure no-one will
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3883 notice them anyway :-)
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3884
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3885 \end{itemize}
3715206397fe chapter on new things in 1.3
Guido van Rossum <guido@python.org>
parents: 2815
diff changeset
3886
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3887
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3888 \chapter{New in Release 1.4}
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3889
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3890
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3891 This chapter describes the major additions to the Python language and
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3892 library in version 1.4. Many minor changes are not listed here;
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3893 it is recommended to read the file \code{Misc/NEWS} in the Python
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3894 source distribution for a complete listing of changes. In particular,
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3895 changes that only affect C programmers or the build and installation
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3896 process are not described in this chapter (the new installation
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3897 lay-out is explained below under \code{sys.prefix} though).
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3898
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
3899 \section{Language Changes}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
3900
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3901 \begin{itemize}
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3902
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3903 \item
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3904 Power operator. \code{x**y} is equivalent to \code{pow(x, y)}.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3905 This operator binds more tightly than \code{*}, \code{/} or \code{\%},
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3906 and binds from right to left when repeated or combined with unary
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3907 operators. For example, \code{x**y**z} is equivalent to
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3908 \code{x**(y**z)}, and \code{-x**y} is \code{-(x**y)}.
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3909
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3910 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3911 Complex numbers. Imaginary literals are writen with a \code{'j'}
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3912 suffix (\code{'J'} is allowed as well.) Complex numbers with a nonzero
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3913 real component are written as \code{(\var{real}+\var{imag}j)}. You
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3914 can also use the new built-in function \code{complex()} which takes
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3915 one or two arguments: \code{complex(x)} is equivalent to \code{x +
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3916 0j}, and \code{complex(x, y)} is \code{x + y*0j}. For example,
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3917 \code{1j**2} yields \code{complex(-1.0)} (which is another way of
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3918 saying ``the real value 1.0 represented as a complex number.''
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3919
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3920 Complex numbers are always represented as two floating point numbers,
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3921 the real and imaginary part.
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3922 To extract these parts from a complex number \code{z},
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3923 use \code{z.real} and \code{z.imag}. The conversion functions to
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3924 floating point and integer (\code{float()}, \code{int()} and
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3925 \code{long()}) don't work for complex numbers --- there is no one
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3926 correct way to convert a complex number to a real number. Use
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3927 \code{abs(z)} to get its magnitude (as a float) or \code{z.real} to
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3928 get its real part.
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3929
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3930 Module \code{cmath} provides versions of all math functions that take
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3931 complex arguments and return complex results. (Module \code{math}
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3932 only supports real numbers, so that \code{math.sqrt(-1)} still raises
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3933 a \code{ValueError} exception. Numerical experts agree that this is
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3934 the way it should be.)
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3935
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3936 \item
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3937 New indexing syntax. It is now possible to use a tuple as an indexing
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3938 expression for a mapping object without parenthesizing it,
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3939 e.g. \code{x[1, 2, 3]} is equivalent to \code{x[(1, 2, 3)]}.
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3940
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3941 \item
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3942 New slicing syntax. In support of the Numerical Python extension
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3943 (distributed independently), slice indices of the form
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3944 \code{x[lo:hi:stride]} are possible, multiple slice indices separated by
4513
847445aeab2d Change last Ellipses to Ellipsis :-(
Guido van Rossum <guido@python.org>
parents: 4509
diff changeset
3945 commas are allowed, and an index position may be replaced by an ellipsis,
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3946 as follows: \code{x[a, ..., z]}. There's also a new built-in function
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3947 \code{slice(lo, hi, stride)} and a new built-in object
4513
847445aeab2d Change last Ellipses to Ellipsis :-(
Guido van Rossum <guido@python.org>
parents: 4509
diff changeset
3948 \code{Ellipsis}, which yield the same effect without using special
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3949 syntax. None of the standard sequence types support indexing with
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3950 slice objects or ellipses yet.
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3951
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3952 Note that when this new slicing syntax is used, the mapping interface
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3953 will be used, not the sequence interface. In particular, when a
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3954 user-defined class instance is sliced using this new slicing syntax,
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3955 its \code{__getitem__} method is invoked --- the
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3956 \code{__getslice__} method is only invoked when a single old-style
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3957 slice is used, i.e. \code{x[lo:hi]}, with possible omission of
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3958 \code{lo} and/or \code{hi}. Some examples:
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3959
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3960 \begin{verbatim}
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3961 x[0:10:2] -> slice(0, 10, 2)
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3962 x[:2:] -> slice(None, 2, None)
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
3963 x[::-1] -> slice(None, None, -1)
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3964 x[::] -> slice(None, None, None)
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3965 x[1, 2:3] -> (1, slice(2, 3, None))
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3966 x[1:2, 3:4] -> (slice(1, 2, None), slice(3, 4, None))
4513
847445aeab2d Change last Ellipses to Ellipsis :-(
Guido van Rossum <guido@python.org>
parents: 4509
diff changeset
3967 x[1:2, ..., 3:4] -> (slice(1, 2, None), Ellipsis,
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3968 slice(3, 4, None))
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3969 \end{verbatim}
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3970
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3971 For more help with this you are referred to the matrix-sig.
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3972
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
3973 \item
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3974 The \code{access} statement is now truly gone; \code{access} is no
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3975 longer a reserved word. This saves a few cycles here and there.
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3976
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
3977 \item
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
3978 Private variables through name mangling.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
3979 There is now limited support for class-private
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3980 identifiers. Any identifier of the form \code{__spam} (at least two
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3981 leading underscores, at most one trailing underscore) is now textually
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3982 replaced with \code{_classname__spam}, where \code{classname} is the
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3983 current class name with leading underscore(s) stripped. This mangling
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3984 is done without regard of the syntactic position of the identifier, so
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3985 it can be used to define class-private instance and class variables,
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3986 methods, as well as globals, and even to store instance variables
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3987 private to this class on instances of {\em other} classes. Truncation
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3988 may occur when the mangled name would be longer than 255 characters.
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3989 Outside classes, or when the class name consists of only underscores,
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3990 no mangling occurs.
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3991
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3992 Name mangling is intended to give classes an easy way to define
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3993 ``private'' instance variables and methods, without having to worry
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3994 about instance variables defined by derived classes, or mucking with
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3995 instance variables by code outside the class. Note that the mangling
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3996 rules are designed mostly to avoid accidents; it still is possible for
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3997 a determined soul to access or modify a variable that is considered
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
3998 private. This can even be useful, e.g. for the debugger, and that's
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
3999 one reason why this loophole is not closed. (Buglet: derivation of a
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4000 class with the same name as the base class makes use of private
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4001 variables of the base class possible.)
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4002
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4003 Notice that code passed to \code{exec}, \code{eval()} or
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4004 \code{evalfile()} does not consider the classname of the invoking
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4005 class to be the current class; this is similar to the effect of the
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4006 \code{global} statement, the effect of which is likewise restricted to
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4007 code that is byte-compiled together. The same restriction applies to
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4008 \code{getattr()}, \code{setattr()} and \code{delattr()}, as well as
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4009 when referencing \code{__dict__} directly.
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4010
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4011 Here's an example of a class that implements its own
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4012 \code{__getattr__} and \code{__setattr__} methods and stores all
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4013 attributes in a private variable, in a way that works in Python 1.4 as
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4014 well as in previous versions:
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4015
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4016 \begin{verbatim}
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4017 class VirtualAttributes:
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4018 __vdict = None
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4019 __vdict_name = locals().keys()[0]
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4020
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4021 def __init__(self):
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4022 self.__dict__[self.__vdict_name] = {}
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4023
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4024 def __getattr__(self, name):
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4025 return self.__vdict[name]
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4026
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4027 def __setattr__(self, name, value):
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4028 self.__vdict[name] = value
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4029 \end{verbatim}
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4030
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4031 {\em Warning: this is an experimental feature.} To avoid all
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4032 potential problems, refrain from using identifiers starting with
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4033 double underscore except for predefined uses like \code{__init__}. To
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4034 use private names while maintaining future compatibility: refrain from
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4035 using the same private name in classes related via subclassing; avoid
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4036 explicit (manual) mangling/unmangling; and assume that at some point
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4037 in the future, leading double underscore will revert to being just a
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4038 naming convention. Discussion on extensive compile-time declarations
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4039 are currently underway, and it is impossible to predict what solution
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4040 will eventually be chosen for private names. Double leading
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4041 underscore is still a candidate, of course --- just not the only one.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4042 It is placed in the distribution in the belief that it is useful, and
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4043 so that widespread experience with its use can be gained. It will not
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4044 be removed without providing a better solution and a migration path.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4045
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4046 \end{itemize}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4047
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4048 \section{Run-time Changes}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4049
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4050 \begin{itemize}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4051
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4052 \item
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4053 New built-in function \code{list()} converts any sequence to a new list.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4054 Note that when the argument is a list, the return value is a fresh
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4055 copy, similar to what would be returned by \code{a[:]}.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4056
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4057 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4058 Improved syntax error message. Syntax errors detected by the code
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4059 generation phase of the Python bytecode compiler now include a line
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4060 number. The line number is appended in parentheses. It is suppressed
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4061 if the error occurs in line 1 (this usually happens in interactive
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4062 use).
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4063
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4064 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4065 Different exception raised.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4066 Unrecognized keyword arguments now raise a \code{TypeError} exception
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4067 rather than \code{KeyError}.
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4068
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4069 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4070 Exceptions in \code{__del__} methods. When a \code{__del__} method
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4071 raises an exception, a warning is written to \code{sys.stderr} and the
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4072 exception is ignored. Formerly, such exceptions were ignored without
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4073 warning. (Propagating the exception is not an option since it it is
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4074 invoked from an object finalizer, which cannot return any kind of
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4075 status or error.) (Buglet: The new behavior, while needed in order to
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4076 debug failing \code{__del__} methods, is occasionally annoying,
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4077 because if affects the program's standard error stream. It honors
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4078 assignments to \code{sys.stderr}, so it can be redirected from within
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4079 a program if desired.)
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4080
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4081 \item
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4082 You can now discover from which file (if any) a module was loaded by
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4083 inspecting its \code{__file__} attribute. This attribute is not
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4084 present for built-in or frozen modules. It points to the shared
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4085 library file for dynamically loaded modules. (Buglet: this may be a
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4086 relative path and is stored in the \code{.pyc} file on compilation.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4087 If you manipulate the current directory with \code{os.chdir()} or move
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4088 \code{.pyc} files around, the value may be incorrect.)
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4089
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4090 \end{itemize}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4091
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4092 \section{New or Updated Modules}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4093
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4094 \begin{itemize}
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4095
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4096 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4097 New built-in module \code{operator}. While undocumented, the concept
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4098 is real simply: \code{operator.__add__(x, y)} does exactly the same
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4099 thing as \code{x+y} (for all types --- built-in, user-defined,
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4100 extension-defined). As a convenience, \code{operator.add} does the
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4101 same thing, but beware --- you can't use \code{operator.and} and a few
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4102 others where the ``natural'' name for an operator is a reserved
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4103 keyword. You can add a single trailing underscore in such cases.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4104
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4105 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4106 New built-in module \code{errno}. See the Library Reference Manual.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4107
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4108 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4109 Rewritten \code{cgi} module. See the Library Reference Manual.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4110
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4111 \item
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4112 Improved restricted execution module (\code{rexec}). New module
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4113 \code{Bastion}. Both are now documented in a new chapter on
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4114 restricted execution in the Library Reference Manual.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4115
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4116 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4117 New string operations (all described in the Library Reference Manual):
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4118 \code{lstrip()}, \code{rstrip()} (strip only the left/right
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4119 whitespace), \code{capitalize()} (uppercase the first character,
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4120 lowercase the rest), \code{capwords()} (capitalize each word,
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4121 delimited a la \code{string.split()}), \code{translate()} (string
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4122 transliteration -- this existed before but can now also delete
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4123 characters by specifying a third argument), \code{maketrans()} (a
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4124 convenience function for creating translation tables for
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4125 \code{translate()} and \code{regex.compile()}). The string function
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4126 \code{split()} has an optional third argument which specifies the
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4127 maximum number of separators to split;
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4128 e.g. \code{string.split('a=b=c', '=', 1)} yields \code{['a', 'b=c']}.
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4129 (Note that for a long time, \code{split()} and \code{splitfields()}
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4130 are synonyms.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4131
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4132 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4133 New regsub operations (see the Library Reference Manual):
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4134 \code{regsub.capwords()} (like \code{string.capwords()} but allows you to
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4135 specify the word delimiter as a regular expression),
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4136 \code{regsub.splitx()} (like \code{regsub.split()} but returns the
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4137 delimiters as well as the words in the resulting list). The optional
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4138 \code{maxsep} argument is also supported by \code{regsub.split()}.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4139
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4140 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4141 Module files \code{pdb.py} and \code{profile.py} can now be invoked as
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4142 scripts to debug c.q. profile other scripts easily. For example:
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4143 \code{python /usr/local/lib/python1.4/profile.py myscript.py}
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4144
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4145 \item
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4146 The \code{os} module now supports the \code{putenv()} function on
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4147 systems where it is provided in the C library (Windows NT and most
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4148 Unix versions). For example, \code{os.putenv('PATH',
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4149 '/bin:/usr/bin')} sets the environment variable \code{PATH} to the
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4150 string \code{'/bin:/usr/bin'}. Such changes to the environment affect
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4151 subprocesses started with \code{os.system()}, \code{os.popen()} or
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4152 \code{os.fork()} and \code{os.execv()}. When \code{putenv()} is
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4153 supported, assignments to items in \code{os.environ} are automatically
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4154 translated into corresponding calls to \code{os.putenv()}; however,
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4155 calls to \code{os.putenv()} don't update \code{os.environ}, so it is
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4156 actually preferable to assign to items of \code{os.environ}. For this
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4157 purpose, the type of \code{os.environ} is changed to a subclass of
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4158 \code{UserDict.UserDict} when \code{os.putenv()} is supported.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4159 (Buglet: \code{os.execve()} still requires a real dictionary, so it
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4160 won't accept \code{os.environ} as its third argument. However, you
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4161 can now use \code{os.execv()} and it will use your changes to
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4162 \code{os.environ}!.)
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4163
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4164 \item
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4165 More new functions in the \code{os} module: \code{mkfifo},
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4166 \code{plock}, \code{remove} (== \code{unlink}), and \code{ftruncate}.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4167 See the Unix manual (section 2, system calls) for these function.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4168 More functions are also available under NT.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4169
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4170 \item
4452
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4171 New functions in the fcntl module: \code{lockf()} and \code{flock()}
edab097882d8 Got rid of most XXXes in the News chapter. Bumped the version to 1.4.
Guido van Rossum <guido@python.org>
parents: 4379
diff changeset
4172 (don't ask \code{:-)}). See the Library Reference Manual.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4173
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4174 \item
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4175 The first item of the module search path, \code{sys.path[0]}, is the
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4176 directory containing the script that was used to invoke the Python
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4177 interpreter. If the script directory is not available (e.g. if the
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4178 interpreter is invoked interactively or if the script is read from
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4179 standard input), \code{sys.path[0]} is the empty string, which directs
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4180 Python to search modules in the current directory first. Notice that
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4181 the script directory is inserted {\em before} the entries inserted as
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4182 a result of \code{\$PYTHONPATH}. There is no longer an entry for the
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4183 current directory later in the path (unless explicitly set in
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4184 \code{\$PYTHONPATH} or overridden at build time).
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4185
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4186 \end{itemize}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4187
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4188 \section{Configuration and Installation}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4189
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4190 \begin{itemize}
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4191
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4192 \item
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4193 More configuration information is now available to Python programs.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4194 The variable \code{sys.prefix} gives the site-specific directory
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4195 prefix where the platform independent Python files are installed; by
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4196 default, this is the string \code{"/usr/local"}. This can be set at
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4197 build time with the \code{--prefix} argument to the \code{configure}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4198 script. The main collection of Python library modules is installed in
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4199 the directory \code{sys.prefix+"/lib/python1.4"} while the platform
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4200 independent header files (all except \code{config.h}) are stored in
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4201 \code{sys.prefix+"/include/python1.4"}.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4202
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4203 Similarly, the variable \code{sys.exec_prefix} gives the site-specific
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4204 directory prefix where the platform {\em de}pendent Python files are
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4205 installed; by default, this is also \code{"/usr/local"}. This can be
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4206 set at build time with the \code{--exec-prefix} argument to the
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4207 \code{configure} script. Specifically, all configuration files
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4208 (e.g. the \code{config.h} header file) are installed in the directory
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4209 \code{sys.exec_prefix+"/lib/python1.4/config"}, and shared library
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4210 modules are installed in
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4211 \code{sys.exec_prefix+"/lib/python1.4/sharedmodules"}.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4212
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4213 Include files are at \code{sys.prefix+"/include/python1.4"}.
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4214
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4215 It is not yet decided what the most portable way is to come up with
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4216 the version number used in these pathnames. For compatibility with
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4217 the 1.4beta releases, sys.version[:3] can be used.
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4218
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4219 On non-Unix systems, these variables are meaningless.
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4220
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4221 \item
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4222 While sites are strongly discouraged from modifying the standard
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4223 Python library (like adding site-specific modules or functions), there
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4224 is now a standard way to invoke site-specific features. The standard
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4225 module \code{site}, when imported, appends two site-specific
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4226 directories to the end of \code{sys.path}:
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4227 \code{\$prefix/lib/site-python} and
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4228 \code{\$exec_prefix/lib/site-python}, where \code{\$prefix} and
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4229 \code{\$exec_prefix} are the directories \code{sys.prefix} and
4379
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4230 \code{sys.exec_prefix} mentioned above.
3061676a659a A first stab at describing what's new in 1.4. Still many XXX'es left.
Guido van Rossum <guido@python.org>
parents: 4118
diff changeset
4231
4491
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4232 After this path manipulation has been performed, an attempt is made to
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4233 import the module \code{sitecustomize}. Any \code{ImportError}
6a75b3cf023c Done with the "New in 1.4" chapter.
Guido van Rossum <guido@python.org>
parents: 4452
diff changeset
4234 exception raised by this attempt is silently ignored.
4118
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
4235
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
4236 \end{itemize}
a86407b3c3ac Version number; started chapter on what's new in 1.4
Guido van Rossum <guido@python.org>
parents: 3659
diff changeset
4237
112
d8a1b6456212 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4238 \end{document}