blob: 03484de256f547958bea9bf5d2e74f1dc2b96653 [file] [log] [blame]
Georg Brandl437e6a32007-08-17 06:27:11 +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
Georg Brandl63fa1682007-10-21 10:24:20 +000023 bytecode
24 Python source code is compiled into bytecode, the internal representation
25 of a Python program in the interpreter. The bytecode is also cached in
26 ``.pyc`` and ``.pyo`` files so that executing the same file is faster the
27 second time (recompilation from source to bytecode can be avoided). This
28 "intermediate language" is said to run on a "virtual machine" that calls
29 the subroutines corresponding to each bytecode.
Georg Brandl437e6a32007-08-17 06:27:11 +000030
31 classic class
32 Any class which does not inherit from :class:`object`. See
Georg Brandl6c82b6c2007-08-17 16:54:59 +000033 :term:`new-style class`.
Georg Brandl437e6a32007-08-17 06:27:11 +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
61 Any *new-style* object that defines the methods :meth:`__get__`,
Georg Brandl5e52db02007-10-21 10:45:46 +000062 :meth:`__set__`, or :meth:`__delete__`. When a class attribute is a
Georg Brandl437e6a32007-08-17 06:27:11 +000063 descriptor, its special binding behavior is triggered upon attribute
Georg Brandl5e52db02007-10-21 10:45:46 +000064 lookup. Normally, using *a.b* to get, set or delete an attribute looks up
65 the object named *b* in the class dictionary for *a*, but if *b* is a
66 descriptor, the respective descriptor method gets called. Understanding
67 descriptors is a key to a deep understanding of Python because they are
68 the basis for many features including functions, methods, properties,
69 class methods, static methods, and reference to super classes.
70
71 For more information about descriptors' methods, see :ref:`descriptors`.
Georg Brandl437e6a32007-08-17 06:27:11 +000072
73 dictionary
74 An associative array, where arbitrary keys are mapped to values. The use
75 of :class:`dict` much resembles that for :class:`list`, but the keys can
76 be any object with a :meth:`__hash__` function, not just integers starting
77 from zero. Called a hash in Perl.
78
79 duck-typing
80 Pythonic programming style that determines an object's type by inspection
81 of its method or attribute signature rather than by explicit relationship
82 to some type object ("If it looks like a duck and quacks like a duck, it
83 must be a duck.") By emphasizing interfaces rather than specific types,
84 well-designed code improves its flexibility by allowing polymorphic
85 substitution. Duck-typing avoids tests using :func:`type` or
86 :func:`isinstance`. Instead, it typically employs :func:`hasattr` tests or
Georg Brandl6c82b6c2007-08-17 16:54:59 +000087 :term:`EAFP` programming.
Georg Brandl437e6a32007-08-17 06:27:11 +000088
89 EAFP
90 Easier to ask for forgiveness than permission. This common Python coding
91 style assumes the existence of valid keys or attributes and catches
92 exceptions if the assumption proves false. This clean and fast style is
93 characterized by the presence of many :keyword:`try` and :keyword:`except`
Georg Brandl6c82b6c2007-08-17 16:54:59 +000094 statements. The technique contrasts with the :term:`LBYL` style that is
Georg Brandl437e6a32007-08-17 06:27:11 +000095 common in many other languages such as C.
96
97 extension module
98 A module written in C, using Python's C API to interact with the core and
99 with user code.
100
101 __future__
102 A pseudo module which programmers can use to enable new language features
103 which are not compatible with the current interpreter. For example, the
104 expression ``11/4`` currently evaluates to ``2``. If the module in which
105 it is executed had enabled *true division* by executing::
106
107 from __future__ import division
108
109 the expression ``11/4`` would evaluate to ``2.75``. By importing the
110 :mod:`__future__` module and evaluating its variables, you can see when a
111 new feature was first added to the language and when it will become the
112 default::
113
114 >>> import __future__
115 >>> __future__.division
116 _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
117
118 garbage collection
119 The process of freeing memory when it is not used anymore. Python
120 performs garbage collection via reference counting and a cyclic garbage
121 collector that is able to detect and break reference cycles.
122
123 generator
124 A function that returns an iterator. It looks like a normal function
125 except that values are returned to the caller using a :keyword:`yield`
126 statement instead of a :keyword:`return` statement. Generator functions
127 often contain one or more :keyword:`for` or :keyword:`while` loops that
128 :keyword:`yield` elements back to the caller. The function execution is
129 stopped at the :keyword:`yield` keyword (returning the result) and is
130 resumed there when the next element is requested by calling the
131 :meth:`next` method of the returned iterator.
132
133 .. index:: single: generator expression
134
135 generator expression
136 An expression that returns a generator. It looks like a normal expression
137 followed by a :keyword:`for` expression defining a loop variable, range,
138 and an optional :keyword:`if` expression. The combined expression
139 generates values for an enclosing function::
140
141 >>> sum(i*i for i in range(10)) # sum of squares 0, 1, 4, ... 81
142 285
143
144 GIL
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000145 See :term:`global interpreter lock`.
Georg Brandl437e6a32007-08-17 06:27:11 +0000146
147 global interpreter lock
148 The lock used by Python threads to assure that only one thread can be run
149 at a time. This simplifies Python by assuring that no two processes can
150 access the same memory at the same time. Locking the entire interpreter
151 makes it easier for the interpreter to be multi-threaded, at the expense
152 of some parallelism on multi-processor machines. Efforts have been made
153 in the past to create a "free-threaded" interpreter (one which locks
154 shared data at a much finer granularity), but performance suffered in the
155 common single-processor case.
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000156
157 hashable
158 An object is *hashable* if it has a hash value that never changes during
159 its lifetime (it needs a :meth:`__hash__` method), and can be compared to
160 other objects (it needs an :meth:`__eq__` or :meth:`__cmp__` method).
161 Hashable objects that compare equal must have the same hash value.
162
163 Hashability makes an object usable as a dictionary key and a set member,
164 because these data structures use the hash value internally.
165
166 All of Python's immutable built-in objects are hashable, while all mutable
167 containers (such as lists or dictionaries) are not. Objects that are
168 instances of user-defined classes are hashable by default; they all
169 compare unequal, and their hash value is their :func:`id`.
Georg Brandl437e6a32007-08-17 06:27:11 +0000170
171 IDLE
172 An Integrated Development Environment for Python. IDLE is a basic editor
173 and interpreter environment that ships with the standard distribution of
174 Python. Good for beginners, it also serves as clear example code for
175 those wanting to implement a moderately sophisticated, multi-platform GUI
176 application.
177
178 immutable
179 An object with fixed value. Immutable objects are numbers, strings or
180 tuples (and more). Such an object cannot be altered. A new object has to
181 be created if a different value has to be stored. They play an important
182 role in places where a constant hash value is needed, for example as a key
183 in a dictionary.
184
185 integer division
186 Mathematical division discarding any remainder. For example, the
187 expression ``11/4`` currently evaluates to ``2`` in contrast to the
188 ``2.75`` returned by float division. Also called *floor division*.
189 When dividing two integers the outcome will always be another integer
190 (having the floor function applied to it). However, if one of the operands
191 is another numeric type (such as a :class:`float`), the result will be
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000192 coerced (see :term:`coercion`) to a common type. For example, an integer
Georg Brandl437e6a32007-08-17 06:27:11 +0000193 divided by a float will result in a float value, possibly with a decimal
194 fraction. Integer division can be forced by using the ``//`` operator
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000195 instead of the ``/`` operator. See also :term:`__future__`.
Georg Brandl437e6a32007-08-17 06:27:11 +0000196
197 interactive
198 Python has an interactive interpreter which means that you can try out
199 things and immediately see their results. Just launch ``python`` with no
200 arguments (possibly by selecting it from your computer's main menu). It is
201 a very powerful way to test out new ideas or inspect modules and packages
202 (remember ``help(x)``).
203
204 interpreted
205 Python is an interpreted language, as opposed to a compiled one. This
206 means that the source files can be run directly without first creating an
207 executable which is then run. Interpreted languages typically have a
208 shorter development/debug cycle than compiled ones, though their programs
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000209 generally also run more slowly. See also :term:`interactive`.
Georg Brandl437e6a32007-08-17 06:27:11 +0000210
211 iterable
212 A container object capable of returning its members one at a
213 time. Examples of iterables include all sequence types (such as
214 :class:`list`, :class:`str`, and :class:`tuple`) and some non-sequence
215 types like :class:`dict` and :class:`file` and objects of any classes you
216 define with an :meth:`__iter__` or :meth:`__getitem__` method. Iterables
217 can be used in a :keyword:`for` loop and in many other places where a
218 sequence is needed (:func:`zip`, :func:`map`, ...). When an iterable
219 object is passed as an argument to the builtin function :func:`iter`, it
220 returns an iterator for the object. This iterator is good for one pass
221 over the set of values. When using iterables, it is usually not necessary
222 to call :func:`iter` or deal with iterator objects yourself. The ``for``
223 statement does that automatically for you, creating a temporary unnamed
224 variable to hold the iterator for the duration of the loop. See also
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000225 :term:`iterator`, :term:`sequence`, and :term:`generator`.
Georg Brandl437e6a32007-08-17 06:27:11 +0000226
227 iterator
228 An object representing a stream of data. Repeated calls to the iterator's
229 :meth:`next` method return successive items in the stream. When no more
230 data is available a :exc:`StopIteration` exception is raised instead. At
231 this point, the iterator object is exhausted and any further calls to its
232 :meth:`next` method just raise :exc:`StopIteration` again. Iterators are
233 required to have an :meth:`__iter__` method that returns the iterator
234 object itself so every iterator is also iterable and may be used in most
235 places where other iterables are accepted. One notable exception is code
236 that attempts multiple iteration passes. A container object (such as a
237 :class:`list`) produces a fresh new iterator each time you pass it to the
238 :func:`iter` function or use it in a :keyword:`for` loop. Attempting this
239 with an iterator will just return the same exhausted iterator object used
240 in the previous iteration pass, making it appear like an empty container.
241
Georg Brandle7a09902007-10-21 12:10:28 +0000242 More information can be found in :ref:`typeiter`.
243
Georg Brandl437e6a32007-08-17 06:27:11 +0000244 LBYL
245 Look before you leap. This coding style explicitly tests for
246 pre-conditions before making calls or lookups. This style contrasts with
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000247 the :term:`EAFP` approach and is characterized by the presence of many
Georg Brandl437e6a32007-08-17 06:27:11 +0000248 :keyword:`if` statements.
249
250 list comprehension
251 A compact way to process all or a subset of elements in a sequence and
252 return a list with the results. ``result = ["0x%02x" % x for x in
253 range(256) if x % 2 == 0]`` generates a list of strings containing hex
254 numbers (0x..) that are even and in the range from 0 to 255. The
255 :keyword:`if` clause is optional. If omitted, all elements in
256 ``range(256)`` are processed.
257
258 mapping
259 A container object (such as :class:`dict`) that supports arbitrary key
260 lookups using the special method :meth:`__getitem__`.
261
262 metaclass
263 The class of a class. Class definitions create a class name, a class
264 dictionary, and a list of base classes. The metaclass is responsible for
265 taking those three arguments and creating the class. Most object oriented
266 programming languages provide a default implementation. What makes Python
267 special is that it is possible to create custom metaclasses. Most users
268 never need this tool, but when the need arises, metaclasses can provide
269 powerful, elegant solutions. They have been used for logging attribute
270 access, adding thread-safety, tracking object creation, implementing
271 singletons, and many other tasks.
Georg Brandla7395032007-10-21 12:15:05 +0000272
273 More information can be found in :ref:`metaclasses`.
Georg Brandl437e6a32007-08-17 06:27:11 +0000274
275 mutable
276 Mutable objects can change their value but keep their :func:`id`. See
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000277 also :term:`immutable`.
Georg Brandl437e6a32007-08-17 06:27:11 +0000278
279 namespace
280 The place where a variable is stored. Namespaces are implemented as
281 dictionaries. There are the local, global and builtin namespaces as well
282 as nested namespaces in objects (in methods). Namespaces support
283 modularity by preventing naming conflicts. For instance, the functions
284 :func:`__builtin__.open` and :func:`os.open` are distinguished by their
285 namespaces. Namespaces also aid readability and maintainability by making
286 it clear which module implements a function. For instance, writing
287 :func:`random.seed` or :func:`itertools.izip` makes it clear that those
288 functions are implemented by the :mod:`random` and :mod:`itertools`
289 modules respectively.
290
291 nested scope
292 The ability to refer to a variable in an enclosing definition. For
293 instance, a function defined inside another function can refer to
294 variables in the outer function. Note that nested scopes work only for
295 reference and not for assignment which will always write to the innermost
296 scope. In contrast, local variables both read and write in the innermost
297 scope. Likewise, global variables read and write to the global namespace.
298
299 new-style class
300 Any class that inherits from :class:`object`. This includes all built-in
301 types like :class:`list` and :class:`dict`. Only new-style classes can
302 use Python's newer, versatile features like :attr:`__slots__`,
303 descriptors, properties, :meth:`__getattribute__`, class methods, and
304 static methods.
Georg Brandla7395032007-10-21 12:15:05 +0000305
306 More information can be found in :ref:`newstyle`.
Georg Brandl437e6a32007-08-17 06:27:11 +0000307
308 Python 3000
309 Nickname for the next major Python version, 3.0 (coined long ago when the
310 release of version 3 was something in the distant future.)
311
312 reference count
313 The number of places where a certain object is referenced to. When the
314 reference count drops to zero, an object is deallocated. While reference
315 counting is invisible on the Python code level, it is used on the
316 implementation level to keep track of allocated memory.
317
318 __slots__
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000319 A declaration inside a :term:`new-style class` that saves memory by
Georg Brandl437e6a32007-08-17 06:27:11 +0000320 pre-declaring space for instance attributes and eliminating instance
321 dictionaries. Though popular, the technique is somewhat tricky to get
322 right and is best reserved for rare cases where there are large numbers of
323 instances in a memory-critical application.
324
325 sequence
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000326 An :term:`iterable` which supports efficient element access using integer
Georg Brandl437e6a32007-08-17 06:27:11 +0000327 indices via the :meth:`__getitem__` and :meth:`__len__` special methods.
328 Some built-in sequence types are :class:`list`, :class:`str`,
329 :class:`tuple`, and :class:`unicode`. Note that :class:`dict` also
330 supports :meth:`__getitem__` and :meth:`__len__`, but is considered a
331 mapping rather than a sequence because the lookups use arbitrary
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000332 :term:`immutable` keys rather than integers.
Georg Brandl437e6a32007-08-17 06:27:11 +0000333
334 type
335 The type of a Python object determines what kind of object it is; every
336 object has a type. An object's type is accessible as its
337 :attr:`__class__` attribute or can be retrieved with ``type(obj)``.
338
339 Zen of Python
340 Listing of Python design principles and philosophies that are helpful in
341 understanding and using the language. The listing can be found by typing
342 "``import this``" at the interactive prompt.