Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 1 | \chapter{Glossary\label{glossary}} |
| 2 | |
| 3 | %%% keep the entries sorted and include at least one \index{} item for each |
Raymond Hettinger | 5a25aa6 | 2003-09-27 05:42:14 +0000 | [diff] [blame] | 4 | %%% cross-references are marked with \emph{entry} |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 5 | |
| 6 | \begin{description} |
| 7 | |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 8 | |
| 9 | \index{>>>} |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10 | \item[\code{>>>}] |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 11 | The typical Python prompt of the interactive shell. Often seen for |
| 12 | code examples that can be tried right away in the interpreter. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 13 | |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 14 | \index{...} |
| 15 | \item[\code{.\code{.}.}] |
| 16 | The typical Python prompt of the interactive shell when entering code |
| 17 | for an indented code block. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 18 | |
| 19 | \index{BDFL} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 20 | \item[BDFL] |
| 21 | Benevolent Dictator For Life, a.k.a. \ulink{Guido van |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 22 | Rossum}{http://www.python.org/\textasciitilde{}guido/}, Python's creator. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 23 | |
| 24 | \index{byte code} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 25 | \item[byte code] |
| 26 | The internal representation of a Python program in the interpreter. |
Raymond Hettinger | f755432 | 2005-08-21 12:35:29 +0000 | [diff] [blame] | 27 | The byte code is also cached in \code{.pyc} and \code{.pyo} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 28 | files so that executing the same file is faster the second time |
Raymond Hettinger | f755432 | 2005-08-21 12:35:29 +0000 | [diff] [blame] | 29 | (recompilation from source to byte code can be avoided). This |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 30 | ``intermediate language'' is said to run on a ``virtual |
| 31 | machine'' that calls the subroutines corresponding to each bytecode. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 32 | |
| 33 | \index{classic class} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 34 | \item[classic class] |
| 35 | Any class which does not inherit from \class{object}. See |
| 36 | \emph{new-style class}. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 37 | |
Skip Montanaro | dbb4078 | 2004-03-27 18:23:11 +0000 | [diff] [blame] | 38 | \index{complex number} |
| 39 | \item[complex number] |
Skip Montanaro | dbb4078 | 2004-03-27 18:23:11 +0000 | [diff] [blame] | 40 | An extension of the familiar real number system in which all numbers are |
| 41 | expressed as a sum of a real part and an imaginary part. Imaginary numbers |
| 42 | are real multiples of the imaginary unit (the square root of {}\code{-1}), |
| 43 | often written {}\code{i} in mathematics or {}\code{j} in engineering. |
| 44 | Python has builtin support for complex numbers, which are written with this |
| 45 | latter notation; the imaginary part is written with a {}\code{j} suffix, |
| 46 | e.g., {}\code{3+1j}. To get access to complex equivalents of the |
| 47 | {}\module{math} module, use {}\module{cmath}. Use of complex numbers is a |
Andrew M. Kuchling | aeaec8d | 2004-03-29 01:19:54 +0000 | [diff] [blame] | 48 | fairly advanced mathematical feature. If you're not aware of a need for them, |
Raymond Hettinger | f13c024 | 2004-03-28 22:44:09 +0000 | [diff] [blame] | 49 | it's almost certain you can safely ignore them. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 50 | |
| 51 | \index{descriptor} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 52 | \item[descriptor] |
| 53 | Any \emph{new-style} object that defines the methods |
| 54 | {}\method{__get__()}, \method{__set__()}, or \method{__delete__()}. |
| 55 | When a class attribute is a descriptor, its special binding behavior |
| 56 | is triggered upon attribute lookup. Normally, writing \var{a.b} looks |
| 57 | up the object \var{b} in the class dictionary for \var{a}, but if |
| 58 | {}\var{b} is a descriptor, the defined method gets called. |
| 59 | Understanding descriptors is a key to a deep understanding of Python |
| 60 | because they are the basis for many features including functions, |
| 61 | methods, properties, class methods, static methods, and reference to |
| 62 | super classes. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 63 | |
| 64 | \index{dictionary} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 65 | \item[dictionary] |
| 66 | An associative array, where arbitrary keys are mapped to values. The |
| 67 | use of \class{dict} much resembles that for \class{list}, but the keys |
| 68 | can be any object with a \method{__hash__()} function, not just |
| 69 | integers starting from zero. Called a hash in Perl. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 70 | |
Raymond Hettinger | d4f5b07 | 2005-01-11 16:11:13 +0000 | [diff] [blame] | 71 | \index{duck-typing} |
Raymond Hettinger | 0f43983 | 2005-05-14 17:18:31 +0000 | [diff] [blame] | 72 | \item[duck-typing] |
Raymond Hettinger | d4f5b07 | 2005-01-11 16:11:13 +0000 | [diff] [blame] | 73 | Pythonic programming style that determines an object's type by inspection |
| 74 | of its method or attribute signature rather than by explicit relationship |
| 75 | to some type object ("If it looks like a duck and quacks like a duck, it |
| 76 | must be a duck.") By emphasizing interfaces rather than specific types, |
| 77 | well-designed code improves its flexibility by allowing polymorphic |
| 78 | substitution. Duck-typing avoids tests using \function{type()} or |
| 79 | \function{isinstance()}. Instead, it typically employs |
| 80 | \function{hasattr()} tests or {}\emph{EAFP} programming. |
| 81 | |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 82 | \index{EAFP} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 83 | \item[EAFP] |
| 84 | Easier to ask for forgiveness than permission. This common Python |
| 85 | coding style assumes the existence of valid keys or attributes and |
| 86 | catches exceptions if the assumption proves false. This clean and |
| 87 | fast style is characterized by the presence of many \keyword{try} and |
| 88 | {}\keyword{except} statements. The technique contrasts with the |
| 89 | {}\emph{LBYL} style that is common in many other languages such as C. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 90 | |
| 91 | \index{__future__} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 92 | \item[__future__] |
| 93 | A pseudo module which programmers can use to enable new language |
Neal Norwitz | 4886cc3 | 2006-08-21 17:06:07 +0000 | [diff] [blame] | 94 | features which are not compatible with the current interpreter. |
| 95 | To enable \code{new_feature} |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 96 | |
| 97 | \begin{verbatim} |
Neal Norwitz | 4886cc3 | 2006-08-21 17:06:07 +0000 | [diff] [blame] | 98 | from __future__ import new_feature |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 99 | \end{verbatim} |
| 100 | |
Neal Norwitz | 4886cc3 | 2006-08-21 17:06:07 +0000 | [diff] [blame] | 101 | By importing the \ulink{\module{__future__}}{../lib/module-future.html} |
Fred Drake | 984920b | 2003-09-28 19:03:36 +0000 | [diff] [blame] | 102 | module and evaluating its variables, you can see when a new feature |
| 103 | was first added to the language and when it will become the default: |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 104 | |
| 105 | \begin{verbatim} |
| 106 | >>> import __future__ |
| 107 | >>> __future__.division |
| 108 | _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192) |
| 109 | \end{verbatim} |
| 110 | |
| 111 | \index{generator} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 112 | \item[generator] |
Skip Montanaro | dbb4078 | 2004-03-27 18:23:11 +0000 | [diff] [blame] | 113 | A function that returns an iterator. It looks like a normal function except |
| 114 | that values are returned to the caller using a \keyword{yield} statement |
| 115 | instead of a {}\keyword{return} statement. Generator functions often |
| 116 | contain one or more {}\keyword{for} or \keyword{while} loops that |
| 117 | \keyword{yield} elements back to the caller. The function execution is |
| 118 | stopped at the {}\keyword{yield} keyword (returning the result) and is |
| 119 | resumed there when the next element is requested by calling the |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 120 | \method{__next__()} method of the returned iterator. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 121 | |
Raymond Hettinger | d348193 | 2004-06-07 21:52:47 +0000 | [diff] [blame] | 122 | \index{generator expression} |
| 123 | \item[generator expression] |
| 124 | An expression that returns a generator. It looks like a normal expression |
| 125 | followed by a \keyword{for} expression defining a loop variable, range, and |
| 126 | an optional \keyword{if} expression. The combined expression generates |
| 127 | values for an enclosing function: |
| 128 | |
| 129 | \begin{verbatim} |
| 130 | >>> sum(i*i for i in range(10)) # sum of squares 0, 1, 4, ... 81 |
| 131 | 285 |
| 132 | \end{verbatim} |
| 133 | |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 134 | \index{GIL} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 135 | \item[GIL] |
| 136 | See \emph{global interpreter lock}. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 137 | |
| 138 | \index{global interpreter lock} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 139 | \item[global interpreter lock] |
| 140 | The lock used by Python threads to assure that only one thread can be |
| 141 | run at a time. This simplifies Python by assuring that no two |
| 142 | processes can access the same memory at the same time. Locking the |
| 143 | entire interpreter makes it easier for the interpreter to be |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 144 | multi-threaded, at the expense of some parallelism on multi-processor |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 145 | machines. Efforts have been made in the past to create a |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 146 | ``free-threaded'' interpreter (one which locks shared data at a much |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 147 | finer granularity), but performance suffered in the common |
| 148 | single-processor case. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 149 | |
| 150 | \index{IDLE} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 151 | \item[IDLE] |
| 152 | An Integrated Development Environment for Python. IDLE is a |
Raymond Hettinger | 5a25aa6 | 2003-09-27 05:42:14 +0000 | [diff] [blame] | 153 | basic editor and interpreter environment that ships with the standard |
| 154 | distribution of Python. Good for beginners, it also serves as clear |
| 155 | example code for those wanting to implement a moderately |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 156 | sophisticated, multi-platform GUI application. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 157 | |
| 158 | \index{immutable} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 159 | \item[immutable] |
Skip Montanaro | dbb4078 | 2004-03-27 18:23:11 +0000 | [diff] [blame] | 160 | An object with fixed value. Immutable objects are numbers, strings or |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 161 | tuples (and more). Such an object cannot be altered. A new object |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 162 | has to be created if a different value has to be stored. They play an |
Raymond Hettinger | 65a350d | 2004-12-02 07:29:43 +0000 | [diff] [blame] | 163 | important role in places where a constant hash value is needed, for |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 164 | example as a key in a dictionary. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 165 | |
| 166 | \index{integer division} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 167 | \item[integer division] |
Neal Norwitz | 4886cc3 | 2006-08-21 17:06:07 +0000 | [diff] [blame] | 168 | Mathematical division including any remainder. The result will always |
| 169 | be a float. For example, the expression \code{11/4} evaluates to \code{2.75}. |
| 170 | Integer division can be forced by using the \code{//} operator instead |
| 171 | of the \code{/} operator. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 172 | |
| 173 | \index{interactive} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 174 | \item[interactive] |
| 175 | Python has an interactive interpreter which means that you can try out |
Raymond Hettinger | 65a350d | 2004-12-02 07:29:43 +0000 | [diff] [blame] | 176 | things and immediately see their results. Just launch \code{python} with no |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 177 | arguments (possibly by selecting it from your computer's main menu). |
| 178 | It is a very powerful way to test out new ideas or inspect modules and |
| 179 | packages (remember \code{help(x)}). |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 180 | |
| 181 | \index{interpreted} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 182 | \item[interpreted] |
Skip Montanaro | dbb4078 | 2004-03-27 18:23:11 +0000 | [diff] [blame] | 183 | Python is an interpreted language, as opposed to a compiled one. This means |
| 184 | that the source files can be run directly without first creating an |
| 185 | executable which is then run. Interpreted languages typically have a |
| 186 | shorter development/debug cycle than compiled ones, though their programs |
| 187 | generally also run more slowly. See also {}\emph{interactive}. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 188 | |
| 189 | \index{iterable} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 190 | \item[iterable] |
| 191 | A container object capable of returning its members one at a time. |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 192 | Examples of iterables include all sequence types (such as \class{list}, |
Raymond Hettinger | 5a25aa6 | 2003-09-27 05:42:14 +0000 | [diff] [blame] | 193 | {}\class{str}, and \class{tuple}) and some non-sequence types like |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 194 | {}\class{dict} and \class{file} and objects of any classes you define |
| 195 | with an \method{__iter__()} or \method{__getitem__()} method. Iterables |
| 196 | can be used in a \keyword{for} loop and in many other places where a |
| 197 | sequence is needed (\function{zip()}, \function{map()}, ...). When an |
| 198 | iterable object is passed as an argument to the builtin function |
| 199 | {}\function{iter()}, it returns an iterator for the object. This |
| 200 | iterator is good for one pass over the set of values. When using |
| 201 | iterables, it is usually not necessary to call \function{iter()} or |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 202 | deal with iterator objects yourself. The \code{for} statement does |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 203 | that automatically for you, creating a temporary unnamed variable to |
| 204 | hold the iterator for the duration of the loop. See also |
| 205 | {}\emph{iterator}, \emph{sequence}, and \emph{generator}. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 206 | |
| 207 | \index{iterator} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 208 | \item[iterator] |
| 209 | An object representing a stream of data. Repeated calls to the |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 210 | iterator's \method{__next__()} method return successive items in the |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 211 | stream. When no more data is available a \exception{StopIteration} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 212 | exception is raised instead. At this point, the iterator object is |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 213 | exhausted and any further calls to its \method{__next__()} method just |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 214 | raise \exception{StopIteration} again. Iterators are required to have |
| 215 | an \method{__iter__()} method that returns the iterator object |
| 216 | itself so every iterator is also iterable and may be used in most |
| 217 | places where other iterables are accepted. One notable exception is |
| 218 | code that attempts multiple iteration passes. A container object |
| 219 | (such as a \class{list}) produces a fresh new iterator each time you |
| 220 | pass it to the \function{iter()} function or use it in a |
| 221 | {}\keyword{for} loop. Attempting this with an iterator will just |
Raymond Hettinger | 65a350d | 2004-12-02 07:29:43 +0000 | [diff] [blame] | 222 | return the same exhausted iterator object used in the previous iteration |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 223 | pass, making it appear like an empty container. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 224 | |
Raymond Hettinger | f755432 | 2005-08-21 12:35:29 +0000 | [diff] [blame] | 225 | \index{LBYL} |
| 226 | \item[LBYL] |
| 227 | Look before you leap. This coding style explicitly tests for |
| 228 | pre-conditions before making calls or lookups. This style contrasts |
| 229 | with the \emph{EAFP} approach and is characterized by the presence of |
| 230 | many \keyword{if} statements. |
| 231 | |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 232 | \index{list comprehension} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 233 | \item[list comprehension] |
| 234 | A compact way to process all or a subset of elements in a sequence and |
| 235 | return a list with the results. \code{result = ["0x\%02x" |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 236 | \% x for x in range(256) if x \% 2 == 0]} generates a list of strings |
| 237 | containing hex numbers (0x..) that are even and in the range from 0 to 255. |
| 238 | The \keyword{if} clause is optional. If omitted, all elements in |
Raymond Hettinger | 65a350d | 2004-12-02 07:29:43 +0000 | [diff] [blame] | 239 | {}\code{range(256)} are processed. |
| 240 | |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 241 | \index{mapping} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 242 | \item[mapping] |
| 243 | A container object (such as \class{dict}) that supports arbitrary key |
| 244 | lookups using the special method \method{__getitem__()}. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 245 | |
| 246 | \index{metaclass} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 247 | \item[metaclass] |
| 248 | The class of a class. Class definitions create a class name, a class |
| 249 | dictionary, and a list of base classes. The metaclass is responsible |
| 250 | for taking those three arguments and creating the class. Most object |
| 251 | oriented programming languages provide a default implementation. What |
| 252 | makes Python special is that it is possible to create custom |
| 253 | metaclasses. Most users never need this tool, but when the need |
| 254 | arises, metaclasses can provide powerful, elegant solutions. They |
| 255 | have been used for logging attribute access, adding thread-safety, |
| 256 | tracking object creation, implementing singletons, and many other |
| 257 | tasks. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 258 | |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 259 | \index{mutable} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 260 | \item[mutable] |
| 261 | Mutable objects can change their value but keep their \function{id()}. |
| 262 | See also \emph{immutable}. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 263 | |
| 264 | \index{namespace} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 265 | \item[namespace] |
| 266 | The place where a variable is stored. Namespaces are implemented as |
Raymond Hettinger | 65a350d | 2004-12-02 07:29:43 +0000 | [diff] [blame] | 267 | dictionaries. There are the local, global and builtin namespaces |
Raymond Hettinger | f755432 | 2005-08-21 12:35:29 +0000 | [diff] [blame] | 268 | as well as nested namespaces in objects (in methods). Namespaces support |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 269 | modularity by preventing naming conflicts. For instance, the |
| 270 | functions \function{__builtin__.open()} and \function{os.open()} are |
| 271 | distinguished by their namespaces. Namespaces also aid readability |
Raymond Hettinger | f755432 | 2005-08-21 12:35:29 +0000 | [diff] [blame] | 272 | and maintainability by making it clear which module implements a |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 273 | function. For instance, writing \function{random.seed()} or |
| 274 | {}\function{itertools.izip()} makes it clear that those functions are |
Fred Drake | 984920b | 2003-09-28 19:03:36 +0000 | [diff] [blame] | 275 | implemented by the \ulink{\module{random}}{../lib/module-random.html} |
| 276 | and \ulink{\module{itertools}}{../lib/module-itertools.html} modules |
| 277 | respectively. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 278 | |
| 279 | \index{nested scope} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 280 | \item[nested scope] |
| 281 | The ability to refer to a variable in an enclosing definition. For |
| 282 | instance, a function defined inside another function can refer to |
| 283 | variables in the outer function. Note that nested scopes work only |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 284 | for reference and not for assignment which will always write to the |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 285 | innermost scope. In contrast, local variables both read and write in |
| 286 | the innermost scope. Likewise, global variables read and write to the |
| 287 | global namespace. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 288 | |
| 289 | \index{new-style class} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 290 | \item[new-style class] |
| 291 | Any class that inherits from \class{object}. This includes all |
| 292 | built-in types like \class{list} and \class{dict}. Only new-style |
| 293 | classes can use Python's newer, versatile features like |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 294 | {}\method{__slots__}, descriptors, properties, |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 295 | \method{__getattribute__()}, class methods, and static methods. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 296 | |
| 297 | \index{Python3000} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 298 | \item[Python3000] |
Raymond Hettinger | f755432 | 2005-08-21 12:35:29 +0000 | [diff] [blame] | 299 | A mythical python release, not required to be backward compatible, with |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 300 | telepathic interface. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 301 | |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 302 | \index{__slots__} |
| 303 | \item[__slots__] |
| 304 | A declaration inside a \emph{new-style class} that saves memory by |
| 305 | pre-declaring space for instance attributes and eliminating instance |
| 306 | dictionaries. Though popular, the technique is somewhat tricky to get |
| 307 | right and is best reserved for rare cases where there are large |
Raymond Hettinger | 65a350d | 2004-12-02 07:29:43 +0000 | [diff] [blame] | 308 | numbers of instances in a memory-critical application. |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 309 | |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 310 | \index{sequence} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 311 | \item[sequence] |
| 312 | An \emph{iterable} which supports efficient element access using |
| 313 | integer indices via the \method{__getitem__()} and |
| 314 | {}\method{__len__()} special methods. Some built-in sequence types |
| 315 | are \class{list}, \class{str}, \class{tuple}, and \class{unicode}. |
| 316 | Note that \class{dict} also supports \method{__getitem__()} and |
| 317 | {}\method{__len__()}, but is considered a mapping rather than a |
| 318 | sequence because the lookups use arbitrary \emph{immutable} keys |
| 319 | rather than integers. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 320 | |
| 321 | \index{Zen of Python} |
Fred Drake | d4a1419 | 2003-09-27 18:59:43 +0000 | [diff] [blame] | 322 | \item[Zen of Python] |
| 323 | Listing of Python design principles and philosophies that are helpful |
| 324 | in understanding and using the language. The listing can be found by |
Raymond Hettinger | 43b5e40 | 2003-09-27 20:19:02 +0000 | [diff] [blame] | 325 | typing ``\code{import this}'' at the interactive prompt. |
Skip Montanaro | 757dedc | 2003-09-24 16:51:23 +0000 | [diff] [blame] | 326 | |
| 327 | \end{description} |