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