blob: 38b46091bebfc47f02d3f245dc8ec5f818208651 [file] [log] [blame]
Skip Montanaro757dedc2003-09-24 16:51:23 +00001\chapter{Glossary\label{glossary}}
2
3%%% keep the entries sorted and include at least one \index{} item for each
Raymond Hettinger5a25aa62003-09-27 05:42:14 +00004%%% cross-references are marked with \emph{entry}
Skip Montanaro757dedc2003-09-24 16:51:23 +00005
6\begin{description}
7
Skip Montanaro757dedc2003-09-24 16:51:23 +00008
9\index{>>>}
Fred Draked4a14192003-09-27 18:59:43 +000010\item[\code{>\code{>}>}]
11The typical Python prompt of the interactive shell. Often seen for
12code examples that can be tried right away in the interpreter.
Skip Montanaro757dedc2003-09-24 16:51:23 +000013
Raymond Hettinger43b5e402003-09-27 20:19:02 +000014\index{...}
15\item[\code{.\code{.}.}]
16The typical Python prompt of the interactive shell when entering code
17for an indented code block.
Skip Montanaro757dedc2003-09-24 16:51:23 +000018
19\index{BDFL}
Fred Draked4a14192003-09-27 18:59:43 +000020\item[BDFL]
21Benevolent Dictator For Life, a.k.a. \ulink{Guido van
Raymond Hettinger43b5e402003-09-27 20:19:02 +000022Rossum}{http://www.python.org/\textasciitilde{}guido/}, Python's creator.
Skip Montanaro757dedc2003-09-24 16:51:23 +000023
24\index{byte code}
Fred Draked4a14192003-09-27 18:59:43 +000025\item[byte code]
26The internal representation of a Python program in the interpreter.
27The byte code is also cached in the \code{.pyc} and \code{.pyo}
28files so that executing the same file is faster the second time
29(compilation from source to byte code can be saved). This
Raymond Hettinger43b5e402003-09-27 20:19:02 +000030``intermediate language'' is said to run on a ``virtual
31machine'' that calls the subroutines corresponding to each bytecode.
Skip Montanaro757dedc2003-09-24 16:51:23 +000032
33\index{classic class}
Fred Draked4a14192003-09-27 18:59:43 +000034\item[classic class]
35Any class which does not inherit from \class{object}. See
36\emph{new-style class}.
Skip Montanaro757dedc2003-09-24 16:51:23 +000037
38\index{coercion}
Fred Draked4a14192003-09-27 18:59:43 +000039\item[coercion]
Skip Montanarodbb40782004-03-27 18:23:11 +000040
41The implicit conversion of an instance of one type to another during an
42operation which involves two arguments of the same type. For example,
43{}\code{int(3.15)} converts the floating point number to the integer,
44{}\code{3}, but in {}\code{3+4.5}, each argument is of a different type (one
45int, one float), and both must be converted to the same type before they can
46be added or it will raise a {}\code{TypeError}. Coercion between two
47operands can be performed with the {}\code{coerce} builtin function; thus,
48{}\code{3+4.5} is equivalent to calling {}\code{operator.add(*coerce(3,
494.5))} and results in {}\code{operator.add(3.0, 4.5)}. Without coercion,
50all arguments of even compatible types would have to be normalized to the
51same value by the programmer, e.g., {}\code{float(3)+4.5} rather than just
52{}\code{3+4.5}.
53
54\index{complex number}
55\item[complex number]
56
57An extension of the familiar real number system in which all numbers are
58expressed as a sum of a real part and an imaginary part. Imaginary numbers
59are real multiples of the imaginary unit (the square root of {}\code{-1}),
60often written {}\code{i} in mathematics or {}\code{j} in engineering.
61Python has builtin support for complex numbers, which are written with this
62latter notation; the imaginary part is written with a {}\code{j} suffix,
63e.g., {}\code{3+1j}. To get access to complex equivalents of the
64{}\module{math} module, use {}\module{cmath}. Use of complex numbers is a
Andrew M. Kuchlingaeaec8d2004-03-29 01:19:54 +000065fairly advanced mathematical feature. If you're not aware of a need for them,
Raymond Hettingerf13c0242004-03-28 22:44:09 +000066it's almost certain you can safely ignore them.
Skip Montanaro757dedc2003-09-24 16:51:23 +000067
68\index{descriptor}
Fred Draked4a14192003-09-27 18:59:43 +000069\item[descriptor]
70Any \emph{new-style} object that defines the methods
71{}\method{__get__()}, \method{__set__()}, or \method{__delete__()}.
72When a class attribute is a descriptor, its special binding behavior
73is triggered upon attribute lookup. Normally, writing \var{a.b} looks
74up the object \var{b} in the class dictionary for \var{a}, but if
75{}\var{b} is a descriptor, the defined method gets called.
76Understanding descriptors is a key to a deep understanding of Python
77because they are the basis for many features including functions,
78methods, properties, class methods, static methods, and reference to
79super classes.
Skip Montanaro757dedc2003-09-24 16:51:23 +000080
81\index{dictionary}
Fred Draked4a14192003-09-27 18:59:43 +000082\item[dictionary]
83An associative array, where arbitrary keys are mapped to values. The
84use of \class{dict} much resembles that for \class{list}, but the keys
85can be any object with a \method{__hash__()} function, not just
86integers starting from zero. Called a hash in Perl.
Skip Montanaro757dedc2003-09-24 16:51:23 +000087
88\index{EAFP}
Fred Draked4a14192003-09-27 18:59:43 +000089\item[EAFP]
90Easier to ask for forgiveness than permission. This common Python
91coding style assumes the existence of valid keys or attributes and
92catches exceptions if the assumption proves false. This clean and
93fast style is characterized by the presence of many \keyword{try} and
94{}\keyword{except} statements. The technique contrasts with the
95{}\emph{LBYL} style that is common in many other languages such as C.
Skip Montanaro757dedc2003-09-24 16:51:23 +000096
97\index{__future__}
Fred Draked4a14192003-09-27 18:59:43 +000098\item[__future__]
99A pseudo module which programmers can use to enable new language
100features which are not compatible with the current interpreter. For
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000101example, the expression \code{11/4} currently evaluates to \code{2}.
102If the module in which it is executed had enabled \emph{true division}
Fred Draked4a14192003-09-27 18:59:43 +0000103by executing:
Skip Montanaro757dedc2003-09-24 16:51:23 +0000104
105\begin{verbatim}
106from __future__ import division
107\end{verbatim}
108
Fred Drake984920b2003-09-28 19:03:36 +0000109the expression \code{11/4} would evaluate to \code{2.75}. By actually
110importing the \ulink{\module{__future__}}{../lib/module-future.html}
111module and evaluating its variables, you can see when a new feature
112was first added to the language and when it will become the default:
Skip Montanaro757dedc2003-09-24 16:51:23 +0000113
114\begin{verbatim}
115>>> import __future__
116>>> __future__.division
117_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
118\end{verbatim}
119
120\index{generator}
Fred Draked4a14192003-09-27 18:59:43 +0000121\item[generator]
Skip Montanarodbb40782004-03-27 18:23:11 +0000122A function that returns an iterator. It looks like a normal function except
123that values are returned to the caller using a \keyword{yield} statement
124instead of a {}\keyword{return} statement. Generator functions often
125contain one or more {}\keyword{for} or \keyword{while} loops that
126\keyword{yield} elements back to the caller. The function execution is
127stopped at the {}\keyword{yield} keyword (returning the result) and is
128resumed there when the next element is requested by calling the
129\method{next()} method of the returned iterator.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000130
131\index{GIL}
Fred Draked4a14192003-09-27 18:59:43 +0000132\item[GIL]
133See \emph{global interpreter lock}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000134
135\index{global interpreter lock}
Fred Draked4a14192003-09-27 18:59:43 +0000136\item[global interpreter lock]
137The lock used by Python threads to assure that only one thread can be
138run at a time. This simplifies Python by assuring that no two
139processes can access the same memory at the same time. Locking the
140entire interpreter makes it easier for the interpreter to be
Skip Montanaro757dedc2003-09-24 16:51:23 +0000141multi-threaded, at the expense of some parallelism on multi-processor
Fred Draked4a14192003-09-27 18:59:43 +0000142machines. Efforts have been made in the past to create a
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000143``free-threaded'' interpreter (one which locks shared data at a much
Fred Draked4a14192003-09-27 18:59:43 +0000144finer granularity), but performance suffered in the common
145single-processor case.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000146
147\index{IDLE}
Fred Draked4a14192003-09-27 18:59:43 +0000148\item[IDLE]
149An Integrated Development Environment for Python. IDLE is a
Raymond Hettinger5a25aa62003-09-27 05:42:14 +0000150basic editor and interpreter environment that ships with the standard
151distribution of Python. Good for beginners, it also serves as clear
152example code for those wanting to implement a moderately
Fred Draked4a14192003-09-27 18:59:43 +0000153sophisticated, multi-platform GUI application.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000154
155\index{immutable}
Fred Draked4a14192003-09-27 18:59:43 +0000156\item[immutable]
Skip Montanarodbb40782004-03-27 18:23:11 +0000157An object with fixed value. Immutable objects are numbers, strings or
Fred Draked4a14192003-09-27 18:59:43 +0000158tuples (and more). Such an object cannot be altered. A new object
Skip Montanaro757dedc2003-09-24 16:51:23 +0000159has to be created if a different value has to be stored. They play an
Fred Draked4a14192003-09-27 18:59:43 +0000160important role in places where a constant hash value is needed. For
161example as a key in a dictionary.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000162
163\index{integer division}
Fred Draked4a14192003-09-27 18:59:43 +0000164\item[integer division]
165Mathematical division discarding any remainder. For example, the
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000166expression \code{11/4} currently evaluates to \code{2} in contrast
Fred Draked4a14192003-09-27 18:59:43 +0000167to the \code{2.75} returned by float division. Also called
168{}\emph{floor division}. When dividing two integers the outcome will
169always be another integer (having the floor function applied to it).
170However, if one of the operands is another numeric type (such as a
171{}\class{float}), the result will be coerced (see \emph{coercion}) to
Skip Montanarodbb40782004-03-27 18:23:11 +0000172a common type. For example, an integer divided by a float will result
Fred Draked4a14192003-09-27 18:59:43 +0000173in a float value, possibly with a decimal fraction. Integer division
174can be forced by using the \code{//} operator instead of the \code{/}
175operator. See also \emph{__future__}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000176
177\index{interactive}
Fred Draked4a14192003-09-27 18:59:43 +0000178\item[interactive]
179Python has an interactive interpreter which means that you can try out
180things and directly see its result. Just launch \code{python} with no
181arguments (possibly by selecting it from your computer's main menu).
182It is a very powerful way to test out new ideas or inspect modules and
183packages (remember \code{help(x)}).
Skip Montanaro757dedc2003-09-24 16:51:23 +0000184
185\index{interpreted}
Fred Draked4a14192003-09-27 18:59:43 +0000186\item[interpreted]
Skip Montanarodbb40782004-03-27 18:23:11 +0000187Python is an interpreted language, as opposed to a compiled one. This means
188that the source files can be run directly without first creating an
189executable which is then run. Interpreted languages typically have a
190shorter development/debug cycle than compiled ones, though their programs
191generally also run more slowly. See also {}\emph{interactive}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000192
193\index{iterable}
Fred Draked4a14192003-09-27 18:59:43 +0000194\item[iterable]
195A container object capable of returning its members one at a time.
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000196Examples of iterables include all sequence types (such as \class{list},
Raymond Hettinger5a25aa62003-09-27 05:42:14 +0000197{}\class{str}, and \class{tuple}) and some non-sequence types like
Fred Draked4a14192003-09-27 18:59:43 +0000198{}\class{dict} and \class{file} and objects of any classes you define
199with an \method{__iter__()} or \method{__getitem__()} method. Iterables
200can be used in a \keyword{for} loop and in many other places where a
201sequence is needed (\function{zip()}, \function{map()}, ...). When an
202iterable object is passed as an argument to the builtin function
203{}\function{iter()}, it returns an iterator for the object. This
204iterator is good for one pass over the set of values. When using
205iterables, it is usually not necessary to call \function{iter()} or
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000206deal with iterator objects yourself. The \code{for} statement does
Fred Draked4a14192003-09-27 18:59:43 +0000207that automatically for you, creating a temporary unnamed variable to
208hold the iterator for the duration of the loop. See also
209{}\emph{iterator}, \emph{sequence}, and \emph{generator}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000210
211\index{iterator}
Fred Draked4a14192003-09-27 18:59:43 +0000212\item[iterator]
213An object representing a stream of data. Repeated calls to the
214iterator's \method{next()} method return successive items in the
Skip Montanaro757dedc2003-09-24 16:51:23 +0000215stream. When no more data is available a \exception{StopIteration}
Fred Draked4a14192003-09-27 18:59:43 +0000216exception is raised instead. At this point, the iterator object is
217exhausted and any further calls to its \method{next()} method just
218raise \exception{StopIteration} again. Iterators are required to have
219an \method{__iter__()} method that returns the iterator object
220itself so every iterator is also iterable and may be used in most
221places where other iterables are accepted. One notable exception is
222code that attempts multiple iteration passes. A container object
223(such as a \class{list}) produces a fresh new iterator each time you
224pass it to the \function{iter()} function or use it in a
225{}\keyword{for} loop. Attempting this with an iterator will just
226return the same exhausted iterator object from the second iteration
227pass, making it appear like an empty container.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000228
229\index{list comprehension}
Fred Draked4a14192003-09-27 18:59:43 +0000230\item[list comprehension]
231A compact way to process all or a subset of elements in a sequence and
232return a list with the results. \code{result = ["0x\%02x"
Skip Montanaro757dedc2003-09-24 16:51:23 +0000233\% x for x in range(256) if x \% 2 == 0]} generates a list of strings
234containing hex numbers (0x..) that are even and in the range from 0 to 255.
235The \keyword{if} clause is optional. If omitted, all elements in
Fred Draked4a14192003-09-27 18:59:43 +0000236{}\code{range(256)} are processed in that case.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000237
238\index{mapping}
Fred Draked4a14192003-09-27 18:59:43 +0000239\item[mapping]
240A container object (such as \class{dict}) that supports arbitrary key
241lookups using the special method \method{__getitem__()}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000242
243\index{metaclass}
Fred Draked4a14192003-09-27 18:59:43 +0000244\item[metaclass]
245The class of a class. Class definitions create a class name, a class
246dictionary, and a list of base classes. The metaclass is responsible
247for taking those three arguments and creating the class. Most object
248oriented programming languages provide a default implementation. What
249makes Python special is that it is possible to create custom
250metaclasses. Most users never need this tool, but when the need
251arises, metaclasses can provide powerful, elegant solutions. They
252have been used for logging attribute access, adding thread-safety,
253tracking object creation, implementing singletons, and many other
254tasks.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000255
256\index{LBYL}
Fred Draked4a14192003-09-27 18:59:43 +0000257\item[LBYL]
258Look before you leap. This coding style explicitly tests for
259pre-conditions before making calls or lookups. This style contrasts
260with the \emph{EAFP} approach and is characterized the presence of
261many \keyword{if} statements.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000262
263\index{mutable}
Fred Draked4a14192003-09-27 18:59:43 +0000264\item[mutable]
265Mutable objects can change their value but keep their \function{id()}.
266See also \emph{immutable}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000267
268\index{namespace}
Fred Draked4a14192003-09-27 18:59:43 +0000269\item[namespace]
270The place where a variable is stored. Namespaces are implemented as
271dictionary. There is the local, global and builtins namespace and the
272nested namespaces in objects (in methods). Namespaces support
273modularity by preventing naming conflicts. For instance, the
274functions \function{__builtin__.open()} and \function{os.open()} are
275distinguished by their namespaces. Namespaces also aid readability
276and maintainability by making it clear which modules implement a
277function. For instance, writing \function{random.seed()} or
278{}\function{itertools.izip()} makes it clear that those functions are
Fred Drake984920b2003-09-28 19:03:36 +0000279implemented by the \ulink{\module{random}}{../lib/module-random.html}
280and \ulink{\module{itertools}}{../lib/module-itertools.html} modules
281respectively.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000282
283\index{nested scope}
Fred Draked4a14192003-09-27 18:59:43 +0000284\item[nested scope]
285The ability to refer to a variable in an enclosing definition. For
286instance, a function defined inside another function can refer to
287variables in the outer function. Note that nested scopes work only
Skip Montanaro757dedc2003-09-24 16:51:23 +0000288for reference and not for assignment which will always write to the
Fred Draked4a14192003-09-27 18:59:43 +0000289innermost scope. In contrast, local variables both read and write in
290the innermost scope. Likewise, global variables read and write to the
291global namespace.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000292
293\index{new-style class}
Fred Draked4a14192003-09-27 18:59:43 +0000294\item[new-style class]
295Any class that inherits from \class{object}. This includes all
296built-in types like \class{list} and \class{dict}. Only new-style
297classes can use Python's newer, versatile features like
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000298{}\method{__slots__}, descriptors, properties,
Fred Draked4a14192003-09-27 18:59:43 +0000299\method{__getattribute__()}, class methods, and static methods.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000300
301\index{Python3000}
Fred Draked4a14192003-09-27 18:59:43 +0000302\item[Python3000]
303A mythical python release, allowed not to be backward compatible, with
304telepathic interface.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000305
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000306\index{__slots__}
307\item[__slots__]
308A declaration inside a \emph{new-style class} that saves memory by
309pre-declaring space for instance attributes and eliminating instance
310dictionaries. Though popular, the technique is somewhat tricky to get
311right and is best reserved for rare cases where there are large
312numbers of instances in a memory critical application.
313
Skip Montanaro757dedc2003-09-24 16:51:23 +0000314\index{sequence}
Fred Draked4a14192003-09-27 18:59:43 +0000315\item[sequence]
316An \emph{iterable} which supports efficient element access using
317integer indices via the \method{__getitem__()} and
318{}\method{__len__()} special methods. Some built-in sequence types
319are \class{list}, \class{str}, \class{tuple}, and \class{unicode}.
320Note that \class{dict} also supports \method{__getitem__()} and
321{}\method{__len__()}, but is considered a mapping rather than a
322sequence because the lookups use arbitrary \emph{immutable} keys
323rather than integers.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000324
325\index{Zen of Python}
Fred Draked4a14192003-09-27 18:59:43 +0000326\item[Zen of Python]
327Listing of Python design principles and philosophies that are helpful
328in understanding and using the language. The listing can be found by
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000329typing ``\code{import this}'' at the interactive prompt.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000330
331\end{description}