blob: eab35f739c967b8fa6f826e87fb8e70835d4b4c2 [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
Raymond Hettingeracdafa82010-11-30 17:50:53 +000030 :ref:`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
32 :func:`hasattr` would be clumsy. Python comes with many built-in ABCs for
33 data structures (in the :mod:`collections` module), numbers (in the
34 :mod:`numbers` module), and streams (in the :mod:`io` module). You can
35 create your own ABC with the :mod:`abc` module.
Benjamin Petersonaac51b82008-07-01 23:33:06 +000036
Georg Brandl584265b2007-12-02 14:58:50 +000037 argument
Skip Montanarof02c5f32008-09-15 02:03:05 +000038 A value passed to a function or method, assigned to a named local
39 variable in the function body. A function or method may have both
40 positional arguments and keyword arguments in its definition.
41 Positional and keyword arguments may be variable-length: ``*`` accepts
42 or passes (if in the function definition or call) several positional
43 arguments in a list, while ``**`` does the same for keyword arguments
44 in a dictionary.
Georg Brandl584265b2007-12-02 14:58:50 +000045
46 Any expression may be used within the argument list, and the evaluated
47 value is passed to the local variable.
Skip Montanaro9feab312008-09-15 02:19:53 +000048
49 attribute
50 A value associated with an object which is referenced by name using
51 dotted expressions. For example, if an object *o* has an attribute
52 *a* it would be referenced as *o.a*.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000053
Georg Brandl437e6a32007-08-17 06:27:11 +000054 BDFL
55 Benevolent Dictator For Life, a.k.a. `Guido van Rossum
56 <http://www.python.org/~guido/>`_, Python's creator.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000057
Georg Brandl63fa1682007-10-21 10:24:20 +000058 bytecode
59 Python source code is compiled into bytecode, the internal representation
60 of a Python program in the interpreter. The bytecode is also cached in
61 ``.pyc`` and ``.pyo`` files so that executing the same file is faster the
62 second time (recompilation from source to bytecode can be avoided). This
Skip Montanarof02c5f32008-09-15 02:03:05 +000063 "intermediate language" is said to run on a :term:`virtual machine`
64 that executes the machine code corresponding to each bytecode.
Skip Montanaro9feab312008-09-15 02:19:53 +000065
Georg Brandl2b4eda42010-07-03 10:25:54 +000066 A list of bytecode instructions can be found in the documentation for
67 :ref:`the dis module <bytecodes>`.
68
Skip Montanaro9feab312008-09-15 02:19:53 +000069 class
70 A template for creating user-defined objects. Class definitions
71 normally contain method definitions which operate on instances of the
72 class.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000073
Georg Brandl437e6a32007-08-17 06:27:11 +000074 classic class
75 Any class which does not inherit from :class:`object`. See
Skip Montanarof02c5f32008-09-15 02:03:05 +000076 :term:`new-style class`. Classic classes will be removed in Python 3.0.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000077
Georg Brandl437e6a32007-08-17 06:27:11 +000078 coercion
79 The implicit conversion of an instance of one type to another during an
80 operation which involves two arguments of the same type. For example,
81 ``int(3.15)`` converts the floating point number to the integer ``3``, but
82 in ``3+4.5``, each argument is of a different type (one int, one float),
83 and both must be converted to the same type before they can be added or it
84 will raise a ``TypeError``. Coercion between two operands can be
Georg Brandld7d4fd72009-07-26 14:37:28 +000085 performed with the ``coerce`` built-in function; thus, ``3+4.5`` is
Georg Brandl437e6a32007-08-17 06:27:11 +000086 equivalent to calling ``operator.add(*coerce(3, 4.5))`` and results in
87 ``operator.add(3.0, 4.5)``. Without coercion, all arguments of even
88 compatible types would have to be normalized to the same value by the
89 programmer, e.g., ``float(3)+4.5`` rather than just ``3+4.5``.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000090
Georg Brandl437e6a32007-08-17 06:27:11 +000091 complex number
92 An extension of the familiar real number system in which all numbers are
93 expressed as a sum of a real part and an imaginary part. Imaginary
94 numbers are real multiples of the imaginary unit (the square root of
95 ``-1``), often written ``i`` in mathematics or ``j`` in
Georg Brandld7d4fd72009-07-26 14:37:28 +000096 engineering. Python has built-in support for complex numbers, which are
Georg Brandl437e6a32007-08-17 06:27:11 +000097 written with this latter notation; the imaginary part is written with a
98 ``j`` suffix, e.g., ``3+1j``. To get access to complex equivalents of the
99 :mod:`math` module, use :mod:`cmath`. Use of complex numbers is a fairly
100 advanced mathematical feature. If you're not aware of a need for them,
101 it's almost certain you can safely ignore them.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000102
Skip Montanaroffe455c2007-12-08 15:23:31 +0000103 context manager
Skip Montanarof02c5f32008-09-15 02:03:05 +0000104 An object which controls the environment seen in a :keyword:`with`
Skip Montanaroffe455c2007-12-08 15:23:31 +0000105 statement by defining :meth:`__enter__` and :meth:`__exit__` methods.
106 See :pep:`343`.
107
Skip Montanarof02c5f32008-09-15 02:03:05 +0000108 CPython
Antoine Pitrou9f41bb32011-01-06 16:35:14 +0000109 The canonical implementation of the Python programming language, as
110 distributed on `python.org <http://python.org>`_. The term "CPython"
111 is used when necessary to distinguish this implementation from others
112 such as Jython or IronPython.
Skip Montanarof02c5f32008-09-15 02:03:05 +0000113
Georg Brandl584265b2007-12-02 14:58:50 +0000114 decorator
115 A function returning another function, usually applied as a function
116 transformation using the ``@wrapper`` syntax. Common examples for
117 decorators are :func:`classmethod` and :func:`staticmethod`.
118
119 The decorator syntax is merely syntactic sugar, the following two
120 function definitions are semantically equivalent::
121
122 def f(...):
123 ...
124 f = staticmethod(f)
125
126 @staticmethod
127 def f(...):
128 ...
129
Georg Brandl5066c0c2008-12-05 18:00:06 +0000130 See :ref:`the documentation for function definition <function>` for more
131 about decorators.
132
Georg Brandl437e6a32007-08-17 06:27:11 +0000133 descriptor
Skip Montanarof02c5f32008-09-15 02:03:05 +0000134 Any *new-style* object which defines the methods :meth:`__get__`,
Georg Brandl5e52db02007-10-21 10:45:46 +0000135 :meth:`__set__`, or :meth:`__delete__`. When a class attribute is a
Georg Brandl437e6a32007-08-17 06:27:11 +0000136 descriptor, its special binding behavior is triggered upon attribute
Georg Brandl5e52db02007-10-21 10:45:46 +0000137 lookup. Normally, using *a.b* to get, set or delete an attribute looks up
138 the object named *b* in the class dictionary for *a*, but if *b* is a
139 descriptor, the respective descriptor method gets called. Understanding
140 descriptors is a key to a deep understanding of Python because they are
141 the basis for many features including functions, methods, properties,
142 class methods, static methods, and reference to super classes.
143
144 For more information about descriptors' methods, see :ref:`descriptors`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000145
Georg Brandl437e6a32007-08-17 06:27:11 +0000146 dictionary
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000147 An associative array, where arbitrary keys are mapped to values. The keys
148 can be any object with :meth:`__hash__` function and :meth:`__eq__`
149 methods. Called a hash in Perl.
Georg Brandle64f7382008-07-20 11:50:29 +0000150
151 docstring
Skip Montanarof02c5f32008-09-15 02:03:05 +0000152 A string literal which appears as the first expression in a class,
153 function or module. While ignored when the suite is executed, it is
154 recognized by the compiler and put into the :attr:`__doc__` attribute
155 of the enclosing class, function or module. Since it is available via
156 introspection, it is the canonical place for documentation of the
Georg Brandle64f7382008-07-20 11:50:29 +0000157 object.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000158
159 duck-typing
Georg Brandle85e1ae2010-10-06 09:17:24 +0000160 A programming style which does not look at an object's type to determine
161 if it has the right interface; instead, the method or attribute is simply
162 called or used ("If it looks like a duck and quacks like a duck, it
Georg Brandl437e6a32007-08-17 06:27:11 +0000163 must be a duck.") By emphasizing interfaces rather than specific types,
164 well-designed code improves its flexibility by allowing polymorphic
165 substitution. Duck-typing avoids tests using :func:`type` or
Georg Brandl04eba2c2010-07-11 08:56:18 +0000166 :func:`isinstance`. (Note, however, that duck-typing can be complemented
167 with :term:`abstract base class`\ es.) Instead, it typically employs
168 :func:`hasattr` tests or :term:`EAFP` programming.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000169
Georg Brandl437e6a32007-08-17 06:27:11 +0000170 EAFP
171 Easier to ask for forgiveness than permission. This common Python coding
172 style assumes the existence of valid keys or attributes and catches
173 exceptions if the assumption proves false. This clean and fast style is
174 characterized by the presence of many :keyword:`try` and :keyword:`except`
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000175 statements. The technique contrasts with the :term:`LBYL` style
Skip Montanarof02c5f32008-09-15 02:03:05 +0000176 common to many other languages such as C.
Georg Brandl437e6a32007-08-17 06:27:11 +0000177
Georg Brandl584265b2007-12-02 14:58:50 +0000178 expression
179 A piece of syntax which can be evaluated to some value. In other words,
180 an expression is an accumulation of expression elements like literals, names,
Skip Montanarof02c5f32008-09-15 02:03:05 +0000181 attribute access, operators or function calls which all return a value.
182 In contrast to many other languages, not all language constructs are expressions.
183 There are also :term:`statement`\s which cannot be used as expressions,
184 such as :keyword:`print` or :keyword:`if`. Assignments are also statements,
185 not expressions.
Georg Brandl584265b2007-12-02 14:58:50 +0000186
Georg Brandl437e6a32007-08-17 06:27:11 +0000187 extension module
Skip Montanarof02c5f32008-09-15 02:03:05 +0000188 A module written in C or C++, using Python's C API to interact with the core and
Georg Brandl437e6a32007-08-17 06:27:11 +0000189 with user code.
Georg Brandl584265b2007-12-02 14:58:50 +0000190
Georg Brandl624f3372009-03-31 16:11:45 +0000191 finder
192 An object that tries to find the :term:`loader` for a module. It must
193 implement a method named :meth:`find_module`. See :pep:`302` for
194 details.
195
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000196 floor division
197 Mathematical division that rounds down to nearest integer. The floor
198 division operator is ``//``. For example, the expression ``11 // 4``
199 evaluates to ``2`` in contrast to the ``2.75`` returned by float true
200 division. Note that ``(-11) // 4`` is ``-3`` because that is ``-2.75``
201 rounded *downward*. See :pep:`238`.
202
Georg Brandl584265b2007-12-02 14:58:50 +0000203 function
204 A series of statements which returns some value to a caller. It can also
205 be passed zero or more arguments which may be used in the execution of
206 the body. See also :term:`argument` and :term:`method`.
207
Georg Brandl437e6a32007-08-17 06:27:11 +0000208 __future__
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000209 A pseudo-module which programmers can use to enable new language features
Georg Brandl437e6a32007-08-17 06:27:11 +0000210 which are not compatible with the current interpreter. For example, the
211 expression ``11/4`` currently evaluates to ``2``. If the module in which
212 it is executed had enabled *true division* by executing::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000213
Georg Brandl437e6a32007-08-17 06:27:11 +0000214 from __future__ import division
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000215
Georg Brandl437e6a32007-08-17 06:27:11 +0000216 the expression ``11/4`` would evaluate to ``2.75``. By importing the
217 :mod:`__future__` module and evaluating its variables, you can see when a
218 new feature was first added to the language and when it will become the
219 default::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000220
Georg Brandl437e6a32007-08-17 06:27:11 +0000221 >>> import __future__
222 >>> __future__.division
223 _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
224
225 garbage collection
226 The process of freeing memory when it is not used anymore. Python
227 performs garbage collection via reference counting and a cyclic garbage
228 collector that is able to detect and break reference cycles.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000229
Georg Brandlea2d3892010-04-02 09:11:49 +0000230 .. index:: single: generator
231
Georg Brandl437e6a32007-08-17 06:27:11 +0000232 generator
Skip Montanarof02c5f32008-09-15 02:03:05 +0000233 A function which returns an iterator. It looks like a normal function
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000234 except that it contains :keyword:`yield` statements for producing a series
235 a values usable in a for-loop or that can be retrieved one at a time with
236 the :func:`next` function. Each :keyword:`yield` temporarily suspends
237 processing, remembering the location execution state (including local
238 variables and pending try-statements). When the generator resumes, it
239 picks-up where it left-off (in contrast to functions which start fresh on
240 every invocation).
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000241
Georg Brandl437e6a32007-08-17 06:27:11 +0000242 .. index:: single: generator expression
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000243
Georg Brandl437e6a32007-08-17 06:27:11 +0000244 generator expression
Georg Brandlea2d3892010-04-02 09:11:49 +0000245 An expression that returns an iterator. It looks like a normal expression
Georg Brandl437e6a32007-08-17 06:27:11 +0000246 followed by a :keyword:`for` expression defining a loop variable, range,
247 and an optional :keyword:`if` expression. The combined expression
248 generates values for an enclosing function::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000249
Georg Brandl437e6a32007-08-17 06:27:11 +0000250 >>> sum(i*i for i in range(10)) # sum of squares 0, 1, 4, ... 81
251 285
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000252
Georg Brandl437e6a32007-08-17 06:27:11 +0000253 GIL
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000254 See :term:`global interpreter lock`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000255
Georg Brandl437e6a32007-08-17 06:27:11 +0000256 global interpreter lock
Antoine Pitrou9f41bb32011-01-06 16:35:14 +0000257 The mechanism used by the :term:`CPython` interpreter to assure that
258 only one thread executes Python :term:`bytecode` at a time.
259 This simplifies the CPython implementation by making the object model
260 (including critical built-in types such as :class:`dict`) implicitly
261 safe against concurrent access. Locking the entire interpreter
262 makes it easier for the interpreter to be multi-threaded, at the
263 expense of much of the parallelism afforded by multi-processor
264 machines.
265
266 However, some extension modules, either standard or third-party,
267 are designed so as to release the GIL when doing computationally-intensive
268 tasks such as compression or hashing. Also, the GIL is always released
269 when doing I/O.
270
271 Past efforts to create a "free-threaded" interpreter (one which locks
272 shared data at a much finer granularity) have not been successful
273 because performance suffered in the common single-processor case. It
274 is believed that overcoming this performance issue would make the
275 implementation much more complicated and therefore costlier to maintain.
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000276
277 hashable
Skip Montanarof02c5f32008-09-15 02:03:05 +0000278 An object is *hashable* if it has a hash value which never changes during
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000279 its lifetime (it needs a :meth:`__hash__` method), and can be compared to
280 other objects (it needs an :meth:`__eq__` or :meth:`__cmp__` method).
Skip Montanarof02c5f32008-09-15 02:03:05 +0000281 Hashable objects which compare equal must have the same hash value.
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000282
283 Hashability makes an object usable as a dictionary key and a set member,
284 because these data structures use the hash value internally.
285
Skip Montanarof02c5f32008-09-15 02:03:05 +0000286 All of Python's immutable built-in objects are hashable, while no mutable
287 containers (such as lists or dictionaries) are. Objects which are
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000288 instances of user-defined classes are hashable by default; they all
289 compare unequal, and their hash value is their :func:`id`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000290
Georg Brandl437e6a32007-08-17 06:27:11 +0000291 IDLE
292 An Integrated Development Environment for Python. IDLE is a basic editor
Skip Montanarof02c5f32008-09-15 02:03:05 +0000293 and interpreter environment which ships with the standard distribution of
Raymond Hettingerf1b678d2010-09-01 22:25:41 +0000294 Python.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000295
Georg Brandl437e6a32007-08-17 06:27:11 +0000296 immutable
Skip Montanarof02c5f32008-09-15 02:03:05 +0000297 An object with a fixed value. Immutable objects include numbers, strings and
298 tuples. Such an object cannot be altered. A new object has to
Georg Brandl437e6a32007-08-17 06:27:11 +0000299 be created if a different value has to be stored. They play an important
300 role in places where a constant hash value is needed, for example as a key
301 in a dictionary.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000302
Georg Brandl437e6a32007-08-17 06:27:11 +0000303 integer division
304 Mathematical division discarding any remainder. For example, the
305 expression ``11/4`` currently evaluates to ``2`` in contrast to the
306 ``2.75`` returned by float division. Also called *floor division*.
307 When dividing two integers the outcome will always be another integer
308 (having the floor function applied to it). However, if one of the operands
309 is another numeric type (such as a :class:`float`), the result will be
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000310 coerced (see :term:`coercion`) to a common type. For example, an integer
Georg Brandl437e6a32007-08-17 06:27:11 +0000311 divided by a float will result in a float value, possibly with a decimal
312 fraction. Integer division can be forced by using the ``//`` operator
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000313 instead of the ``/`` operator. See also :term:`__future__`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000314
Georg Brandl624f3372009-03-31 16:11:45 +0000315 importer
316 An object that both finds and loads a module; both a
317 :term:`finder` and :term:`loader` object.
318
Georg Brandl437e6a32007-08-17 06:27:11 +0000319 interactive
Skip Montanarof02c5f32008-09-15 02:03:05 +0000320 Python has an interactive interpreter which means you can enter
321 statements and expressions at the interpreter prompt, immediately
322 execute them and see their results. Just launch ``python`` with no
323 arguments (possibly by selecting it from your computer's main
324 menu). It is a very powerful way to test out new ideas or inspect
325 modules and packages (remember ``help(x)``).
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000326
Georg Brandl437e6a32007-08-17 06:27:11 +0000327 interpreted
Skip Montanarof02c5f32008-09-15 02:03:05 +0000328 Python is an interpreted language, as opposed to a compiled one,
329 though the distinction can be blurry because of the presence of the
330 bytecode compiler. This means that source files can be run directly
331 without explicitly creating an executable which is then run.
332 Interpreted languages typically have a shorter development/debug cycle
333 than compiled ones, though their programs generally also run more
334 slowly. See also :term:`interactive`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000335
Georg Brandl437e6a32007-08-17 06:27:11 +0000336 iterable
337 A container object capable of returning its members one at a
338 time. Examples of iterables include all sequence types (such as
339 :class:`list`, :class:`str`, and :class:`tuple`) and some non-sequence
340 types like :class:`dict` and :class:`file` and objects of any classes you
341 define with an :meth:`__iter__` or :meth:`__getitem__` method. Iterables
342 can be used in a :keyword:`for` loop and in many other places where a
343 sequence is needed (:func:`zip`, :func:`map`, ...). When an iterable
Georg Brandld7d4fd72009-07-26 14:37:28 +0000344 object is passed as an argument to the built-in function :func:`iter`, it
Georg Brandl437e6a32007-08-17 06:27:11 +0000345 returns an iterator for the object. This iterator is good for one pass
346 over the set of values. When using iterables, it is usually not necessary
347 to call :func:`iter` or deal with iterator objects yourself. The ``for``
348 statement does that automatically for you, creating a temporary unnamed
349 variable to hold the iterator for the duration of the loop. See also
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000350 :term:`iterator`, :term:`sequence`, and :term:`generator`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000351
Georg Brandl437e6a32007-08-17 06:27:11 +0000352 iterator
353 An object representing a stream of data. Repeated calls to the iterator's
354 :meth:`next` method return successive items in the stream. When no more
Skip Montanarof02c5f32008-09-15 02:03:05 +0000355 data are available a :exc:`StopIteration` exception is raised instead. At
Georg Brandl437e6a32007-08-17 06:27:11 +0000356 this point, the iterator object is exhausted and any further calls to its
357 :meth:`next` method just raise :exc:`StopIteration` again. Iterators are
358 required to have an :meth:`__iter__` method that returns the iterator
359 object itself so every iterator is also iterable and may be used in most
360 places where other iterables are accepted. One notable exception is code
Skip Montanarof02c5f32008-09-15 02:03:05 +0000361 which attempts multiple iteration passes. A container object (such as a
Georg Brandl437e6a32007-08-17 06:27:11 +0000362 :class:`list`) produces a fresh new iterator each time you pass it to the
363 :func:`iter` function or use it in a :keyword:`for` loop. Attempting this
364 with an iterator will just return the same exhausted iterator object used
365 in the previous iteration pass, making it appear like an empty container.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000366
Georg Brandle7a09902007-10-21 12:10:28 +0000367 More information can be found in :ref:`typeiter`.
368
Georg Brandl3b85b9b2010-11-26 08:20:18 +0000369 key function
370 A key function or collation function is a callable that returns a value
371 used for sorting or ordering. For example, :func:`locale.strxfrm` is
372 used to produce a sort key that is aware of locale specific sort
373 conventions.
374
375 A number of tools in Python accept key functions to control how elements
376 are ordered or grouped. They include :func:`min`, :func:`max`,
377 :func:`sorted`, :meth:`list.sort`, :func:`heapq.nsmallest`,
378 :func:`heapq.nlargest`, and :func:`itertools.groupby`.
379
380 There are several ways to create a key function. For example. the
381 :meth:`str.lower` method can serve as a key function for case insensitive
382 sorts. Alternatively, an ad-hoc key function can be built from a
383 :keyword:`lambda` expression such as ``lambda r: (r[0], r[2])``. Also,
384 the :mod:`operator` module provides three key function constuctors:
385 :func:`~operator.attrgetter`, :func:`~operator.itemgetter`, and
386 :func:`~operator.methodcaller`. See the :ref:`Sorting HOW TO
387 <sortinghowto>` for examples of how to create and use key functions.
388
Georg Brandl584265b2007-12-02 14:58:50 +0000389 keyword argument
390 Arguments which are preceded with a ``variable_name=`` in the call.
391 The variable name designates the local name in the function to which the
392 value is assigned. ``**`` is used to accept or pass a dictionary of
393 keyword arguments. See :term:`argument`.
394
Raymond Hettinger0d6fa4d2010-11-06 00:06:14 +0000395 key function
396 A key function or collation function is a callable that returns a value
397 used for sorting or ordering. For example, :func:`locale.strxfrm` is
398 used to produce a sort key that is aware of locale specific sort
399 conventions.
400
401 A number of tools in Python accept key functions to control how elements
402 are ordered or grouped. They include :func:`min`, :func:`max`,
403 :func:`sorted`, :meth:`list.sort`, :func:`heapq.nsmallest`,
404 :func:`heapq.nlargest`, and :func:`itertools.groupby`.
405
406 There are several ways to create a key function. For example. the
407 :meth:`str.lower` method can serve as a key function for case insensitive
408 sorts. Alternatively, an ad-hoc key function can be built from a
409 :keyword:`lambda` expression such as ``lambda r: (r[0], r[2])``. Also,
410 the :mod:`operator` module provides three key function constuctors:
411 :func:`~operator.attrgetter`, :func:`~operator.itemgetter`, and
Georg Brandl3b85b9b2010-11-26 08:20:18 +0000412 :func:`~operator.methodcaller`. See the :ref:`sortinghowto` for
Raymond Hettinger0d6fa4d2010-11-06 00:06:14 +0000413 examples of how to create and use key functions.
414
Georg Brandl584265b2007-12-02 14:58:50 +0000415 lambda
416 An anonymous inline function consisting of a single :term:`expression`
417 which is evaluated when the function is called. The syntax to create
418 a lambda function is ``lambda [arguments]: expression``
419
Georg Brandl437e6a32007-08-17 06:27:11 +0000420 LBYL
421 Look before you leap. This coding style explicitly tests for
422 pre-conditions before making calls or lookups. This style contrasts with
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000423 the :term:`EAFP` approach and is characterized by the presence of many
Georg Brandl437e6a32007-08-17 06:27:11 +0000424 :keyword:`if` statements.
Skip Montanaro9feab312008-09-15 02:19:53 +0000425
426 list
427 A built-in Python :term:`sequence`. Despite its name it is more akin
428 to an array in other languages than to a linked list since access to
429 elements are O(1).
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000430
Georg Brandl437e6a32007-08-17 06:27:11 +0000431 list comprehension
Skip Montanarof02c5f32008-09-15 02:03:05 +0000432 A compact way to process all or part of the elements in a sequence and
Georg Brandl437e6a32007-08-17 06:27:11 +0000433 return a list with the results. ``result = ["0x%02x" % x for x in
Skip Montanarof02c5f32008-09-15 02:03:05 +0000434 range(256) if x % 2 == 0]`` generates a list of strings containing
435 even hex numbers (0x..) in the range from 0 to 255. The :keyword:`if`
436 clause is optional. If omitted, all elements in ``range(256)`` are
437 processed.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000438
Georg Brandl624f3372009-03-31 16:11:45 +0000439 loader
440 An object that loads a module. It must define a method named
441 :meth:`load_module`. A loader is typically returned by a
442 :term:`finder`. See :pep:`302` for details.
443
Georg Brandl437e6a32007-08-17 06:27:11 +0000444 mapping
Raymond Hettingerc4c52dd2011-01-08 23:50:39 +0000445 A container object that supports arbitrary key lookups and implements the
446 methods specified in the :class:`Mapping` or :class:`MutableMapping`
447 :ref:`abstract base classes <abstract-base-classes>`. Examples include
448 :class:`dict`, :class:`collections.defaultdict`,
449 :class:`collections.OrderedDict` and :class:`collections.Counter`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000450
Georg Brandl437e6a32007-08-17 06:27:11 +0000451 metaclass
452 The class of a class. Class definitions create a class name, a class
453 dictionary, and a list of base classes. The metaclass is responsible for
454 taking those three arguments and creating the class. Most object oriented
455 programming languages provide a default implementation. What makes Python
456 special is that it is possible to create custom metaclasses. Most users
457 never need this tool, but when the need arises, metaclasses can provide
458 powerful, elegant solutions. They have been used for logging attribute
459 access, adding thread-safety, tracking object creation, implementing
460 singletons, and many other tasks.
Georg Brandla7395032007-10-21 12:15:05 +0000461
462 More information can be found in :ref:`metaclasses`.
Georg Brandl584265b2007-12-02 14:58:50 +0000463
464 method
Skip Montanarof02c5f32008-09-15 02:03:05 +0000465 A function which is defined inside a class body. If called as an attribute
Georg Brandl584265b2007-12-02 14:58:50 +0000466 of an instance of that class, the method will get the instance object as
467 its first :term:`argument` (which is usually called ``self``).
468 See :term:`function` and :term:`nested scope`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000469
Georg Brandl437e6a32007-08-17 06:27:11 +0000470 mutable
471 Mutable objects can change their value but keep their :func:`id`. See
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000472 also :term:`immutable`.
Georg Brandle3c3db52008-01-11 09:55:53 +0000473
474 named tuple
Raymond Hettingeraff711d2009-02-04 19:25:17 +0000475 Any tuple-like class whose indexable elements are also accessible using
Raymond Hettingerc20ed512008-01-13 06:15:15 +0000476 named attributes (for example, :func:`time.localtime` returns a
Raymond Hettinger8bdd0442008-01-13 06:18:07 +0000477 tuple-like object where the *year* is accessible either with an
Raymond Hettingerc20ed512008-01-13 06:15:15 +0000478 index such as ``t[0]`` or with a named attribute like ``t.tm_year``).
479
480 A named tuple can be a built-in type such as :class:`time.struct_time`,
481 or it can be created with a regular class definition. A full featured
482 named tuple can also be created with the factory function
483 :func:`collections.namedtuple`. The latter approach automatically
484 provides extra features such as a self-documenting representation like
485 ``Employee(name='jones', title='programmer')``.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000486
Georg Brandl437e6a32007-08-17 06:27:11 +0000487 namespace
488 The place where a variable is stored. Namespaces are implemented as
Georg Brandld7d4fd72009-07-26 14:37:28 +0000489 dictionaries. There are the local, global and built-in namespaces as well
Georg Brandl437e6a32007-08-17 06:27:11 +0000490 as nested namespaces in objects (in methods). Namespaces support
491 modularity by preventing naming conflicts. For instance, the functions
492 :func:`__builtin__.open` and :func:`os.open` are distinguished by their
493 namespaces. Namespaces also aid readability and maintainability by making
494 it clear which module implements a function. For instance, writing
495 :func:`random.seed` or :func:`itertools.izip` makes it clear that those
496 functions are implemented by the :mod:`random` and :mod:`itertools`
Skip Montanarof02c5f32008-09-15 02:03:05 +0000497 modules, respectively.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000498
Georg Brandl437e6a32007-08-17 06:27:11 +0000499 nested scope
500 The ability to refer to a variable in an enclosing definition. For
501 instance, a function defined inside another function can refer to
502 variables in the outer function. Note that nested scopes work only for
503 reference and not for assignment which will always write to the innermost
504 scope. In contrast, local variables both read and write in the innermost
505 scope. Likewise, global variables read and write to the global namespace.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000506
Georg Brandl437e6a32007-08-17 06:27:11 +0000507 new-style class
Skip Montanarof02c5f32008-09-15 02:03:05 +0000508 Any class which inherits from :class:`object`. This includes all built-in
Georg Brandl437e6a32007-08-17 06:27:11 +0000509 types like :class:`list` and :class:`dict`. Only new-style classes can
510 use Python's newer, versatile features like :attr:`__slots__`,
Skip Montanarof02c5f32008-09-15 02:03:05 +0000511 descriptors, properties, and :meth:`__getattribute__`.
Georg Brandla7395032007-10-21 12:15:05 +0000512
513 More information can be found in :ref:`newstyle`.
Skip Montanaro9feab312008-09-15 02:19:53 +0000514
515 object
516 Any data with state (attributes or value) and defined behavior
517 (methods). Also the ultimate base class of any :term:`new-style
518 class`.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000519
Georg Brandl584265b2007-12-02 14:58:50 +0000520 positional argument
521 The arguments assigned to local names inside a function or method,
522 determined by the order in which they were given in the call. ``*`` is
523 used to either accept multiple positional arguments (when in the
524 definition), or pass several arguments as a list to a function. See
525 :term:`argument`.
526
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000527 Python 3000
Benjamin Peterson518c44c2008-05-16 22:59:28 +0000528 Nickname for the next major Python version, 3.0 (coined long ago
529 when the release of version 3 was something in the distant future.) This
530 is also abbreviated "Py3k".
Georg Brandl437e6a32007-08-17 06:27:11 +0000531
Georg Brandl584265b2007-12-02 14:58:50 +0000532 Pythonic
Skip Montanarof02c5f32008-09-15 02:03:05 +0000533 An idea or piece of code which closely follows the most common idioms
534 of the Python language, rather than implementing code using concepts
535 common to other languages. For example, a common idiom in Python is
536 to loop over all elements of an iterable using a :keyword:`for`
537 statement. Many other languages don't have this type of construct, so
538 people unfamiliar with Python sometimes use a numerical counter instead::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000539
Georg Brandl584265b2007-12-02 14:58:50 +0000540 for i in range(len(food)):
541 print food[i]
542
543 As opposed to the cleaner, Pythonic method::
544
545 for piece in food:
546 print piece
547
Georg Brandl437e6a32007-08-17 06:27:11 +0000548 reference count
Skip Montanarof02c5f32008-09-15 02:03:05 +0000549 The number of references to an object. When the reference count of an
550 object drops to zero, it is deallocated. Reference counting is
551 generally not visible to Python code, but it is a key element of the
552 :term:`CPython` implementation. The :mod:`sys` module defines a
553 :func:`getrefcount` function that programmers can call to return the
554 reference count for a particular object.
555
Georg Brandl437e6a32007-08-17 06:27:11 +0000556 __slots__
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000557 A declaration inside a :term:`new-style class` that saves memory by
Georg Brandl437e6a32007-08-17 06:27:11 +0000558 pre-declaring space for instance attributes and eliminating instance
559 dictionaries. Though popular, the technique is somewhat tricky to get
560 right and is best reserved for rare cases where there are large numbers of
561 instances in a memory-critical application.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000562
Georg Brandl437e6a32007-08-17 06:27:11 +0000563 sequence
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000564 An :term:`iterable` which supports efficient element access using integer
Skip Montanarof02c5f32008-09-15 02:03:05 +0000565 indices via the :meth:`__getitem__` special method and defines a
566 :meth:`len` method that returns the length of the sequence.
Georg Brandl437e6a32007-08-17 06:27:11 +0000567 Some built-in sequence types are :class:`list`, :class:`str`,
568 :class:`tuple`, and :class:`unicode`. Note that :class:`dict` also
569 supports :meth:`__getitem__` and :meth:`__len__`, but is considered a
570 mapping rather than a sequence because the lookups use arbitrary
Georg Brandl6c82b6c2007-08-17 16:54:59 +0000571 :term:`immutable` keys rather than integers.
Georg Brandl437e6a32007-08-17 06:27:11 +0000572
Georg Brandl584265b2007-12-02 14:58:50 +0000573 slice
Georg Brandl968a3e52007-12-02 18:17:50 +0000574 An object usually containing a portion of a :term:`sequence`. A slice is
Georg Brandl584265b2007-12-02 14:58:50 +0000575 created using the subscript notation, ``[]`` with colons between numbers
576 when several are given, such as in ``variable_name[1:3:5]``. The bracket
577 (subscript) notation uses :class:`slice` objects internally (or in older
578 versions, :meth:`__getslice__` and :meth:`__setslice__`).
579
Georg Brandl9a053732008-12-05 15:29:39 +0000580 special method
581 A method that is called implicitly by Python to execute a certain
582 operation on a type, such as addition. Such methods have names starting
583 and ending with double underscores. Special methods are documented in
584 :ref:`specialnames`.
585
Georg Brandl584265b2007-12-02 14:58:50 +0000586 statement
587 A statement is part of a suite (a "block" of code). A statement is either
588 an :term:`expression` or a one of several constructs with a keyword, such
589 as :keyword:`if`, :keyword:`while` or :keyword:`print`.
590
Skip Montanaro9feab312008-09-15 02:19:53 +0000591 triple-quoted string
592 A string which is bound by three instances of either a quotation mark
593 (") or an apostrophe ('). While they don't provide any functionality
594 not available with single-quoted strings, they are useful for a number
595 of reasons. They allow you to include unescaped single and double
596 quotes within a string and they can span multiple lines without the
597 use of the continuation character, making them especially useful when
598 writing docstrings.
599
Georg Brandl437e6a32007-08-17 06:27:11 +0000600 type
601 The type of a Python object determines what kind of object it is; every
602 object has a type. An object's type is accessible as its
603 :attr:`__class__` attribute or can be retrieved with ``type(obj)``.
Skip Montanarof02c5f32008-09-15 02:03:05 +0000604
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +0000605 view
606 The objects returned from :meth:`dict.viewkeys`, :meth:`dict.viewvalues`,
607 and :meth:`dict.viewitems` are called dictionary views. They are lazy
608 sequences that will see changes in the underlying dictionary. To force
609 the dictionary view to become a full list use ``list(dictview)``. See
610 :ref:`dict-views`.
611
Skip Montanarof02c5f32008-09-15 02:03:05 +0000612 virtual machine
613 A computer defined entirely in software. Python's virtual machine
614 executes the :term:`bytecode` emitted by the bytecode compiler.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000615
Georg Brandl437e6a32007-08-17 06:27:11 +0000616 Zen of Python
617 Listing of Python design principles and philosophies that are helpful in
618 understanding and using the language. The listing can be found by typing
619 "``import this``" at the interactive prompt.