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