blob: a19416b69b16bb698ec77aae6d2059664992a71a [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{>>>}
Thomas Wouters477c8d52006-05-27 19:21:47 +000010\item[\code{>>>}]
Fred Draked4a14192003-09-27 18:59:43 +000011The 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.
Raymond Hettingerf7554322005-08-21 12:35:29 +000027The byte code is also cached in \code{.pyc} and \code{.pyo}
Fred Draked4a14192003-09-27 18:59:43 +000028files so that executing the same file is faster the second time
Raymond Hettingerf7554322005-08-21 12:35:29 +000029(recompilation from source to byte code can be avoided). 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
Skip Montanarodbb40782004-03-27 18:23:11 +000038\index{complex number}
39\item[complex number]
Skip Montanarodbb40782004-03-27 18:23:11 +000040An extension of the familiar real number system in which all numbers are
41expressed as a sum of a real part and an imaginary part. Imaginary numbers
42are real multiples of the imaginary unit (the square root of {}\code{-1}),
43often written {}\code{i} in mathematics or {}\code{j} in engineering.
44Python has builtin support for complex numbers, which are written with this
45latter notation; the imaginary part is written with a {}\code{j} suffix,
46e.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. Kuchlingaeaec8d2004-03-29 01:19:54 +000048fairly advanced mathematical feature. If you're not aware of a need for them,
Raymond Hettingerf13c0242004-03-28 22:44:09 +000049it's almost certain you can safely ignore them.
Skip Montanaro757dedc2003-09-24 16:51:23 +000050
51\index{descriptor}
Fred Draked4a14192003-09-27 18:59:43 +000052\item[descriptor]
53Any \emph{new-style} object that defines the methods
54{}\method{__get__()}, \method{__set__()}, or \method{__delete__()}.
55When a class attribute is a descriptor, its special binding behavior
56is triggered upon attribute lookup. Normally, writing \var{a.b} looks
57up the object \var{b} in the class dictionary for \var{a}, but if
58{}\var{b} is a descriptor, the defined method gets called.
59Understanding descriptors is a key to a deep understanding of Python
60because they are the basis for many features including functions,
61methods, properties, class methods, static methods, and reference to
62super classes.
Skip Montanaro757dedc2003-09-24 16:51:23 +000063
64\index{dictionary}
Fred Draked4a14192003-09-27 18:59:43 +000065\item[dictionary]
66An associative array, where arbitrary keys are mapped to values. The
67use of \class{dict} much resembles that for \class{list}, but the keys
68can be any object with a \method{__hash__()} function, not just
69integers starting from zero. Called a hash in Perl.
Skip Montanaro757dedc2003-09-24 16:51:23 +000070
Raymond Hettingerd4f5b072005-01-11 16:11:13 +000071\index{duck-typing}
Raymond Hettinger0f439832005-05-14 17:18:31 +000072\item[duck-typing]
Raymond Hettingerd4f5b072005-01-11 16:11:13 +000073Pythonic programming style that determines an object's type by inspection
74of its method or attribute signature rather than by explicit relationship
75to some type object ("If it looks like a duck and quacks like a duck, it
76must be a duck.") By emphasizing interfaces rather than specific types,
77well-designed code improves its flexibility by allowing polymorphic
78substitution. 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 Montanaro757dedc2003-09-24 16:51:23 +000082\index{EAFP}
Fred Draked4a14192003-09-27 18:59:43 +000083\item[EAFP]
84Easier to ask for forgiveness than permission. This common Python
85coding style assumes the existence of valid keys or attributes and
86catches exceptions if the assumption proves false. This clean and
87fast 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 Montanaro757dedc2003-09-24 16:51:23 +000090
91\index{__future__}
Fred Draked4a14192003-09-27 18:59:43 +000092\item[__future__]
93A pseudo module which programmers can use to enable new language
Neal Norwitz4886cc32006-08-21 17:06:07 +000094features which are not compatible with the current interpreter.
95To enable \code{new_feature}
Skip Montanaro757dedc2003-09-24 16:51:23 +000096
97\begin{verbatim}
Neal Norwitz4886cc32006-08-21 17:06:07 +000098from __future__ import new_feature
Skip Montanaro757dedc2003-09-24 16:51:23 +000099\end{verbatim}
100
Neal Norwitz4886cc32006-08-21 17:06:07 +0000101By importing the \ulink{\module{__future__}}{../lib/module-future.html}
Fred Drake984920b2003-09-28 19:03:36 +0000102module and evaluating its variables, you can see when a new feature
103was first added to the language and when it will become the default:
Skip Montanaro757dedc2003-09-24 16:51:23 +0000104
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 Draked4a14192003-09-27 18:59:43 +0000112\item[generator]
Skip Montanarodbb40782004-03-27 18:23:11 +0000113A function that returns an iterator. It looks like a normal function except
114that values are returned to the caller using a \keyword{yield} statement
115instead of a {}\keyword{return} statement. Generator functions often
116contain one or more {}\keyword{for} or \keyword{while} loops that
117\keyword{yield} elements back to the caller. The function execution is
118stopped at the {}\keyword{yield} keyword (returning the result) and is
119resumed there when the next element is requested by calling the
Georg Brandla18af4e2007-04-21 15:47:16 +0000120\method{__next__()} method of the returned iterator.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000121
Raymond Hettingerd3481932004-06-07 21:52:47 +0000122\index{generator expression}
123\item[generator expression]
124An expression that returns a generator. It looks like a normal expression
125followed by a \keyword{for} expression defining a loop variable, range, and
126an optional \keyword{if} expression. The combined expression generates
127values for an enclosing function:
128
129\begin{verbatim}
130>>> sum(i*i for i in range(10)) # sum of squares 0, 1, 4, ... 81
131285
132\end{verbatim}
133
Skip Montanaro757dedc2003-09-24 16:51:23 +0000134\index{GIL}
Fred Draked4a14192003-09-27 18:59:43 +0000135\item[GIL]
136See \emph{global interpreter lock}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000137
138\index{global interpreter lock}
Fred Draked4a14192003-09-27 18:59:43 +0000139\item[global interpreter lock]
140The lock used by Python threads to assure that only one thread can be
141run at a time. This simplifies Python by assuring that no two
142processes can access the same memory at the same time. Locking the
143entire interpreter makes it easier for the interpreter to be
Skip Montanaro757dedc2003-09-24 16:51:23 +0000144multi-threaded, at the expense of some parallelism on multi-processor
Fred Draked4a14192003-09-27 18:59:43 +0000145machines. Efforts have been made in the past to create a
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000146``free-threaded'' interpreter (one which locks shared data at a much
Fred Draked4a14192003-09-27 18:59:43 +0000147finer granularity), but performance suffered in the common
148single-processor case.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000149
150\index{IDLE}
Fred Draked4a14192003-09-27 18:59:43 +0000151\item[IDLE]
152An Integrated Development Environment for Python. IDLE is a
Raymond Hettinger5a25aa62003-09-27 05:42:14 +0000153basic editor and interpreter environment that ships with the standard
154distribution of Python. Good for beginners, it also serves as clear
155example code for those wanting to implement a moderately
Fred Draked4a14192003-09-27 18:59:43 +0000156sophisticated, multi-platform GUI application.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000157
158\index{immutable}
Fred Draked4a14192003-09-27 18:59:43 +0000159\item[immutable]
Skip Montanarodbb40782004-03-27 18:23:11 +0000160An object with fixed value. Immutable objects are numbers, strings or
Fred Draked4a14192003-09-27 18:59:43 +0000161tuples (and more). Such an object cannot be altered. A new object
Skip Montanaro757dedc2003-09-24 16:51:23 +0000162has to be created if a different value has to be stored. They play an
Raymond Hettinger65a350d2004-12-02 07:29:43 +0000163important role in places where a constant hash value is needed, for
Fred Draked4a14192003-09-27 18:59:43 +0000164example as a key in a dictionary.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000165
166\index{integer division}
Fred Draked4a14192003-09-27 18:59:43 +0000167\item[integer division]
Neal Norwitz4886cc32006-08-21 17:06:07 +0000168Mathematical division including any remainder. The result will always
169be a float. For example, the expression \code{11/4} evaluates to \code{2.75}.
170Integer division can be forced by using the \code{//} operator instead
171of the \code{/} operator.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000172
173\index{interactive}
Fred Draked4a14192003-09-27 18:59:43 +0000174\item[interactive]
175Python has an interactive interpreter which means that you can try out
Raymond Hettinger65a350d2004-12-02 07:29:43 +0000176things and immediately see their results. Just launch \code{python} with no
Fred Draked4a14192003-09-27 18:59:43 +0000177arguments (possibly by selecting it from your computer's main menu).
178It is a very powerful way to test out new ideas or inspect modules and
179packages (remember \code{help(x)}).
Skip Montanaro757dedc2003-09-24 16:51:23 +0000180
181\index{interpreted}
Fred Draked4a14192003-09-27 18:59:43 +0000182\item[interpreted]
Skip Montanarodbb40782004-03-27 18:23:11 +0000183Python is an interpreted language, as opposed to a compiled one. This means
184that the source files can be run directly without first creating an
185executable which is then run. Interpreted languages typically have a
186shorter development/debug cycle than compiled ones, though their programs
187generally also run more slowly. See also {}\emph{interactive}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000188
189\index{iterable}
Fred Draked4a14192003-09-27 18:59:43 +0000190\item[iterable]
191A container object capable of returning its members one at a time.
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000192Examples of iterables include all sequence types (such as \class{list},
Raymond Hettinger5a25aa62003-09-27 05:42:14 +0000193{}\class{str}, and \class{tuple}) and some non-sequence types like
Fred Draked4a14192003-09-27 18:59:43 +0000194{}\class{dict} and \class{file} and objects of any classes you define
195with an \method{__iter__()} or \method{__getitem__()} method. Iterables
196can be used in a \keyword{for} loop and in many other places where a
197sequence is needed (\function{zip()}, \function{map()}, ...). When an
198iterable object is passed as an argument to the builtin function
199{}\function{iter()}, it returns an iterator for the object. This
200iterator is good for one pass over the set of values. When using
201iterables, it is usually not necessary to call \function{iter()} or
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000202deal with iterator objects yourself. The \code{for} statement does
Fred Draked4a14192003-09-27 18:59:43 +0000203that automatically for you, creating a temporary unnamed variable to
204hold the iterator for the duration of the loop. See also
205{}\emph{iterator}, \emph{sequence}, and \emph{generator}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000206
207\index{iterator}
Fred Draked4a14192003-09-27 18:59:43 +0000208\item[iterator]
209An object representing a stream of data. Repeated calls to the
Georg Brandla18af4e2007-04-21 15:47:16 +0000210iterator's \method{__next__()} method return successive items in the
Skip Montanaro757dedc2003-09-24 16:51:23 +0000211stream. When no more data is available a \exception{StopIteration}
Fred Draked4a14192003-09-27 18:59:43 +0000212exception is raised instead. At this point, the iterator object is
Georg Brandla18af4e2007-04-21 15:47:16 +0000213exhausted and any further calls to its \method{__next__()} method just
Fred Draked4a14192003-09-27 18:59:43 +0000214raise \exception{StopIteration} again. Iterators are required to have
215an \method{__iter__()} method that returns the iterator object
216itself so every iterator is also iterable and may be used in most
217places where other iterables are accepted. One notable exception is
218code that attempts multiple iteration passes. A container object
219(such as a \class{list}) produces a fresh new iterator each time you
220pass it to the \function{iter()} function or use it in a
221{}\keyword{for} loop. Attempting this with an iterator will just
Raymond Hettinger65a350d2004-12-02 07:29:43 +0000222return the same exhausted iterator object used in the previous iteration
Fred Draked4a14192003-09-27 18:59:43 +0000223pass, making it appear like an empty container.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000224
Raymond Hettingerf7554322005-08-21 12:35:29 +0000225\index{LBYL}
226\item[LBYL]
227Look before you leap. This coding style explicitly tests for
228pre-conditions before making calls or lookups. This style contrasts
229with the \emph{EAFP} approach and is characterized by the presence of
230many \keyword{if} statements.
231
Skip Montanaro757dedc2003-09-24 16:51:23 +0000232\index{list comprehension}
Fred Draked4a14192003-09-27 18:59:43 +0000233\item[list comprehension]
234A compact way to process all or a subset of elements in a sequence and
235return a list with the results. \code{result = ["0x\%02x"
Skip Montanaro757dedc2003-09-24 16:51:23 +0000236\% x for x in range(256) if x \% 2 == 0]} generates a list of strings
237containing hex numbers (0x..) that are even and in the range from 0 to 255.
238The \keyword{if} clause is optional. If omitted, all elements in
Raymond Hettinger65a350d2004-12-02 07:29:43 +0000239{}\code{range(256)} are processed.
240
Skip Montanaro757dedc2003-09-24 16:51:23 +0000241\index{mapping}
Fred Draked4a14192003-09-27 18:59:43 +0000242\item[mapping]
243A container object (such as \class{dict}) that supports arbitrary key
244lookups using the special method \method{__getitem__()}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000245
246\index{metaclass}
Fred Draked4a14192003-09-27 18:59:43 +0000247\item[metaclass]
248The class of a class. Class definitions create a class name, a class
249dictionary, and a list of base classes. The metaclass is responsible
250for taking those three arguments and creating the class. Most object
251oriented programming languages provide a default implementation. What
252makes Python special is that it is possible to create custom
253metaclasses. Most users never need this tool, but when the need
254arises, metaclasses can provide powerful, elegant solutions. They
255have been used for logging attribute access, adding thread-safety,
256tracking object creation, implementing singletons, and many other
257tasks.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000258
Skip Montanaro757dedc2003-09-24 16:51:23 +0000259\index{mutable}
Fred Draked4a14192003-09-27 18:59:43 +0000260\item[mutable]
261Mutable objects can change their value but keep their \function{id()}.
262See also \emph{immutable}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000263
264\index{namespace}
Fred Draked4a14192003-09-27 18:59:43 +0000265\item[namespace]
266The place where a variable is stored. Namespaces are implemented as
Raymond Hettinger65a350d2004-12-02 07:29:43 +0000267dictionaries. There are the local, global and builtin namespaces
Raymond Hettingerf7554322005-08-21 12:35:29 +0000268as well as nested namespaces in objects (in methods). Namespaces support
Fred Draked4a14192003-09-27 18:59:43 +0000269modularity by preventing naming conflicts. For instance, the
270functions \function{__builtin__.open()} and \function{os.open()} are
271distinguished by their namespaces. Namespaces also aid readability
Raymond Hettingerf7554322005-08-21 12:35:29 +0000272and maintainability by making it clear which module implements a
Fred Draked4a14192003-09-27 18:59:43 +0000273function. For instance, writing \function{random.seed()} or
274{}\function{itertools.izip()} makes it clear that those functions are
Fred Drake984920b2003-09-28 19:03:36 +0000275implemented by the \ulink{\module{random}}{../lib/module-random.html}
276and \ulink{\module{itertools}}{../lib/module-itertools.html} modules
277respectively.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000278
279\index{nested scope}
Fred Draked4a14192003-09-27 18:59:43 +0000280\item[nested scope]
281The ability to refer to a variable in an enclosing definition. For
282instance, a function defined inside another function can refer to
283variables in the outer function. Note that nested scopes work only
Skip Montanaro757dedc2003-09-24 16:51:23 +0000284for reference and not for assignment which will always write to the
Fred Draked4a14192003-09-27 18:59:43 +0000285innermost scope. In contrast, local variables both read and write in
286the innermost scope. Likewise, global variables read and write to the
287global namespace.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000288
289\index{new-style class}
Fred Draked4a14192003-09-27 18:59:43 +0000290\item[new-style class]
291Any class that inherits from \class{object}. This includes all
292built-in types like \class{list} and \class{dict}. Only new-style
293classes can use Python's newer, versatile features like
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000294{}\method{__slots__}, descriptors, properties,
Fred Draked4a14192003-09-27 18:59:43 +0000295\method{__getattribute__()}, class methods, and static methods.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000296
297\index{Python3000}
Fred Draked4a14192003-09-27 18:59:43 +0000298\item[Python3000]
Raymond Hettingerf7554322005-08-21 12:35:29 +0000299A mythical python release, not required to be backward compatible, with
Fred Draked4a14192003-09-27 18:59:43 +0000300telepathic interface.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000301
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000302\index{__slots__}
303\item[__slots__]
304A declaration inside a \emph{new-style class} that saves memory by
305pre-declaring space for instance attributes and eliminating instance
306dictionaries. Though popular, the technique is somewhat tricky to get
307right and is best reserved for rare cases where there are large
Raymond Hettinger65a350d2004-12-02 07:29:43 +0000308numbers of instances in a memory-critical application.
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000309
Skip Montanaro757dedc2003-09-24 16:51:23 +0000310\index{sequence}
Fred Draked4a14192003-09-27 18:59:43 +0000311\item[sequence]
312An \emph{iterable} which supports efficient element access using
313integer indices via the \method{__getitem__()} and
314{}\method{__len__()} special methods. Some built-in sequence types
315are \class{list}, \class{str}, \class{tuple}, and \class{unicode}.
316Note that \class{dict} also supports \method{__getitem__()} and
317{}\method{__len__()}, but is considered a mapping rather than a
318sequence because the lookups use arbitrary \emph{immutable} keys
319rather than integers.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000320
321\index{Zen of Python}
Fred Draked4a14192003-09-27 18:59:43 +0000322\item[Zen of Python]
323Listing of Python design principles and philosophies that are helpful
324in understanding and using the language. The listing can be found by
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000325typing ``\code{import this}'' at the interactive prompt.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000326
327\end{description}