blob: eb72558ecc796a473cd0cf973cc7c6690119aecf [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 ``>>>``
Skip Montanarof02c5f32008-09-15 02:03:05 +000012 The default Python prompt of the interactive shell. Often seen for code
13 examples which can be executed interactively in the interpreter.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000014
Georg Brandl437e6a32007-08-17 06:27:11 +000015 ``...``
Skip Montanarof02c5f32008-09-15 02:03:05 +000016 The default Python prompt of the interactive shell when entering code for
17 an indented code block or within a pair of matching left and right
18 delimiters (parentheses, square brackets or curly braces).
Georg Brandl584265b2007-12-02 14:58:50 +000019
Georg Brandl5a42ca62008-05-20 07:20:12 +000020 2to3
21 A tool that tries to convert Python 2.x code to Python 3.x code by
Georg Brandl09302282010-10-06 09:32:48 +000022 handling most of the incompatibilities which can be detected by parsing the
Georg Brandl5a42ca62008-05-20 07:20:12 +000023 source and traversing the parse tree.
24
25 2to3 is available in the standard library as :mod:`lib2to3`; a standalone
Benjamin Peterson40202212008-07-24 02:45:37 +000026 entry point is provided as :file:`Tools/scripts/2to3`. See
27 :ref:`2to3-reference`.
Georg Brandl5a42ca62008-05-20 07:20:12 +000028
Benjamin Peterson9385b9d2008-07-03 12:57:35 +000029 abstract base class
Éric Araujo8fde9502011-07-29 11:34:17 +020030 Abstract base classes complement :term:`duck-typing` by
Georg Brandld7d4fd72009-07-26 14:37:28 +000031 providing a way to define interfaces when other techniques like
Éric Araujo8fde9502011-07-29 11:34:17 +020032 :func:`hasattr` would be clumsy or subtly wrong (for example with
33 :ref:`magic methods <new-style-special-lookup>`). Python comes with many built-in ABCs for
Georg Brandld7d4fd72009-07-26 14:37:28 +000034 data structures (in the :mod:`collections` module), numbers (in the
35 :mod:`numbers` module), and streams (in the :mod:`io` module). You can
Éric Araujo8fde9502011-07-29 11:34:17 +020036 create your own ABCs with the :mod:`abc` module.
Benjamin Petersonaac51b82008-07-01 23:33:06 +000037
Georg Brandl584265b2007-12-02 14:58:50 +000038 argument
Skip Montanarof02c5f32008-09-15 02:03:05 +000039 A value passed to a function or method, assigned to a named local
40 variable in the function body. A function or method may have both
41 positional arguments and keyword arguments in its definition.
42 Positional and keyword arguments may be variable-length: ``*`` accepts
43 or passes (if in the function definition or call) several positional
44 arguments in a list, while ``**`` does the same for keyword arguments
45 in a dictionary.
Georg Brandl584265b2007-12-02 14:58:50 +000046
47 Any expression may be used within the argument list, and the evaluated
48 value is passed to the local variable.
Skip Montanaro9feab312008-09-15 02:19:53 +000049
50 attribute
51 A value associated with an object which is referenced by name using
52 dotted expressions. For example, if an object *o* has an attribute
53 *a* it would be referenced as *o.a*.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000054
Georg Brandl437e6a32007-08-17 06:27:11 +000055 BDFL
56 Benevolent Dictator For Life, a.k.a. `Guido van Rossum
57 <http://www.python.org/~guido/>`_, Python's creator.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000058
Georg Brandl63fa1682007-10-21 10:24:20 +000059 bytecode
60 Python source code is compiled into bytecode, the internal representation
61 of a Python program in the interpreter. The bytecode is also cached in
62 ``.pyc`` and ``.pyo`` files so that executing the same file is faster the
63 second time (recompilation from source to bytecode can be avoided). This
Skip Montanarof02c5f32008-09-15 02:03:05 +000064 "intermediate language" is said to run on a :term:`virtual machine`
65 that executes the machine code corresponding to each bytecode.
Skip Montanaro9feab312008-09-15 02:19:53 +000066
Georg Brandl2b4eda42010-07-03 10:25:54 +000067 A list of bytecode instructions can be found in the documentation for
68 :ref:`the dis module <bytecodes>`.
69
Skip Montanaro9feab312008-09-15 02:19:53 +000070 class
71 A template for creating user-defined objects. Class definitions
72 normally contain method definitions which operate on instances of the
73 class.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000074
Georg Brandl437e6a32007-08-17 06:27:11 +000075 classic class
76 Any class which does not inherit from :class:`object`. See
Skip Montanarof02c5f32008-09-15 02:03:05 +000077 :term:`new-style class`. Classic classes will be removed in Python 3.0.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000078
Georg Brandl437e6a32007-08-17 06:27:11 +000079 coercion
80 The implicit conversion of an instance of one type to another during an
81 operation which involves two arguments of the same type. For example,
82 ``int(3.15)`` converts the floating point number to the integer ``3``, but
83 in ``3+4.5``, each argument is of a different type (one int, one float),
84 and both must be converted to the same type before they can be added or it
85 will raise a ``TypeError``. Coercion between two operands can be
Georg Brandld7d4fd72009-07-26 14:37:28 +000086 performed with the ``coerce`` built-in function; thus, ``3+4.5`` is
Georg Brandl437e6a32007-08-17 06:27:11 +000087 equivalent to calling ``operator.add(*coerce(3, 4.5))`` and results in
88 ``operator.add(3.0, 4.5)``. Without coercion, all arguments of even
89 compatible types would have to be normalized to the same value by the
90 programmer, e.g., ``float(3)+4.5`` rather than just ``3+4.5``.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000091
Georg Brandl437e6a32007-08-17 06:27:11 +000092 complex number
93 An extension of the familiar real number system in which all numbers are
94 expressed as a sum of a real part and an imaginary part. Imaginary
95 numbers are real multiples of the imaginary unit (the square root of
96 ``-1``), often written ``i`` in mathematics or ``j`` in
Georg Brandld7d4fd72009-07-26 14:37:28 +000097 engineering. Python has built-in support for complex numbers, which are
Georg Brandl437e6a32007-08-17 06:27:11 +000098 written with this latter notation; the imaginary part is written with a
99 ``j`` suffix, e.g., ``3+1j``. To get access to complex equivalents of the
100 :mod:`math` module, use :mod:`cmath`. Use of complex numbers is a fairly
101 advanced mathematical feature. If you're not aware of a need for them,
102 it's almost certain you can safely ignore them.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000103
Skip Montanaroffe455c2007-12-08 15:23:31 +0000104 context manager
Skip Montanarof02c5f32008-09-15 02:03:05 +0000105 An object which controls the environment seen in a :keyword:`with`
Skip Montanaroffe455c2007-12-08 15:23:31 +0000106 statement by defining :meth:`__enter__` and :meth:`__exit__` methods.
107 See :pep:`343`.
108
Skip Montanarof02c5f32008-09-15 02:03:05 +0000109 CPython
Antoine Pitrou9f41bb32011-01-06 16:35:14 +0000110 The canonical implementation of the Python programming language, as
111 distributed on `python.org <http://python.org>`_. The term "CPython"
112 is used when necessary to distinguish this implementation from others
113 such as Jython or IronPython.
Skip Montanarof02c5f32008-09-15 02:03:05 +0000114
Georg Brandl584265b2007-12-02 14:58:50 +0000115 decorator
116 A function returning another function, usually applied as a function
117 transformation using the ``@wrapper`` syntax. Common examples for
118 decorators are :func:`classmethod` and :func:`staticmethod`.
119
120 The decorator syntax is merely syntactic sugar, the following two
121 function definitions are semantically equivalent::
122
123 def f(...):
124 ...
125 f = staticmethod(f)
126
127 @staticmethod
128 def f(...):
129 ...
130
Georg Brandl5066c0c2008-12-05 18:00:06 +0000131 See :ref:`the documentation for function definition <function>` for more
132 about decorators.
133
Georg Brandl437e6a32007-08-17 06:27:11 +0000134 descriptor
Skip Montanarof02c5f32008-09-15 02:03:05 +0000135 Any *new-style* object which defines the methods :meth:`__get__`,
Georg Brandl5e52db02007-10-21 10:45:46 +0000136 :meth:`__set__`, or :meth:`__delete__`. When a class attribute is a
Georg Brandl437e6a32007-08-17 06:27:11 +0000137 descriptor, its special binding behavior is triggered upon attribute
Georg Brandl5e52db02007-10-21 10:45:46 +0000138 lookup. Normally, using *a.b* to get, set or delete an attribute looks up
139 the object named *b* in the class dictionary for *a*, but if *b* is a
140 descriptor, the respective descriptor method gets called. Understanding
141 descriptors is a key to a deep understanding of Python because they are
142 the basis for many features including functions, methods, properties,
143 class methods, static methods, and reference to super classes.
144
145 For more information about descriptors' methods, see :ref:`descriptors`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000146
Georg Brandl437e6a32007-08-17 06:27:11 +0000147 dictionary
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000148 An associative array, where arbitrary keys are mapped to values. The keys
149 can be any object with :meth:`__hash__` function and :meth:`__eq__`
150 methods. Called a hash in Perl.
Georg Brandle64f7382008-07-20 11:50:29 +0000151
152 docstring
Skip Montanarof02c5f32008-09-15 02:03:05 +0000153 A string literal which appears as the first expression in a class,
154 function or module. While ignored when the suite is executed, it is
155 recognized by the compiler and put into the :attr:`__doc__` attribute
156 of the enclosing class, function or module. Since it is available via
157 introspection, it is the canonical place for documentation of the
Georg Brandle64f7382008-07-20 11:50:29 +0000158 object.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000159
160 duck-typing
Georg Brandle85e1ae2010-10-06 09:17:24 +0000161 A programming style which does not look at an object's type to determine
162 if it has the right interface; instead, the method or attribute is simply
163 called or used ("If it looks like a duck and quacks like a duck, it
Georg Brandl437e6a32007-08-17 06:27:11 +0000164 must be a duck.") By emphasizing interfaces rather than specific types,
165 well-designed code improves its flexibility by allowing polymorphic
166 substitution. Duck-typing avoids tests using :func:`type` or
Georg Brandl04eba2c2010-07-11 08:56:18 +0000167 :func:`isinstance`. (Note, however, that duck-typing can be complemented
168 with :term:`abstract base class`\ es.) Instead, it typically employs
169 :func:`hasattr` tests or :term:`EAFP` programming.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000170
Georg Brandl437e6a32007-08-17 06:27:11 +0000171 EAFP
172 Easier to ask for forgiveness than permission. This common Python coding
173 style assumes the existence of valid keys or attributes and catches
174 exceptions if the assumption proves false. This clean and fast style is
175 characterized by the presence of many :keyword:`try` and :keyword:`except`
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000176 statements. The technique contrasts with the :term:`LBYL` style
Skip Montanarof02c5f32008-09-15 02:03:05 +0000177 common to many other languages such as C.
Georg Brandl437e6a32007-08-17 06:27:11 +0000178
Georg Brandl584265b2007-12-02 14:58:50 +0000179 expression
180 A piece of syntax which can be evaluated to some value. In other words,
181 an expression is an accumulation of expression elements like literals, names,
Skip Montanarof02c5f32008-09-15 02:03:05 +0000182 attribute access, operators or function calls which all return a value.
183 In contrast to many other languages, not all language constructs are expressions.
184 There are also :term:`statement`\s which cannot be used as expressions,
185 such as :keyword:`print` or :keyword:`if`. Assignments are also statements,
186 not expressions.
Georg Brandl584265b2007-12-02 14:58:50 +0000187
Georg Brandl437e6a32007-08-17 06:27:11 +0000188 extension module
Georg Brandl28dadd92011-02-25 10:50:32 +0000189 A module written in C or C++, using Python's C API to interact with the
190 core and with user code.
Georg Brandl584265b2007-12-02 14:58:50 +0000191
Georg Brandl624f3372009-03-31 16:11:45 +0000192 finder
193 An object that tries to find the :term:`loader` for a module. It must
194 implement a method named :meth:`find_module`. See :pep:`302` for
195 details.
196
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000197 floor division
198 Mathematical division that rounds down to nearest integer. The floor
199 division operator is ``//``. For example, the expression ``11 // 4``
200 evaluates to ``2`` in contrast to the ``2.75`` returned by float true
201 division. Note that ``(-11) // 4`` is ``-3`` because that is ``-2.75``
202 rounded *downward*. See :pep:`238`.
203
Georg Brandl584265b2007-12-02 14:58:50 +0000204 function
205 A series of statements which returns some value to a caller. It can also
206 be passed zero or more arguments which may be used in the execution of
207 the body. See also :term:`argument` and :term:`method`.
208
Georg Brandl437e6a32007-08-17 06:27:11 +0000209 __future__
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000210 A pseudo-module which programmers can use to enable new language features
Georg Brandl437e6a32007-08-17 06:27:11 +0000211 which are not compatible with the current interpreter. For example, the
212 expression ``11/4`` currently evaluates to ``2``. If the module in which
213 it is executed had enabled *true division* by executing::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000214
Georg Brandl437e6a32007-08-17 06:27:11 +0000215 from __future__ import division
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000216
Georg Brandl437e6a32007-08-17 06:27:11 +0000217 the expression ``11/4`` would evaluate to ``2.75``. By importing the
218 :mod:`__future__` module and evaluating its variables, you can see when a
219 new feature was first added to the language and when it will become the
220 default::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000221
Georg Brandl437e6a32007-08-17 06:27:11 +0000222 >>> import __future__
223 >>> __future__.division
224 _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
225
226 garbage collection
227 The process of freeing memory when it is not used anymore. Python
228 performs garbage collection via reference counting and a cyclic garbage
229 collector that is able to detect and break reference cycles.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000230
Georg Brandlea2d3892010-04-02 09:11:49 +0000231 .. index:: single: generator
232
Georg Brandl437e6a32007-08-17 06:27:11 +0000233 generator
Skip Montanarof02c5f32008-09-15 02:03:05 +0000234 A function which returns an iterator. It looks like a normal function
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000235 except that it contains :keyword:`yield` statements for producing a series
236 a values usable in a for-loop or that can be retrieved one at a time with
237 the :func:`next` function. Each :keyword:`yield` temporarily suspends
238 processing, remembering the location execution state (including local
239 variables and pending try-statements). When the generator resumes, it
240 picks-up where it left-off (in contrast to functions which start fresh on
241 every invocation).
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000242
Georg Brandl437e6a32007-08-17 06:27:11 +0000243 .. index:: single: generator expression
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000244
Georg Brandl437e6a32007-08-17 06:27:11 +0000245 generator expression
Georg Brandlea2d3892010-04-02 09:11:49 +0000246 An expression that returns an iterator. It looks like a normal expression
Georg Brandl437e6a32007-08-17 06:27:11 +0000247 followed by a :keyword:`for` expression defining a loop variable, range,
248 and an optional :keyword:`if` expression. The combined expression
249 generates values for an enclosing function::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000250
Georg Brandl437e6a32007-08-17 06:27:11 +0000251 >>> sum(i*i for i in range(10)) # sum of squares 0, 1, 4, ... 81
252 285
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000253
Georg Brandl437e6a32007-08-17 06:27:11 +0000254 GIL
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000255 See :term:`global interpreter lock`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000256
Georg Brandl437e6a32007-08-17 06:27:11 +0000257 global interpreter lock
Antoine Pitrou9f41bb32011-01-06 16:35:14 +0000258 The mechanism used by the :term:`CPython` interpreter to assure that
259 only one thread executes Python :term:`bytecode` at a time.
260 This simplifies the CPython implementation by making the object model
261 (including critical built-in types such as :class:`dict`) implicitly
262 safe against concurrent access. Locking the entire interpreter
263 makes it easier for the interpreter to be multi-threaded, at the
264 expense of much of the parallelism afforded by multi-processor
265 machines.
266
267 However, some extension modules, either standard or third-party,
268 are designed so as to release the GIL when doing computationally-intensive
269 tasks such as compression or hashing. Also, the GIL is always released
270 when doing I/O.
271
272 Past efforts to create a "free-threaded" interpreter (one which locks
273 shared data at a much finer granularity) have not been successful
274 because performance suffered in the common single-processor case. It
275 is believed that overcoming this performance issue would make the
276 implementation much more complicated and therefore costlier to maintain.
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000277
278 hashable
Skip Montanarof02c5f32008-09-15 02:03:05 +0000279 An object is *hashable* if it has a hash value which never changes during
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000280 its lifetime (it needs a :meth:`__hash__` method), and can be compared to
281 other objects (it needs an :meth:`__eq__` or :meth:`__cmp__` method).
Skip Montanarof02c5f32008-09-15 02:03:05 +0000282 Hashable objects which compare equal must have the same hash value.
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000283
284 Hashability makes an object usable as a dictionary key and a set member,
285 because these data structures use the hash value internally.
286
Skip Montanarof02c5f32008-09-15 02:03:05 +0000287 All of Python's immutable built-in objects are hashable, while no mutable
288 containers (such as lists or dictionaries) are. Objects which are
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000289 instances of user-defined classes are hashable by default; they all
290 compare unequal, and their hash value is their :func:`id`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000291
Georg Brandl437e6a32007-08-17 06:27:11 +0000292 IDLE
293 An Integrated Development Environment for Python. IDLE is a basic editor
Skip Montanarof02c5f32008-09-15 02:03:05 +0000294 and interpreter environment which ships with the standard distribution of
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000295 Python.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000296
Georg Brandl437e6a32007-08-17 06:27:11 +0000297 immutable
Skip Montanarof02c5f32008-09-15 02:03:05 +0000298 An object with a fixed value. Immutable objects include numbers, strings and
299 tuples. Such an object cannot be altered. A new object has to
Georg Brandl437e6a32007-08-17 06:27:11 +0000300 be created if a different value has to be stored. They play an important
301 role in places where a constant hash value is needed, for example as a key
302 in a dictionary.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000303
Georg Brandl437e6a32007-08-17 06:27:11 +0000304 integer division
305 Mathematical division discarding any remainder. For example, the
306 expression ``11/4`` currently evaluates to ``2`` in contrast to the
307 ``2.75`` returned by float division. Also called *floor division*.
308 When dividing two integers the outcome will always be another integer
309 (having the floor function applied to it). However, if one of the operands
310 is another numeric type (such as a :class:`float`), the result will be
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000311 coerced (see :term:`coercion`) to a common type. For example, an integer
Georg Brandl437e6a32007-08-17 06:27:11 +0000312 divided by a float will result in a float value, possibly with a decimal
313 fraction. Integer division can be forced by using the ``//`` operator
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000314 instead of the ``/`` operator. See also :term:`__future__`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000315
Georg Brandl624f3372009-03-31 16:11:45 +0000316 importer
317 An object that both finds and loads a module; both a
318 :term:`finder` and :term:`loader` object.
319
Georg Brandl437e6a32007-08-17 06:27:11 +0000320 interactive
Skip Montanarof02c5f32008-09-15 02:03:05 +0000321 Python has an interactive interpreter which means you can enter
322 statements and expressions at the interpreter prompt, immediately
323 execute them and see their results. Just launch ``python`` with no
324 arguments (possibly by selecting it from your computer's main
325 menu). It is a very powerful way to test out new ideas or inspect
326 modules and packages (remember ``help(x)``).
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000327
Georg Brandl437e6a32007-08-17 06:27:11 +0000328 interpreted
Skip Montanarof02c5f32008-09-15 02:03:05 +0000329 Python is an interpreted language, as opposed to a compiled one,
330 though the distinction can be blurry because of the presence of the
331 bytecode compiler. This means that source files can be run directly
332 without explicitly creating an executable which is then run.
333 Interpreted languages typically have a shorter development/debug cycle
334 than compiled ones, though their programs generally also run more
335 slowly. See also :term:`interactive`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000336
Georg Brandl437e6a32007-08-17 06:27:11 +0000337 iterable
338 A container object capable of returning its members one at a
339 time. Examples of iterables include all sequence types (such as
340 :class:`list`, :class:`str`, and :class:`tuple`) and some non-sequence
341 types like :class:`dict` and :class:`file` and objects of any classes you
342 define with an :meth:`__iter__` or :meth:`__getitem__` method. Iterables
343 can be used in a :keyword:`for` loop and in many other places where a
344 sequence is needed (:func:`zip`, :func:`map`, ...). When an iterable
Georg Brandld7d4fd72009-07-26 14:37:28 +0000345 object is passed as an argument to the built-in function :func:`iter`, it
Georg Brandl437e6a32007-08-17 06:27:11 +0000346 returns an iterator for the object. This iterator is good for one pass
347 over the set of values. When using iterables, it is usually not necessary
348 to call :func:`iter` or deal with iterator objects yourself. The ``for``
349 statement does that automatically for you, creating a temporary unnamed
350 variable to hold the iterator for the duration of the loop. See also
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000351 :term:`iterator`, :term:`sequence`, and :term:`generator`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000352
Georg Brandl437e6a32007-08-17 06:27:11 +0000353 iterator
354 An object representing a stream of data. Repeated calls to the iterator's
355 :meth:`next` method return successive items in the stream. When no more
Skip Montanarof02c5f32008-09-15 02:03:05 +0000356 data are available a :exc:`StopIteration` exception is raised instead. At
Georg Brandl437e6a32007-08-17 06:27:11 +0000357 this point, the iterator object is exhausted and any further calls to its
358 :meth:`next` method just raise :exc:`StopIteration` again. Iterators are
359 required to have an :meth:`__iter__` method that returns the iterator
360 object itself so every iterator is also iterable and may be used in most
361 places where other iterables are accepted. One notable exception is code
Skip Montanarof02c5f32008-09-15 02:03:05 +0000362 which attempts multiple iteration passes. A container object (such as a
Georg Brandl437e6a32007-08-17 06:27:11 +0000363 :class:`list`) produces a fresh new iterator each time you pass it to the
364 :func:`iter` function or use it in a :keyword:`for` loop. Attempting this
365 with an iterator will just return the same exhausted iterator object used
366 in the previous iteration pass, making it appear like an empty container.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000367
Georg Brandle7a09902007-10-21 12:10:28 +0000368 More information can be found in :ref:`typeiter`.
369
Georg Brandl3b85b9b2010-11-26 08:20:18 +0000370 key function
371 A key function or collation function is a callable that returns a value
372 used for sorting or ordering. For example, :func:`locale.strxfrm` is
373 used to produce a sort key that is aware of locale specific sort
374 conventions.
375
376 A number of tools in Python accept key functions to control how elements
377 are ordered or grouped. They include :func:`min`, :func:`max`,
378 :func:`sorted`, :meth:`list.sort`, :func:`heapq.nsmallest`,
379 :func:`heapq.nlargest`, and :func:`itertools.groupby`.
380
381 There are several ways to create a key function. For example. the
382 :meth:`str.lower` method can serve as a key function for case insensitive
383 sorts. Alternatively, an ad-hoc key function can be built from a
384 :keyword:`lambda` expression such as ``lambda r: (r[0], r[2])``. Also,
385 the :mod:`operator` module provides three key function constuctors:
386 :func:`~operator.attrgetter`, :func:`~operator.itemgetter`, and
387 :func:`~operator.methodcaller`. See the :ref:`Sorting HOW TO
388 <sortinghowto>` for examples of how to create and use key functions.
389
Georg Brandl584265b2007-12-02 14:58:50 +0000390 keyword argument
391 Arguments which are preceded with a ``variable_name=`` in the call.
392 The variable name designates the local name in the function to which the
393 value is assigned. ``**`` is used to accept or pass a dictionary of
394 keyword arguments. See :term:`argument`.
395
396 lambda
397 An anonymous inline function consisting of a single :term:`expression`
398 which is evaluated when the function is called. The syntax to create
399 a lambda function is ``lambda [arguments]: expression``
400
Georg Brandl437e6a32007-08-17 06:27:11 +0000401 LBYL
402 Look before you leap. This coding style explicitly tests for
403 pre-conditions before making calls or lookups. This style contrasts with
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000404 the :term:`EAFP` approach and is characterized by the presence of many
Georg Brandl437e6a32007-08-17 06:27:11 +0000405 :keyword:`if` statements.
Skip Montanaro9feab312008-09-15 02:19:53 +0000406
407 list
408 A built-in Python :term:`sequence`. Despite its name it is more akin
409 to an array in other languages than to a linked list since access to
410 elements are O(1).
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000411
Georg Brandl437e6a32007-08-17 06:27:11 +0000412 list comprehension
Skip Montanarof02c5f32008-09-15 02:03:05 +0000413 A compact way to process all or part of the elements in a sequence and
Georg Brandl437e6a32007-08-17 06:27:11 +0000414 return a list with the results. ``result = ["0x%02x" % x for x in
Skip Montanarof02c5f32008-09-15 02:03:05 +0000415 range(256) if x % 2 == 0]`` generates a list of strings containing
416 even hex numbers (0x..) in the range from 0 to 255. The :keyword:`if`
417 clause is optional. If omitted, all elements in ``range(256)`` are
418 processed.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000419
Georg Brandl624f3372009-03-31 16:11:45 +0000420 loader
421 An object that loads a module. It must define a method named
422 :meth:`load_module`. A loader is typically returned by a
423 :term:`finder`. See :pep:`302` for details.
424
Georg Brandl437e6a32007-08-17 06:27:11 +0000425 mapping
Raymond Hettingerc4c52dd2011-01-08 23:50:39 +0000426 A container object that supports arbitrary key lookups and implements the
427 methods specified in the :class:`Mapping` or :class:`MutableMapping`
Éric Araujo8fde9502011-07-29 11:34:17 +0200428 :ref:`abstract base classes <collections-abstract-base-classes>`. Examples
429 include :class:`dict`, :class:`collections.defaultdict`,
Raymond Hettingerc4c52dd2011-01-08 23:50:39 +0000430 :class:`collections.OrderedDict` and :class:`collections.Counter`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000431
Georg Brandl437e6a32007-08-17 06:27:11 +0000432 metaclass
433 The class of a class. Class definitions create a class name, a class
434 dictionary, and a list of base classes. The metaclass is responsible for
435 taking those three arguments and creating the class. Most object oriented
436 programming languages provide a default implementation. What makes Python
437 special is that it is possible to create custom metaclasses. Most users
438 never need this tool, but when the need arises, metaclasses can provide
439 powerful, elegant solutions. They have been used for logging attribute
440 access, adding thread-safety, tracking object creation, implementing
441 singletons, and many other tasks.
Georg Brandla7395032007-10-21 12:15:05 +0000442
443 More information can be found in :ref:`metaclasses`.
Georg Brandl584265b2007-12-02 14:58:50 +0000444
445 method
Skip Montanarof02c5f32008-09-15 02:03:05 +0000446 A function which is defined inside a class body. If called as an attribute
Georg Brandl584265b2007-12-02 14:58:50 +0000447 of an instance of that class, the method will get the instance object as
448 its first :term:`argument` (which is usually called ``self``).
449 See :term:`function` and :term:`nested scope`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000450
Georg Brandl437e6a32007-08-17 06:27:11 +0000451 mutable
452 Mutable objects can change their value but keep their :func:`id`. See
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000453 also :term:`immutable`.
Georg Brandle3c3db52008-01-11 09:55:53 +0000454
455 named tuple
Raymond Hettingeraff711d2009-02-04 19:25:17 +0000456 Any tuple-like class whose indexable elements are also accessible using
Raymond Hettingerc20ed512008-01-13 06:15:15 +0000457 named attributes (for example, :func:`time.localtime` returns a
Raymond Hettinger8bdd0442008-01-13 06:18:07 +0000458 tuple-like object where the *year* is accessible either with an
Raymond Hettingerc20ed512008-01-13 06:15:15 +0000459 index such as ``t[0]`` or with a named attribute like ``t.tm_year``).
460
461 A named tuple can be a built-in type such as :class:`time.struct_time`,
462 or it can be created with a regular class definition. A full featured
463 named tuple can also be created with the factory function
464 :func:`collections.namedtuple`. The latter approach automatically
465 provides extra features such as a self-documenting representation like
466 ``Employee(name='jones', title='programmer')``.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000467
Georg Brandl437e6a32007-08-17 06:27:11 +0000468 namespace
469 The place where a variable is stored. Namespaces are implemented as
Georg Brandld7d4fd72009-07-26 14:37:28 +0000470 dictionaries. There are the local, global and built-in namespaces as well
Georg Brandl437e6a32007-08-17 06:27:11 +0000471 as nested namespaces in objects (in methods). Namespaces support
472 modularity by preventing naming conflicts. For instance, the functions
473 :func:`__builtin__.open` and :func:`os.open` are distinguished by their
474 namespaces. Namespaces also aid readability and maintainability by making
475 it clear which module implements a function. For instance, writing
476 :func:`random.seed` or :func:`itertools.izip` makes it clear that those
477 functions are implemented by the :mod:`random` and :mod:`itertools`
Skip Montanarof02c5f32008-09-15 02:03:05 +0000478 modules, respectively.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000479
Georg Brandl437e6a32007-08-17 06:27:11 +0000480 nested scope
481 The ability to refer to a variable in an enclosing definition. For
482 instance, a function defined inside another function can refer to
483 variables in the outer function. Note that nested scopes work only for
484 reference and not for assignment which will always write to the innermost
485 scope. In contrast, local variables both read and write in the innermost
486 scope. Likewise, global variables read and write to the global namespace.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000487
Georg Brandl437e6a32007-08-17 06:27:11 +0000488 new-style class
Skip Montanarof02c5f32008-09-15 02:03:05 +0000489 Any class which inherits from :class:`object`. This includes all built-in
Georg Brandl437e6a32007-08-17 06:27:11 +0000490 types like :class:`list` and :class:`dict`. Only new-style classes can
491 use Python's newer, versatile features like :attr:`__slots__`,
Skip Montanarof02c5f32008-09-15 02:03:05 +0000492 descriptors, properties, and :meth:`__getattribute__`.
Georg Brandla7395032007-10-21 12:15:05 +0000493
494 More information can be found in :ref:`newstyle`.
Skip Montanaro9feab312008-09-15 02:19:53 +0000495
496 object
497 Any data with state (attributes or value) and defined behavior
498 (methods). Also the ultimate base class of any :term:`new-style
499 class`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000500
Georg Brandl584265b2007-12-02 14:58:50 +0000501 positional argument
502 The arguments assigned to local names inside a function or method,
503 determined by the order in which they were given in the call. ``*`` is
504 used to either accept multiple positional arguments (when in the
505 definition), or pass several arguments as a list to a function. See
506 :term:`argument`.
507
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000508 Python 3000
Benjamin Peterson518c44c2008-05-16 22:59:28 +0000509 Nickname for the next major Python version, 3.0 (coined long ago
510 when the release of version 3 was something in the distant future.) This
511 is also abbreviated "Py3k".
Georg Brandl437e6a32007-08-17 06:27:11 +0000512
Georg Brandl584265b2007-12-02 14:58:50 +0000513 Pythonic
Skip Montanarof02c5f32008-09-15 02:03:05 +0000514 An idea or piece of code which closely follows the most common idioms
515 of the Python language, rather than implementing code using concepts
516 common to other languages. For example, a common idiom in Python is
517 to loop over all elements of an iterable using a :keyword:`for`
518 statement. Many other languages don't have this type of construct, so
519 people unfamiliar with Python sometimes use a numerical counter instead::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000520
Georg Brandl584265b2007-12-02 14:58:50 +0000521 for i in range(len(food)):
522 print food[i]
523
524 As opposed to the cleaner, Pythonic method::
525
526 for piece in food:
527 print piece
528
Georg Brandl437e6a32007-08-17 06:27:11 +0000529 reference count
Skip Montanarof02c5f32008-09-15 02:03:05 +0000530 The number of references to an object. When the reference count of an
531 object drops to zero, it is deallocated. Reference counting is
532 generally not visible to Python code, but it is a key element of the
533 :term:`CPython` implementation. The :mod:`sys` module defines a
534 :func:`getrefcount` function that programmers can call to return the
535 reference count for a particular object.
536
Georg Brandl437e6a32007-08-17 06:27:11 +0000537 __slots__
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000538 A declaration inside a :term:`new-style class` that saves memory by
Georg Brandl437e6a32007-08-17 06:27:11 +0000539 pre-declaring space for instance attributes and eliminating instance
540 dictionaries. Though popular, the technique is somewhat tricky to get
541 right and is best reserved for rare cases where there are large numbers of
542 instances in a memory-critical application.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000543
Georg Brandl437e6a32007-08-17 06:27:11 +0000544 sequence
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000545 An :term:`iterable` which supports efficient element access using integer
Skip Montanarof02c5f32008-09-15 02:03:05 +0000546 indices via the :meth:`__getitem__` special method and defines a
547 :meth:`len` method that returns the length of the sequence.
Georg Brandl437e6a32007-08-17 06:27:11 +0000548 Some built-in sequence types are :class:`list`, :class:`str`,
549 :class:`tuple`, and :class:`unicode`. Note that :class:`dict` also
550 supports :meth:`__getitem__` and :meth:`__len__`, but is considered a
551 mapping rather than a sequence because the lookups use arbitrary
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000552 :term:`immutable` keys rather than integers.
Georg Brandl437e6a32007-08-17 06:27:11 +0000553
Georg Brandl584265b2007-12-02 14:58:50 +0000554 slice
Georg Brandl968a3e52007-12-02 18:17:50 +0000555 An object usually containing a portion of a :term:`sequence`. A slice is
Georg Brandl584265b2007-12-02 14:58:50 +0000556 created using the subscript notation, ``[]`` with colons between numbers
557 when several are given, such as in ``variable_name[1:3:5]``. The bracket
558 (subscript) notation uses :class:`slice` objects internally (or in older
559 versions, :meth:`__getslice__` and :meth:`__setslice__`).
560
Georg Brandl9a053732008-12-05 15:29:39 +0000561 special method
562 A method that is called implicitly by Python to execute a certain
563 operation on a type, such as addition. Such methods have names starting
564 and ending with double underscores. Special methods are documented in
565 :ref:`specialnames`.
566
Georg Brandl584265b2007-12-02 14:58:50 +0000567 statement
568 A statement is part of a suite (a "block" of code). A statement is either
569 an :term:`expression` or a one of several constructs with a keyword, such
570 as :keyword:`if`, :keyword:`while` or :keyword:`print`.
571
Skip Montanaro9feab312008-09-15 02:19:53 +0000572 triple-quoted string
573 A string which is bound by three instances of either a quotation mark
574 (") or an apostrophe ('). While they don't provide any functionality
575 not available with single-quoted strings, they are useful for a number
576 of reasons. They allow you to include unescaped single and double
577 quotes within a string and they can span multiple lines without the
578 use of the continuation character, making them especially useful when
579 writing docstrings.
580
Georg Brandl437e6a32007-08-17 06:27:11 +0000581 type
582 The type of a Python object determines what kind of object it is; every
583 object has a type. An object's type is accessible as its
584 :attr:`__class__` attribute or can be retrieved with ``type(obj)``.
Skip Montanarof02c5f32008-09-15 02:03:05 +0000585
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +0000586 view
587 The objects returned from :meth:`dict.viewkeys`, :meth:`dict.viewvalues`,
588 and :meth:`dict.viewitems` are called dictionary views. They are lazy
589 sequences that will see changes in the underlying dictionary. To force
590 the dictionary view to become a full list use ``list(dictview)``. See
591 :ref:`dict-views`.
592
Skip Montanarof02c5f32008-09-15 02:03:05 +0000593 virtual machine
594 A computer defined entirely in software. Python's virtual machine
595 executes the :term:`bytecode` emitted by the bytecode compiler.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000596
Georg Brandl437e6a32007-08-17 06:27:11 +0000597 Zen of Python
598 Listing of Python design principles and philosophies that are helpful in
599 understanding and using the language. The listing can be found by typing
600 "``import this``" at the interactive prompt.