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