blob: 4702d6700f96aaa643db20c285ede6db591bb685 [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]
40Converting data from one type to another. For example,
41{}\code{int(3.15)} coerces the floating point number to the integer,
42{}\code{3}. Most mathematical operations have rules for coercing
Raymond Hettinger43b5e402003-09-27 20:19:02 +000043their arguments to a common type. For instance, adding \code{3+4.5},
44causes the integer \code{3} to be coerced to be a float
Fred Draked4a14192003-09-27 18:59:43 +000045{}\code{3.0} before adding to \code{4.5} resulting in the float
46{}\code{7.5}.
Skip Montanaro757dedc2003-09-24 16:51:23 +000047
48\index{descriptor}
Fred Draked4a14192003-09-27 18:59:43 +000049\item[descriptor]
50Any \emph{new-style} object that defines the methods
51{}\method{__get__()}, \method{__set__()}, or \method{__delete__()}.
52When a class attribute is a descriptor, its special binding behavior
53is triggered upon attribute lookup. Normally, writing \var{a.b} looks
54up the object \var{b} in the class dictionary for \var{a}, but if
55{}\var{b} is a descriptor, the defined method gets called.
56Understanding descriptors is a key to a deep understanding of Python
57because they are the basis for many features including functions,
58methods, properties, class methods, static methods, and reference to
59super classes.
Skip Montanaro757dedc2003-09-24 16:51:23 +000060
61\index{dictionary}
Fred Draked4a14192003-09-27 18:59:43 +000062\item[dictionary]
63An associative array, where arbitrary keys are mapped to values. The
64use of \class{dict} much resembles that for \class{list}, but the keys
65can be any object with a \method{__hash__()} function, not just
66integers starting from zero. Called a hash in Perl.
Skip Montanaro757dedc2003-09-24 16:51:23 +000067
68\index{EAFP}
Fred Draked4a14192003-09-27 18:59:43 +000069\item[EAFP]
70Easier to ask for forgiveness than permission. This common Python
71coding style assumes the existence of valid keys or attributes and
72catches exceptions if the assumption proves false. This clean and
73fast style is characterized by the presence of many \keyword{try} and
74{}\keyword{except} statements. The technique contrasts with the
75{}\emph{LBYL} style that is common in many other languages such as C.
Skip Montanaro757dedc2003-09-24 16:51:23 +000076
77\index{__future__}
Fred Draked4a14192003-09-27 18:59:43 +000078\item[__future__]
79A pseudo module which programmers can use to enable new language
80features which are not compatible with the current interpreter. For
Raymond Hettinger43b5e402003-09-27 20:19:02 +000081example, the expression \code{11/4} currently evaluates to \code{2}.
82If the module in which it is executed had enabled \emph{true division}
Fred Draked4a14192003-09-27 18:59:43 +000083by executing:
Skip Montanaro757dedc2003-09-24 16:51:23 +000084
85\begin{verbatim}
86from __future__ import division
87\end{verbatim}
88
Fred Drake984920b2003-09-28 19:03:36 +000089the expression \code{11/4} would evaluate to \code{2.75}. By actually
90importing the \ulink{\module{__future__}}{../lib/module-future.html}
91module and evaluating its variables, you can see when a new feature
92was first added to the language and when it will become the default:
Skip Montanaro757dedc2003-09-24 16:51:23 +000093
94\begin{verbatim}
95>>> import __future__
96>>> __future__.division
97_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
98\end{verbatim}
99
100\index{generator}
Fred Draked4a14192003-09-27 18:59:43 +0000101\item[generator]
102A function that returns an iterator. It looks like a normal function
103except that the \keyword{yield} keyword is used instead of
Skip Montanaro757dedc2003-09-24 16:51:23 +0000104{}\keyword{return}. Generator functions often contain one or more
Fred Draked4a14192003-09-27 18:59:43 +0000105{}\keyword{for} or \keyword{while} loops that \keyword{yield} elements
106back to the caller. The function execution is stopped at the
107{}\keyword{yield} keyword (returning the result) and is resumed there
108when the next element is requested by calling the \method{next()}
109method of the returned iterator.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000110
111\index{GIL}
Fred Draked4a14192003-09-27 18:59:43 +0000112\item[GIL]
113See \emph{global interpreter lock}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000114
115\index{global interpreter lock}
Fred Draked4a14192003-09-27 18:59:43 +0000116\item[global interpreter lock]
117The lock used by Python threads to assure that only one thread can be
118run at a time. This simplifies Python by assuring that no two
119processes can access the same memory at the same time. Locking the
120entire interpreter makes it easier for the interpreter to be
Skip Montanaro757dedc2003-09-24 16:51:23 +0000121multi-threaded, at the expense of some parallelism on multi-processor
Fred Draked4a14192003-09-27 18:59:43 +0000122machines. Efforts have been made in the past to create a
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000123``free-threaded'' interpreter (one which locks shared data at a much
Fred Draked4a14192003-09-27 18:59:43 +0000124finer granularity), but performance suffered in the common
125single-processor case.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000126
127\index{IDLE}
Fred Draked4a14192003-09-27 18:59:43 +0000128\item[IDLE]
129An Integrated Development Environment for Python. IDLE is a
Raymond Hettinger5a25aa62003-09-27 05:42:14 +0000130basic editor and interpreter environment that ships with the standard
131distribution of Python. Good for beginners, it also serves as clear
132example code for those wanting to implement a moderately
Fred Draked4a14192003-09-27 18:59:43 +0000133sophisticated, multi-platform GUI application.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000134
135\index{immutable}
Fred Draked4a14192003-09-27 18:59:43 +0000136\item[immutable]
137A object with fixed value. Immutable objects are numbers, strings or
138tuples (and more). Such an object cannot be altered. A new object
Skip Montanaro757dedc2003-09-24 16:51:23 +0000139has to be created if a different value has to be stored. They play an
Fred Draked4a14192003-09-27 18:59:43 +0000140important role in places where a constant hash value is needed. For
141example as a key in a dictionary.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000142
143\index{integer division}
Fred Draked4a14192003-09-27 18:59:43 +0000144\item[integer division]
145Mathematical division discarding any remainder. For example, the
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000146expression \code{11/4} currently evaluates to \code{2} in contrast
Fred Draked4a14192003-09-27 18:59:43 +0000147to the \code{2.75} returned by float division. Also called
148{}\emph{floor division}. When dividing two integers the outcome will
149always be another integer (having the floor function applied to it).
150However, if one of the operands is another numeric type (such as a
151{}\class{float}), the result will be coerced (see \emph{coercion}) to
152a common type. For example, a integer divided by a float will result
153in a float value, possibly with a decimal fraction. Integer division
154can be forced by using the \code{//} operator instead of the \code{/}
155operator. See also \emph{__future__}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000156
157\index{interactive}
Fred Draked4a14192003-09-27 18:59:43 +0000158\item[interactive]
159Python has an interactive interpreter which means that you can try out
160things and directly see its result. Just launch \code{python} with no
161arguments (possibly by selecting it from your computer's main menu).
162It is a very powerful way to test out new ideas or inspect modules and
163packages (remember \code{help(x)}).
Skip Montanaro757dedc2003-09-24 16:51:23 +0000164
165\index{interpreted}
Fred Draked4a14192003-09-27 18:59:43 +0000166\item[interpreted]
167Python is an interpreted language, opposed to a compiled one. This
168means that the source files can be run right away without first making
169an executable which is then run. Interpreted languages typically have
Skip Montanaro757dedc2003-09-24 16:51:23 +0000170a shorter development/debug cycle than compiled ones. See also
Fred Draked4a14192003-09-27 18:59:43 +0000171{}\emph{interactive}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000172
173\index{iterable}
Fred Draked4a14192003-09-27 18:59:43 +0000174\item[iterable]
175A container object capable of returning its members one at a time.
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000176Examples of iterables include all sequence types (such as \class{list},
Raymond Hettinger5a25aa62003-09-27 05:42:14 +0000177{}\class{str}, and \class{tuple}) and some non-sequence types like
Fred Draked4a14192003-09-27 18:59:43 +0000178{}\class{dict} and \class{file} and objects of any classes you define
179with an \method{__iter__()} or \method{__getitem__()} method. Iterables
180can be used in a \keyword{for} loop and in many other places where a
181sequence is needed (\function{zip()}, \function{map()}, ...). When an
182iterable object is passed as an argument to the builtin function
183{}\function{iter()}, it returns an iterator for the object. This
184iterator is good for one pass over the set of values. When using
185iterables, it is usually not necessary to call \function{iter()} or
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000186deal with iterator objects yourself. The \code{for} statement does
Fred Draked4a14192003-09-27 18:59:43 +0000187that automatically for you, creating a temporary unnamed variable to
188hold the iterator for the duration of the loop. See also
189{}\emph{iterator}, \emph{sequence}, and \emph{generator}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000190
191\index{iterator}
Fred Draked4a14192003-09-27 18:59:43 +0000192\item[iterator]
193An object representing a stream of data. Repeated calls to the
194iterator's \method{next()} method return successive items in the
Skip Montanaro757dedc2003-09-24 16:51:23 +0000195stream. When no more data is available a \exception{StopIteration}
Fred Draked4a14192003-09-27 18:59:43 +0000196exception is raised instead. At this point, the iterator object is
197exhausted and any further calls to its \method{next()} method just
198raise \exception{StopIteration} again. Iterators are required to have
199an \method{__iter__()} method that returns the iterator object
200itself so every iterator is also iterable and may be used in most
201places where other iterables are accepted. One notable exception is
202code that attempts multiple iteration passes. A container object
203(such as a \class{list}) produces a fresh new iterator each time you
204pass it to the \function{iter()} function or use it in a
205{}\keyword{for} loop. Attempting this with an iterator will just
206return the same exhausted iterator object from the second iteration
207pass, making it appear like an empty container.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000208
209\index{list comprehension}
Fred Draked4a14192003-09-27 18:59:43 +0000210\item[list comprehension]
211A compact way to process all or a subset of elements in a sequence and
212return a list with the results. \code{result = ["0x\%02x"
Skip Montanaro757dedc2003-09-24 16:51:23 +0000213\% x for x in range(256) if x \% 2 == 0]} generates a list of strings
214containing hex numbers (0x..) that are even and in the range from 0 to 255.
215The \keyword{if} clause is optional. If omitted, all elements in
Fred Draked4a14192003-09-27 18:59:43 +0000216{}\code{range(256)} are processed in that case.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000217
218\index{mapping}
Fred Draked4a14192003-09-27 18:59:43 +0000219\item[mapping]
220A container object (such as \class{dict}) that supports arbitrary key
221lookups using the special method \method{__getitem__()}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000222
223\index{metaclass}
Fred Draked4a14192003-09-27 18:59:43 +0000224\item[metaclass]
225The class of a class. Class definitions create a class name, a class
226dictionary, and a list of base classes. The metaclass is responsible
227for taking those three arguments and creating the class. Most object
228oriented programming languages provide a default implementation. What
229makes Python special is that it is possible to create custom
230metaclasses. Most users never need this tool, but when the need
231arises, metaclasses can provide powerful, elegant solutions. They
232have been used for logging attribute access, adding thread-safety,
233tracking object creation, implementing singletons, and many other
234tasks.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000235
236\index{LBYL}
Fred Draked4a14192003-09-27 18:59:43 +0000237\item[LBYL]
238Look before you leap. This coding style explicitly tests for
239pre-conditions before making calls or lookups. This style contrasts
240with the \emph{EAFP} approach and is characterized the presence of
241many \keyword{if} statements.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000242
243\index{mutable}
Fred Draked4a14192003-09-27 18:59:43 +0000244\item[mutable]
245Mutable objects can change their value but keep their \function{id()}.
246See also \emph{immutable}.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000247
248\index{namespace}
Fred Draked4a14192003-09-27 18:59:43 +0000249\item[namespace]
250The place where a variable is stored. Namespaces are implemented as
251dictionary. There is the local, global and builtins namespace and the
252nested namespaces in objects (in methods). Namespaces support
253modularity by preventing naming conflicts. For instance, the
254functions \function{__builtin__.open()} and \function{os.open()} are
255distinguished by their namespaces. Namespaces also aid readability
256and maintainability by making it clear which modules implement a
257function. For instance, writing \function{random.seed()} or
258{}\function{itertools.izip()} makes it clear that those functions are
Fred Drake984920b2003-09-28 19:03:36 +0000259implemented by the \ulink{\module{random}}{../lib/module-random.html}
260and \ulink{\module{itertools}}{../lib/module-itertools.html} modules
261respectively.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000262
263\index{nested scope}
Fred Draked4a14192003-09-27 18:59:43 +0000264\item[nested scope]
265The ability to refer to a variable in an enclosing definition. For
266instance, a function defined inside another function can refer to
267variables in the outer function. Note that nested scopes work only
Skip Montanaro757dedc2003-09-24 16:51:23 +0000268for reference and not for assignment which will always write to the
Fred Draked4a14192003-09-27 18:59:43 +0000269innermost scope. In contrast, local variables both read and write in
270the innermost scope. Likewise, global variables read and write to the
271global namespace.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000272
273\index{new-style class}
Fred Draked4a14192003-09-27 18:59:43 +0000274\item[new-style class]
275Any class that inherits from \class{object}. This includes all
276built-in types like \class{list} and \class{dict}. Only new-style
277classes can use Python's newer, versatile features like
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000278{}\method{__slots__}, descriptors, properties,
Fred Draked4a14192003-09-27 18:59:43 +0000279\method{__getattribute__()}, class methods, and static methods.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000280
281\index{Python3000}
Fred Draked4a14192003-09-27 18:59:43 +0000282\item[Python3000]
283A mythical python release, allowed not to be backward compatible, with
284telepathic interface.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000285
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000286\index{__slots__}
287\item[__slots__]
288A declaration inside a \emph{new-style class} that saves memory by
289pre-declaring space for instance attributes and eliminating instance
290dictionaries. Though popular, the technique is somewhat tricky to get
291right and is best reserved for rare cases where there are large
292numbers of instances in a memory critical application.
293
Skip Montanaro757dedc2003-09-24 16:51:23 +0000294\index{sequence}
Fred Draked4a14192003-09-27 18:59:43 +0000295\item[sequence]
296An \emph{iterable} which supports efficient element access using
297integer indices via the \method{__getitem__()} and
298{}\method{__len__()} special methods. Some built-in sequence types
299are \class{list}, \class{str}, \class{tuple}, and \class{unicode}.
300Note that \class{dict} also supports \method{__getitem__()} and
301{}\method{__len__()}, but is considered a mapping rather than a
302sequence because the lookups use arbitrary \emph{immutable} keys
303rather than integers.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000304
305\index{Zen of Python}
Fred Draked4a14192003-09-27 18:59:43 +0000306\item[Zen of Python]
307Listing of Python design principles and philosophies that are helpful
308in understanding and using the language. The listing can be found by
Raymond Hettinger43b5e402003-09-27 20:19:02 +0000309typing ``\code{import this}'' at the interactive prompt.
Skip Montanaro757dedc2003-09-24 16:51:23 +0000310
311\end{description}