| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 1 | .. _glossary: |
| 2 | |
| 3 | ******** |
| 4 | Glossary |
| 5 | ******** |
| 6 | |
| 7 | .. if you add new entries, keep the alphabetical sorting! |
| 8 | |
| 9 | .. glossary:: |
| 10 | |
| 11 | ``>>>`` |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 12 | The default Python prompt of the interactive shell. Often seen for code |
| 13 | examples which can be executed interactively in the interpreter. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 14 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 15 | ``...`` |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 16 | 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 Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 19 | |
| Georg Brandl | 5a42ca6 | 2008-05-20 07:20:12 +0000 | [diff] [blame] | 20 | 2to3 |
| 21 | A tool that tries to convert Python 2.x code to Python 3.x code by |
| Georg Brandl | 0930228 | 2010-10-06 09:32:48 +0000 | [diff] [blame] | 22 | handling most of the incompatibilities which can be detected by parsing the |
| Georg Brandl | 5a42ca6 | 2008-05-20 07:20:12 +0000 | [diff] [blame] | 23 | source and traversing the parse tree. |
| 24 | |
| 25 | 2to3 is available in the standard library as :mod:`lib2to3`; a standalone |
| Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 26 | entry point is provided as :file:`Tools/scripts/2to3`. See |
| 27 | :ref:`2to3-reference`. |
| Georg Brandl | 5a42ca6 | 2008-05-20 07:20:12 +0000 | [diff] [blame] | 28 | |
| Benjamin Peterson | 9385b9d | 2008-07-03 12:57:35 +0000 | [diff] [blame] | 29 | abstract base class |
| Éric Araujo | 8fde950 | 2011-07-29 11:34:17 +0200 | [diff] [blame] | 30 | Abstract base classes complement :term:`duck-typing` by |
| Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 31 | providing a way to define interfaces when other techniques like |
| Éric Araujo | 8fde950 | 2011-07-29 11:34:17 +0200 | [diff] [blame] | 32 | :func:`hasattr` would be clumsy or subtly wrong (for example with |
| Éric Araujo | e0054c5 | 2011-08-19 09:15:47 +0200 | [diff] [blame] | 33 | :ref:`magic methods <new-style-special-lookup>`). ABCs introduce virtual |
| 34 | subclasses, which are classes that don't inherit from a class but are |
| 35 | still recognized by :func:`isinstance` and :func:`issubclass`; see the |
| 36 | :mod:`abc` module documentation. Python comes with many built-in ABCs for |
| Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 37 | data structures (in the :mod:`collections` module), numbers (in the |
| 38 | :mod:`numbers` module), and streams (in the :mod:`io` module). You can |
| Éric Araujo | 8fde950 | 2011-07-29 11:34:17 +0200 | [diff] [blame] | 39 | create your own ABCs with the :mod:`abc` module. |
| Benjamin Peterson | aac51b8 | 2008-07-01 23:33:06 +0000 | [diff] [blame] | 40 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 41 | argument |
| Chris Jerdonek | 8da8268 | 2012-11-29 19:03:37 -0800 | [diff] [blame] | 42 | A value passed to a :term:`function` (or :term:`method`) when calling the |
| 43 | function. There are two types of arguments: |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 44 | |
| Chris Jerdonek | 8da8268 | 2012-11-29 19:03:37 -0800 | [diff] [blame] | 45 | * :dfn:`keyword argument`: an argument preceded by an identifier (e.g. |
| 46 | ``name=``) in a function call or passed as a value in a dictionary |
| 47 | preceded by ``**``. For example, ``3`` and ``5`` are both keyword |
| 48 | arguments in the following calls to :func:`complex`:: |
| 49 | |
| 50 | complex(real=3, imag=5) |
| 51 | complex(**{'real': 3, 'imag': 5}) |
| 52 | |
| 53 | * :dfn:`positional argument`: an argument that is not a keyword argument. |
| 54 | Positional arguments can appear at the beginning of an argument list |
| 55 | and/or be passed as elements of an :term:`iterable` preceded by ``*``. |
| 56 | For example, ``3`` and ``5`` are both positional arguments in the |
| 57 | following calls:: |
| 58 | |
| 59 | complex(3, 5) |
| 60 | complex(*(3, 5)) |
| 61 | |
| 62 | Arguments are assigned to the named local variables in a function body. |
| 63 | See the :ref:`calls` section for the rules governing this assignment. |
| 64 | Syntactically, any expression can be used to represent an argument; the |
| 65 | evaluated value is assigned to the local variable. |
| 66 | |
| 67 | See also the :term:`parameter` glossary entry and the FAQ question on |
| 68 | :ref:`the difference between arguments and parameters |
| 69 | <faq-argument-vs-parameter>`. |
| Skip Montanaro | 9feab31 | 2008-09-15 02:19:53 +0000 | [diff] [blame] | 70 | |
| 71 | attribute |
| 72 | A value associated with an object which is referenced by name using |
| 73 | dotted expressions. For example, if an object *o* has an attribute |
| 74 | *a* it would be referenced as *o.a*. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 75 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 76 | BDFL |
| 77 | Benevolent Dictator For Life, a.k.a. `Guido van Rossum |
| Georg Brandl | 06f3b3b | 2014-10-29 08:36:35 +0100 | [diff] [blame] | 78 | <https://www.python.org/~guido/>`_, Python's creator. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 79 | |
| Ezio Melotti | 1c0d8a7 | 2013-04-30 23:33:31 +0300 | [diff] [blame] | 80 | bytes-like object |
| Ezio Melotti | 71c1cff | 2013-05-01 14:13:05 +0300 | [diff] [blame] | 81 | An object that supports the :ref:`buffer protocol <bufferobjects>`, |
| Antoine Pitrou | a09657e | 2013-05-04 20:18:34 +0200 | [diff] [blame] | 82 | like :class:`str`, :class:`bytearray` or :class:`memoryview`. |
| 83 | Bytes-like objects can be used for various operations that expect |
| 84 | binary data, such as compression, saving to a binary file or sending |
| 85 | over a socket. Some operations need the binary data to be mutable, |
| 86 | in which case not all bytes-like objects can apply. |
| Ezio Melotti | 1c0d8a7 | 2013-04-30 23:33:31 +0300 | [diff] [blame] | 87 | |
| Georg Brandl | 63fa168 | 2007-10-21 10:24:20 +0000 | [diff] [blame] | 88 | bytecode |
| 89 | Python source code is compiled into bytecode, the internal representation |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 90 | of a Python program in the CPython interpreter. The bytecode is also |
| 91 | cached in ``.pyc`` and ``.pyo`` files so that executing the same file is |
| 92 | faster the second time (recompilation from source to bytecode can be |
| 93 | avoided). This "intermediate language" is said to run on a |
| 94 | :term:`virtual machine` that executes the machine code corresponding to |
| 95 | each bytecode. Do note that bytecodes are not expected to work between |
| 96 | different Python virtual machines, nor to be stable between Python |
| 97 | releases. |
| Skip Montanaro | 9feab31 | 2008-09-15 02:19:53 +0000 | [diff] [blame] | 98 | |
| Georg Brandl | 2b4eda4 | 2010-07-03 10:25:54 +0000 | [diff] [blame] | 99 | A list of bytecode instructions can be found in the documentation for |
| 100 | :ref:`the dis module <bytecodes>`. |
| 101 | |
| Skip Montanaro | 9feab31 | 2008-09-15 02:19:53 +0000 | [diff] [blame] | 102 | class |
| 103 | A template for creating user-defined objects. Class definitions |
| 104 | normally contain method definitions which operate on instances of the |
| 105 | class. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 106 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 107 | classic class |
| 108 | Any class which does not inherit from :class:`object`. See |
| Ezio Melotti | 510ff54 | 2012-05-03 19:21:40 +0300 | [diff] [blame] | 109 | :term:`new-style class`. Classic classes have been removed in Python 3. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 110 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 111 | coercion |
| 112 | The implicit conversion of an instance of one type to another during an |
| 113 | operation which involves two arguments of the same type. For example, |
| 114 | ``int(3.15)`` converts the floating point number to the integer ``3``, but |
| 115 | in ``3+4.5``, each argument is of a different type (one int, one float), |
| 116 | and both must be converted to the same type before they can be added or it |
| 117 | will raise a ``TypeError``. Coercion between two operands can be |
| Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 118 | performed with the ``coerce`` built-in function; thus, ``3+4.5`` is |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 119 | equivalent to calling ``operator.add(*coerce(3, 4.5))`` and results in |
| 120 | ``operator.add(3.0, 4.5)``. Without coercion, all arguments of even |
| 121 | compatible types would have to be normalized to the same value by the |
| 122 | programmer, e.g., ``float(3)+4.5`` rather than just ``3+4.5``. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 123 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 124 | complex number |
| 125 | An extension of the familiar real number system in which all numbers are |
| 126 | expressed as a sum of a real part and an imaginary part. Imaginary |
| 127 | numbers are real multiples of the imaginary unit (the square root of |
| 128 | ``-1``), often written ``i`` in mathematics or ``j`` in |
| Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 129 | engineering. Python has built-in support for complex numbers, which are |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 130 | written with this latter notation; the imaginary part is written with a |
| 131 | ``j`` suffix, e.g., ``3+1j``. To get access to complex equivalents of the |
| 132 | :mod:`math` module, use :mod:`cmath`. Use of complex numbers is a fairly |
| 133 | advanced mathematical feature. If you're not aware of a need for them, |
| 134 | it's almost certain you can safely ignore them. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 135 | |
| Skip Montanaro | ffe455c | 2007-12-08 15:23:31 +0000 | [diff] [blame] | 136 | context manager |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 137 | An object which controls the environment seen in a :keyword:`with` |
| Skip Montanaro | ffe455c | 2007-12-08 15:23:31 +0000 | [diff] [blame] | 138 | statement by defining :meth:`__enter__` and :meth:`__exit__` methods. |
| 139 | See :pep:`343`. |
| 140 | |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 141 | CPython |
| Antoine Pitrou | 9f41bb3 | 2011-01-06 16:35:14 +0000 | [diff] [blame] | 142 | The canonical implementation of the Python programming language, as |
| Georg Brandl | 06f3b3b | 2014-10-29 08:36:35 +0100 | [diff] [blame] | 143 | distributed on `python.org <https://www.python.org>`_. The term "CPython" |
| Antoine Pitrou | 9f41bb3 | 2011-01-06 16:35:14 +0000 | [diff] [blame] | 144 | is used when necessary to distinguish this implementation from others |
| 145 | such as Jython or IronPython. |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 146 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 147 | decorator |
| 148 | A function returning another function, usually applied as a function |
| 149 | transformation using the ``@wrapper`` syntax. Common examples for |
| 150 | decorators are :func:`classmethod` and :func:`staticmethod`. |
| 151 | |
| 152 | The decorator syntax is merely syntactic sugar, the following two |
| 153 | function definitions are semantically equivalent:: |
| 154 | |
| 155 | def f(...): |
| 156 | ... |
| 157 | f = staticmethod(f) |
| 158 | |
| 159 | @staticmethod |
| 160 | def f(...): |
| 161 | ... |
| 162 | |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 163 | The same concept exists for classes, but is less commonly used there. See |
| 164 | the documentation for :ref:`function definitions <function>` and |
| 165 | :ref:`class definitions <class>` for more about decorators. |
| Georg Brandl | 5066c0c | 2008-12-05 18:00:06 +0000 | [diff] [blame] | 166 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 167 | descriptor |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 168 | Any *new-style* object which defines the methods :meth:`__get__`, |
| Georg Brandl | 5e52db0 | 2007-10-21 10:45:46 +0000 | [diff] [blame] | 169 | :meth:`__set__`, or :meth:`__delete__`. When a class attribute is a |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 170 | descriptor, its special binding behavior is triggered upon attribute |
| Georg Brandl | 5e52db0 | 2007-10-21 10:45:46 +0000 | [diff] [blame] | 171 | lookup. Normally, using *a.b* to get, set or delete an attribute looks up |
| 172 | the object named *b* in the class dictionary for *a*, but if *b* is a |
| 173 | descriptor, the respective descriptor method gets called. Understanding |
| 174 | descriptors is a key to a deep understanding of Python because they are |
| 175 | the basis for many features including functions, methods, properties, |
| 176 | class methods, static methods, and reference to super classes. |
| 177 | |
| 178 | For more information about descriptors' methods, see :ref:`descriptors`. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 179 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 180 | dictionary |
| Senthil Kumaran | c768d4c | 2012-03-12 10:05:04 -0700 | [diff] [blame] | 181 | An associative array, where arbitrary keys are mapped to values. The |
| 182 | keys can be any object with :meth:`__hash__` and :meth:`__eq__` methods. |
| 183 | Called a hash in Perl. |
| Georg Brandl | e64f738 | 2008-07-20 11:50:29 +0000 | [diff] [blame] | 184 | |
| Martin Panter | 4a33724 | 2015-10-07 10:19:39 +0000 | [diff] [blame] | 185 | dictionary view |
| 186 | The objects returned from :meth:`dict.viewkeys`, :meth:`dict.viewvalues`, |
| 187 | and :meth:`dict.viewitems` are called dictionary views. They provide a dynamic |
| 188 | view on the dictionary’s entries, which means that when the dictionary |
| Martin Panter | 840c82e | 2015-10-07 10:39:13 +0000 | [diff] [blame] | 189 | changes, the view reflects these changes. To force the |
| Martin Panter | 4a33724 | 2015-10-07 10:19:39 +0000 | [diff] [blame] | 190 | dictionary view to become a full list use ``list(dictview)``. See |
| 191 | :ref:`dict-views`. |
| 192 | |
| Georg Brandl | e64f738 | 2008-07-20 11:50:29 +0000 | [diff] [blame] | 193 | docstring |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 194 | A string literal which appears as the first expression in a class, |
| 195 | function or module. While ignored when the suite is executed, it is |
| 196 | recognized by the compiler and put into the :attr:`__doc__` attribute |
| 197 | of the enclosing class, function or module. Since it is available via |
| 198 | introspection, it is the canonical place for documentation of the |
| Georg Brandl | e64f738 | 2008-07-20 11:50:29 +0000 | [diff] [blame] | 199 | object. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 200 | |
| 201 | duck-typing |
| Georg Brandl | e85e1ae | 2010-10-06 09:17:24 +0000 | [diff] [blame] | 202 | A programming style which does not look at an object's type to determine |
| 203 | if it has the right interface; instead, the method or attribute is simply |
| 204 | called or used ("If it looks like a duck and quacks like a duck, it |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 205 | must be a duck.") By emphasizing interfaces rather than specific types, |
| 206 | well-designed code improves its flexibility by allowing polymorphic |
| 207 | substitution. Duck-typing avoids tests using :func:`type` or |
| Georg Brandl | 04eba2c | 2010-07-11 08:56:18 +0000 | [diff] [blame] | 208 | :func:`isinstance`. (Note, however, that duck-typing can be complemented |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 209 | with :term:`abstract base classes <abstract base class>`.) Instead, it |
| 210 | typically employs :func:`hasattr` tests or :term:`EAFP` programming. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 211 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 212 | EAFP |
| 213 | Easier to ask for forgiveness than permission. This common Python coding |
| 214 | style assumes the existence of valid keys or attributes and catches |
| 215 | exceptions if the assumption proves false. This clean and fast style is |
| 216 | characterized by the presence of many :keyword:`try` and :keyword:`except` |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 217 | statements. The technique contrasts with the :term:`LBYL` style |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 218 | common to many other languages such as C. |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 219 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 220 | expression |
| 221 | A piece of syntax which can be evaluated to some value. In other words, |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 222 | an expression is an accumulation of expression elements like literals, |
| 223 | names, attribute access, operators or function calls which all return a |
| 224 | value. In contrast to many other languages, not all language constructs |
| 225 | are expressions. There are also :term:`statement`\s which cannot be used |
| 226 | as expressions, such as :keyword:`print` or :keyword:`if`. Assignments |
| 227 | are also statements, not expressions. |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 228 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 229 | extension module |
| Georg Brandl | 28dadd9 | 2011-02-25 10:50:32 +0000 | [diff] [blame] | 230 | A module written in C or C++, using Python's C API to interact with the |
| 231 | core and with user code. |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 232 | |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 233 | file object |
| 234 | An object exposing a file-oriented API (with methods such as |
| 235 | :meth:`read()` or :meth:`write()`) to an underlying resource. Depending |
| 236 | on the way it was created, a file object can mediate access to a real |
| Sandro Tosi | fd4c4b1 | 2012-06-02 23:40:59 +0200 | [diff] [blame] | 237 | on-disk file or to another type of storage or communication device |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 238 | (for example standard input/output, in-memory buffers, sockets, pipes, |
| 239 | etc.). File objects are also called :dfn:`file-like objects` or |
| 240 | :dfn:`streams`. |
| 241 | |
| 242 | There are actually three categories of file objects: raw binary files, |
| 243 | buffered binary files and text files. Their interfaces are defined in the |
| 244 | :mod:`io` module. The canonical way to create a file object is by using |
| 245 | the :func:`open` function. |
| 246 | |
| 247 | file-like object |
| 248 | A synonym for :term:`file object`. |
| 249 | |
| Georg Brandl | 624f337 | 2009-03-31 16:11:45 +0000 | [diff] [blame] | 250 | finder |
| 251 | An object that tries to find the :term:`loader` for a module. It must |
| 252 | implement a method named :meth:`find_module`. See :pep:`302` for |
| 253 | details. |
| 254 | |
| Raymond Hettinger | f1b678d | 2010-09-01 22:25:41 +0000 | [diff] [blame] | 255 | floor division |
| 256 | Mathematical division that rounds down to nearest integer. The floor |
| 257 | division operator is ``//``. For example, the expression ``11 // 4`` |
| 258 | evaluates to ``2`` in contrast to the ``2.75`` returned by float true |
| 259 | division. Note that ``(-11) // 4`` is ``-3`` because that is ``-2.75`` |
| 260 | rounded *downward*. See :pep:`238`. |
| 261 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 262 | function |
| 263 | A series of statements which returns some value to a caller. It can also |
| Chris Jerdonek | cf4710c | 2012-12-25 14:50:21 -0800 | [diff] [blame] | 264 | be passed zero or more :term:`arguments <argument>` which may be used in |
| 265 | the execution of the body. See also :term:`parameter`, :term:`method`, |
| 266 | and the :ref:`function` section. |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 267 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 268 | __future__ |
| Raymond Hettinger | f1b678d | 2010-09-01 22:25:41 +0000 | [diff] [blame] | 269 | A pseudo-module which programmers can use to enable new language features |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 270 | which are not compatible with the current interpreter. For example, the |
| 271 | expression ``11/4`` currently evaluates to ``2``. If the module in which |
| 272 | it is executed had enabled *true division* by executing:: |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 273 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 274 | from __future__ import division |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 275 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 276 | the expression ``11/4`` would evaluate to ``2.75``. By importing the |
| 277 | :mod:`__future__` module and evaluating its variables, you can see when a |
| 278 | new feature was first added to the language and when it will become the |
| 279 | default:: |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 280 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 281 | >>> import __future__ |
| 282 | >>> __future__.division |
| 283 | _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192) |
| 284 | |
| 285 | garbage collection |
| 286 | The process of freeing memory when it is not used anymore. Python |
| 287 | performs garbage collection via reference counting and a cyclic garbage |
| 288 | collector that is able to detect and break reference cycles. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 289 | |
| Georg Brandl | ea2d389 | 2010-04-02 09:11:49 +0000 | [diff] [blame] | 290 | .. index:: single: generator |
| 291 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 292 | generator |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 293 | A function which returns an iterator. It looks like a normal function |
| Raymond Hettinger | f1b678d | 2010-09-01 22:25:41 +0000 | [diff] [blame] | 294 | except that it contains :keyword:`yield` statements for producing a series |
| Georg Brandl | 7ff0360 | 2015-01-15 08:16:01 +0100 | [diff] [blame] | 295 | of values usable in a for-loop or that can be retrieved one at a time with |
| Raymond Hettinger | f1b678d | 2010-09-01 22:25:41 +0000 | [diff] [blame] | 296 | the :func:`next` function. Each :keyword:`yield` temporarily suspends |
| 297 | processing, remembering the location execution state (including local |
| 298 | variables and pending try-statements). When the generator resumes, it |
| 299 | picks-up where it left-off (in contrast to functions which start fresh on |
| 300 | every invocation). |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 301 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 302 | .. index:: single: generator expression |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 303 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 304 | generator expression |
| Georg Brandl | ea2d389 | 2010-04-02 09:11:49 +0000 | [diff] [blame] | 305 | An expression that returns an iterator. It looks like a normal expression |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 306 | followed by a :keyword:`for` expression defining a loop variable, range, |
| 307 | and an optional :keyword:`if` expression. The combined expression |
| 308 | generates values for an enclosing function:: |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 309 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 310 | >>> sum(i*i for i in range(10)) # sum of squares 0, 1, 4, ... 81 |
| 311 | 285 |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 312 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 313 | GIL |
| Georg Brandl | 6c82b6c | 2007-08-17 16:54:59 +0000 | [diff] [blame] | 314 | See :term:`global interpreter lock`. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 315 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 316 | global interpreter lock |
| Antoine Pitrou | 9f41bb3 | 2011-01-06 16:35:14 +0000 | [diff] [blame] | 317 | The mechanism used by the :term:`CPython` interpreter to assure that |
| 318 | only one thread executes Python :term:`bytecode` at a time. |
| 319 | This simplifies the CPython implementation by making the object model |
| 320 | (including critical built-in types such as :class:`dict`) implicitly |
| 321 | safe against concurrent access. Locking the entire interpreter |
| 322 | makes it easier for the interpreter to be multi-threaded, at the |
| 323 | expense of much of the parallelism afforded by multi-processor |
| 324 | machines. |
| 325 | |
| 326 | However, some extension modules, either standard or third-party, |
| 327 | are designed so as to release the GIL when doing computationally-intensive |
| 328 | tasks such as compression or hashing. Also, the GIL is always released |
| 329 | when doing I/O. |
| 330 | |
| 331 | Past efforts to create a "free-threaded" interpreter (one which locks |
| 332 | shared data at a much finer granularity) have not been successful |
| 333 | because performance suffered in the common single-processor case. It |
| 334 | is believed that overcoming this performance issue would make the |
| 335 | implementation much more complicated and therefore costlier to maintain. |
| Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 336 | |
| 337 | hashable |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 338 | An object is *hashable* if it has a hash value which never changes during |
| Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 339 | its lifetime (it needs a :meth:`__hash__` method), and can be compared to |
| 340 | other objects (it needs an :meth:`__eq__` or :meth:`__cmp__` method). |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 341 | Hashable objects which compare equal must have the same hash value. |
| Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 342 | |
| 343 | Hashability makes an object usable as a dictionary key and a set member, |
| 344 | because these data structures use the hash value internally. |
| 345 | |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 346 | All of Python's immutable built-in objects are hashable, while no mutable |
| 347 | containers (such as lists or dictionaries) are. Objects which are |
| Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 348 | instances of user-defined classes are hashable by default; they all |
| Ezio Melotti | 4cb7fca | 2016-01-12 11:36:25 +0200 | [diff] [blame^] | 349 | compare unequal (except with themselves), and their hash value is derived |
| 350 | from their :func:`id`. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 351 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 352 | IDLE |
| 353 | An Integrated Development Environment for Python. IDLE is a basic editor |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 354 | and interpreter environment which ships with the standard distribution of |
| Raymond Hettinger | f1b678d | 2010-09-01 22:25:41 +0000 | [diff] [blame] | 355 | Python. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 356 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 357 | immutable |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 358 | An object with a fixed value. Immutable objects include numbers, strings and |
| 359 | tuples. Such an object cannot be altered. A new object has to |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 360 | be created if a different value has to be stored. They play an important |
| 361 | role in places where a constant hash value is needed, for example as a key |
| 362 | in a dictionary. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 363 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 364 | integer division |
| 365 | Mathematical division discarding any remainder. For example, the |
| 366 | expression ``11/4`` currently evaluates to ``2`` in contrast to the |
| 367 | ``2.75`` returned by float division. Also called *floor division*. |
| 368 | When dividing two integers the outcome will always be another integer |
| 369 | (having the floor function applied to it). However, if one of the operands |
| 370 | is another numeric type (such as a :class:`float`), the result will be |
| Georg Brandl | 6c82b6c | 2007-08-17 16:54:59 +0000 | [diff] [blame] | 371 | coerced (see :term:`coercion`) to a common type. For example, an integer |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 372 | divided by a float will result in a float value, possibly with a decimal |
| 373 | fraction. Integer division can be forced by using the ``//`` operator |
| Georg Brandl | 6c82b6c | 2007-08-17 16:54:59 +0000 | [diff] [blame] | 374 | instead of the ``/`` operator. See also :term:`__future__`. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 375 | |
| Georg Brandl | 2808a12 | 2013-10-08 08:05:33 +0200 | [diff] [blame] | 376 | importing |
| 377 | The process by which Python code in one module is made available to |
| 378 | Python code in another module. |
| 379 | |
| Georg Brandl | 624f337 | 2009-03-31 16:11:45 +0000 | [diff] [blame] | 380 | importer |
| 381 | An object that both finds and loads a module; both a |
| 382 | :term:`finder` and :term:`loader` object. |
| 383 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 384 | interactive |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 385 | Python has an interactive interpreter which means you can enter |
| 386 | statements and expressions at the interpreter prompt, immediately |
| 387 | execute them and see their results. Just launch ``python`` with no |
| 388 | arguments (possibly by selecting it from your computer's main |
| 389 | menu). It is a very powerful way to test out new ideas or inspect |
| 390 | modules and packages (remember ``help(x)``). |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 391 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 392 | interpreted |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 393 | Python is an interpreted language, as opposed to a compiled one, |
| 394 | though the distinction can be blurry because of the presence of the |
| 395 | bytecode compiler. This means that source files can be run directly |
| 396 | without explicitly creating an executable which is then run. |
| 397 | Interpreted languages typically have a shorter development/debug cycle |
| 398 | than compiled ones, though their programs generally also run more |
| 399 | slowly. See also :term:`interactive`. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 400 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 401 | iterable |
| Chris Jerdonek | 717e50e | 2013-01-04 04:41:34 -0800 | [diff] [blame] | 402 | An object capable of returning its members one at a time. Examples of |
| 403 | iterables include all sequence types (such as :class:`list`, :class:`str`, |
| 404 | and :class:`tuple`) and some non-sequence types like :class:`dict` |
| 405 | and :class:`file` and objects of any classes you define |
| 406 | with an :meth:`__iter__` or :meth:`__getitem__` method. Iterables can be |
| 407 | used in a :keyword:`for` loop and in many other places where a sequence is |
| 408 | needed (:func:`zip`, :func:`map`, ...). When an iterable object is passed |
| 409 | as an argument to the built-in function :func:`iter`, it returns an |
| 410 | iterator for the object. This iterator is good for one pass over the set |
| 411 | of values. When using iterables, it is usually not necessary to call |
| 412 | :func:`iter` or deal with iterator objects yourself. The ``for`` |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 413 | statement does that automatically for you, creating a temporary unnamed |
| 414 | variable to hold the iterator for the duration of the loop. See also |
| Georg Brandl | 6c82b6c | 2007-08-17 16:54:59 +0000 | [diff] [blame] | 415 | :term:`iterator`, :term:`sequence`, and :term:`generator`. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 416 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 417 | iterator |
| 418 | An object representing a stream of data. Repeated calls to the iterator's |
| Serhiy Storchaka | ea21772 | 2014-09-05 23:34:12 +0300 | [diff] [blame] | 419 | :meth:`~generator.next` method return successive items in the stream. When no more |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 420 | data are available a :exc:`StopIteration` exception is raised instead. At |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 421 | this point, the iterator object is exhausted and any further calls to its |
| Serhiy Storchaka | ea21772 | 2014-09-05 23:34:12 +0300 | [diff] [blame] | 422 | :meth:`~generator.next` method just raise :exc:`StopIteration` again. Iterators are |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 423 | required to have an :meth:`__iter__` method that returns the iterator |
| 424 | object itself so every iterator is also iterable and may be used in most |
| 425 | places where other iterables are accepted. One notable exception is code |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 426 | which attempts multiple iteration passes. A container object (such as a |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 427 | :class:`list`) produces a fresh new iterator each time you pass it to the |
| 428 | :func:`iter` function or use it in a :keyword:`for` loop. Attempting this |
| 429 | with an iterator will just return the same exhausted iterator object used |
| 430 | in the previous iteration pass, making it appear like an empty container. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 431 | |
| Georg Brandl | e7a0990 | 2007-10-21 12:10:28 +0000 | [diff] [blame] | 432 | More information can be found in :ref:`typeiter`. |
| 433 | |
| Georg Brandl | 3b85b9b | 2010-11-26 08:20:18 +0000 | [diff] [blame] | 434 | key function |
| 435 | A key function or collation function is a callable that returns a value |
| 436 | used for sorting or ordering. For example, :func:`locale.strxfrm` is |
| 437 | used to produce a sort key that is aware of locale specific sort |
| 438 | conventions. |
| 439 | |
| 440 | A number of tools in Python accept key functions to control how elements |
| 441 | are ordered or grouped. They include :func:`min`, :func:`max`, |
| 442 | :func:`sorted`, :meth:`list.sort`, :func:`heapq.nsmallest`, |
| 443 | :func:`heapq.nlargest`, and :func:`itertools.groupby`. |
| 444 | |
| 445 | There are several ways to create a key function. For example. the |
| 446 | :meth:`str.lower` method can serve as a key function for case insensitive |
| 447 | sorts. Alternatively, an ad-hoc key function can be built from a |
| 448 | :keyword:`lambda` expression such as ``lambda r: (r[0], r[2])``. Also, |
| Sandro Tosi | d987c02 | 2012-04-01 01:49:46 +0200 | [diff] [blame] | 449 | the :mod:`operator` module provides three key function constructors: |
| Georg Brandl | 3b85b9b | 2010-11-26 08:20:18 +0000 | [diff] [blame] | 450 | :func:`~operator.attrgetter`, :func:`~operator.itemgetter`, and |
| 451 | :func:`~operator.methodcaller`. See the :ref:`Sorting HOW TO |
| 452 | <sortinghowto>` for examples of how to create and use key functions. |
| 453 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 454 | keyword argument |
| Chris Jerdonek | 8da8268 | 2012-11-29 19:03:37 -0800 | [diff] [blame] | 455 | See :term:`argument`. |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 456 | |
| 457 | lambda |
| 458 | An anonymous inline function consisting of a single :term:`expression` |
| 459 | which is evaluated when the function is called. The syntax to create |
| 460 | a lambda function is ``lambda [arguments]: expression`` |
| 461 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 462 | LBYL |
| 463 | Look before you leap. This coding style explicitly tests for |
| 464 | pre-conditions before making calls or lookups. This style contrasts with |
| Georg Brandl | 6c82b6c | 2007-08-17 16:54:59 +0000 | [diff] [blame] | 465 | the :term:`EAFP` approach and is characterized by the presence of many |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 466 | :keyword:`if` statements. |
| Skip Montanaro | 9feab31 | 2008-09-15 02:19:53 +0000 | [diff] [blame] | 467 | |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 468 | In a multi-threaded environment, the LBYL approach can risk introducing a |
| 469 | race condition between "the looking" and "the leaping". For example, the |
| 470 | code, ``if key in mapping: return mapping[key]`` can fail if another |
| 471 | thread removes *key* from *mapping* after the test, but before the lookup. |
| 472 | This issue can be solved with locks or by using the EAFP approach. |
| 473 | |
| Skip Montanaro | 9feab31 | 2008-09-15 02:19:53 +0000 | [diff] [blame] | 474 | list |
| 475 | A built-in Python :term:`sequence`. Despite its name it is more akin |
| 476 | to an array in other languages than to a linked list since access to |
| 477 | elements are O(1). |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 478 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 479 | list comprehension |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 480 | A compact way to process all or part of the elements in a sequence and |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 481 | return a list with the results. ``result = ["0x%02x" % x for x in |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 482 | range(256) if x % 2 == 0]`` generates a list of strings containing |
| 483 | even hex numbers (0x..) in the range from 0 to 255. The :keyword:`if` |
| 484 | clause is optional. If omitted, all elements in ``range(256)`` are |
| 485 | processed. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 486 | |
| Georg Brandl | 624f337 | 2009-03-31 16:11:45 +0000 | [diff] [blame] | 487 | loader |
| 488 | An object that loads a module. It must define a method named |
| 489 | :meth:`load_module`. A loader is typically returned by a |
| 490 | :term:`finder`. See :pep:`302` for details. |
| 491 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 492 | mapping |
| Raymond Hettinger | c4c52dd | 2011-01-08 23:50:39 +0000 | [diff] [blame] | 493 | A container object that supports arbitrary key lookups and implements the |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 494 | methods specified in the :class:`~collections.Mapping` or |
| 495 | :class:`~collections.MutableMapping` |
| Éric Araujo | 8fde950 | 2011-07-29 11:34:17 +0200 | [diff] [blame] | 496 | :ref:`abstract base classes <collections-abstract-base-classes>`. Examples |
| 497 | include :class:`dict`, :class:`collections.defaultdict`, |
| Raymond Hettinger | c4c52dd | 2011-01-08 23:50:39 +0000 | [diff] [blame] | 498 | :class:`collections.OrderedDict` and :class:`collections.Counter`. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 499 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 500 | metaclass |
| 501 | The class of a class. Class definitions create a class name, a class |
| 502 | dictionary, and a list of base classes. The metaclass is responsible for |
| 503 | taking those three arguments and creating the class. Most object oriented |
| 504 | programming languages provide a default implementation. What makes Python |
| 505 | special is that it is possible to create custom metaclasses. Most users |
| 506 | never need this tool, but when the need arises, metaclasses can provide |
| 507 | powerful, elegant solutions. They have been used for logging attribute |
| 508 | access, adding thread-safety, tracking object creation, implementing |
| 509 | singletons, and many other tasks. |
| Georg Brandl | a739503 | 2007-10-21 12:15:05 +0000 | [diff] [blame] | 510 | |
| 511 | More information can be found in :ref:`metaclasses`. |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 512 | |
| 513 | method |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 514 | A function which is defined inside a class body. If called as an attribute |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 515 | of an instance of that class, the method will get the instance object as |
| 516 | its first :term:`argument` (which is usually called ``self``). |
| 517 | See :term:`function` and :term:`nested scope`. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 518 | |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 519 | method resolution order |
| 520 | Method Resolution Order is the order in which base classes are searched |
| 521 | for a member during lookup. See `The Python 2.3 Method Resolution Order |
| Senthil Kumaran | 145b1c6 | 2016-01-09 22:36:13 -0800 | [diff] [blame] | 522 | <https://www.python.org/download/releases/2.3/mro/>`_ for details of the |
| 523 | algorithm used by the Python interpreter since the 2.3 release. |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 524 | |
| Georg Brandl | 2808a12 | 2013-10-08 08:05:33 +0200 | [diff] [blame] | 525 | module |
| 526 | An object that serves as an organizational unit of Python code. Modules |
| 527 | have a namespace containing arbitrary Python objects. Modules are loaded |
| 528 | into Python by the process of :term:`importing`. |
| 529 | |
| 530 | See also :term:`package`. |
| 531 | |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 532 | MRO |
| 533 | See :term:`method resolution order`. |
| 534 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 535 | mutable |
| 536 | Mutable objects can change their value but keep their :func:`id`. See |
| Georg Brandl | 6c82b6c | 2007-08-17 16:54:59 +0000 | [diff] [blame] | 537 | also :term:`immutable`. |
| Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 538 | |
| 539 | named tuple |
| Raymond Hettinger | aff711d | 2009-02-04 19:25:17 +0000 | [diff] [blame] | 540 | Any tuple-like class whose indexable elements are also accessible using |
| Raymond Hettinger | c20ed51 | 2008-01-13 06:15:15 +0000 | [diff] [blame] | 541 | named attributes (for example, :func:`time.localtime` returns a |
| Raymond Hettinger | 8bdd044 | 2008-01-13 06:18:07 +0000 | [diff] [blame] | 542 | tuple-like object where the *year* is accessible either with an |
| Raymond Hettinger | c20ed51 | 2008-01-13 06:15:15 +0000 | [diff] [blame] | 543 | index such as ``t[0]`` or with a named attribute like ``t.tm_year``). |
| 544 | |
| 545 | A named tuple can be a built-in type such as :class:`time.struct_time`, |
| 546 | or it can be created with a regular class definition. A full featured |
| 547 | named tuple can also be created with the factory function |
| 548 | :func:`collections.namedtuple`. The latter approach automatically |
| 549 | provides extra features such as a self-documenting representation like |
| 550 | ``Employee(name='jones', title='programmer')``. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 551 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 552 | namespace |
| 553 | The place where a variable is stored. Namespaces are implemented as |
| Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 554 | dictionaries. There are the local, global and built-in namespaces as well |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 555 | as nested namespaces in objects (in methods). Namespaces support |
| 556 | modularity by preventing naming conflicts. For instance, the functions |
| 557 | :func:`__builtin__.open` and :func:`os.open` are distinguished by their |
| 558 | namespaces. Namespaces also aid readability and maintainability by making |
| 559 | it clear which module implements a function. For instance, writing |
| 560 | :func:`random.seed` or :func:`itertools.izip` makes it clear that those |
| 561 | functions are implemented by the :mod:`random` and :mod:`itertools` |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 562 | modules, respectively. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 563 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 564 | nested scope |
| 565 | The ability to refer to a variable in an enclosing definition. For |
| 566 | instance, a function defined inside another function can refer to |
| Sandro Tosi | 03c6110 | 2012-01-17 18:32:02 +0100 | [diff] [blame] | 567 | variables in the outer function. Note that nested scopes work only for |
| 568 | reference and not for assignment which will always write to the innermost |
| 569 | scope. In contrast, local variables both read and write in the innermost |
| 570 | scope. Likewise, global variables read and write to the global namespace. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 571 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 572 | new-style class |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 573 | Any class which inherits from :class:`object`. This includes all built-in |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 574 | types like :class:`list` and :class:`dict`. Only new-style classes can |
| Serhiy Storchaka | ad16b72 | 2013-10-09 14:02:14 +0300 | [diff] [blame] | 575 | use Python's newer, versatile features like :attr:`~object.__slots__`, |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 576 | descriptors, properties, and :meth:`__getattribute__`. |
| Georg Brandl | a739503 | 2007-10-21 12:15:05 +0000 | [diff] [blame] | 577 | |
| 578 | More information can be found in :ref:`newstyle`. |
| Skip Montanaro | 9feab31 | 2008-09-15 02:19:53 +0000 | [diff] [blame] | 579 | |
| 580 | object |
| 581 | Any data with state (attributes or value) and defined behavior |
| 582 | (methods). Also the ultimate base class of any :term:`new-style |
| 583 | class`. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 584 | |
| Georg Brandl | 2808a12 | 2013-10-08 08:05:33 +0200 | [diff] [blame] | 585 | package |
| 586 | A Python :term:`module` which can contain submodules or recursively, |
| 587 | subpackages. Technically, a package is a Python module with an |
| 588 | ``__path__`` attribute. |
| 589 | |
| Chris Jerdonek | 8da8268 | 2012-11-29 19:03:37 -0800 | [diff] [blame] | 590 | parameter |
| 591 | A named entity in a :term:`function` (or method) definition that |
| 592 | specifies an :term:`argument` (or in some cases, arguments) that the |
| 593 | function can accept. There are four types of parameters: |
| 594 | |
| 595 | * :dfn:`positional-or-keyword`: specifies an argument that can be passed |
| 596 | either :term:`positionally <argument>` or as a :term:`keyword argument |
| 597 | <argument>`. This is the default kind of parameter, for example *foo* |
| 598 | and *bar* in the following:: |
| 599 | |
| 600 | def func(foo, bar=None): ... |
| 601 | |
| 602 | * :dfn:`positional-only`: specifies an argument that can be supplied only |
| 603 | by position. Python has no syntax for defining positional-only |
| 604 | parameters. However, some built-in functions have positional-only |
| 605 | parameters (e.g. :func:`abs`). |
| 606 | |
| 607 | * :dfn:`var-positional`: specifies that an arbitrary sequence of |
| 608 | positional arguments can be provided (in addition to any positional |
| 609 | arguments already accepted by other parameters). Such a parameter can |
| 610 | be defined by prepending the parameter name with ``*``, for example |
| 611 | *args* in the following:: |
| 612 | |
| 613 | def func(*args, **kwargs): ... |
| 614 | |
| 615 | * :dfn:`var-keyword`: specifies that arbitrarily many keyword arguments |
| 616 | can be provided (in addition to any keyword arguments already accepted |
| 617 | by other parameters). Such a parameter can be defined by prepending |
| 618 | the parameter name with ``**``, for example *kwargs* in the example |
| 619 | above. |
| 620 | |
| 621 | Parameters can specify both optional and required arguments, as well as |
| 622 | default values for some optional arguments. |
| 623 | |
| 624 | See also the :term:`argument` glossary entry, the FAQ question on |
| 625 | :ref:`the difference between arguments and parameters |
| 626 | <faq-argument-vs-parameter>`, and the :ref:`function` section. |
| 627 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 628 | positional argument |
| Chris Jerdonek | 8da8268 | 2012-11-29 19:03:37 -0800 | [diff] [blame] | 629 | See :term:`argument`. |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 630 | |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 631 | Python 3000 |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 632 | Nickname for the Python 3.x release line (coined long ago when the release |
| 633 | of version 3 was something in the distant future.) This is also |
| 634 | abbreviated "Py3k". |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 635 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 636 | Pythonic |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 637 | An idea or piece of code which closely follows the most common idioms |
| 638 | of the Python language, rather than implementing code using concepts |
| 639 | common to other languages. For example, a common idiom in Python is |
| 640 | to loop over all elements of an iterable using a :keyword:`for` |
| 641 | statement. Many other languages don't have this type of construct, so |
| 642 | people unfamiliar with Python sometimes use a numerical counter instead:: |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 643 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 644 | for i in range(len(food)): |
| 645 | print food[i] |
| 646 | |
| 647 | As opposed to the cleaner, Pythonic method:: |
| 648 | |
| 649 | for piece in food: |
| 650 | print piece |
| 651 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 652 | reference count |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 653 | The number of references to an object. When the reference count of an |
| 654 | object drops to zero, it is deallocated. Reference counting is |
| 655 | generally not visible to Python code, but it is a key element of the |
| 656 | :term:`CPython` implementation. The :mod:`sys` module defines a |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 657 | :func:`~sys.getrefcount` function that programmers can call to return the |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 658 | reference count for a particular object. |
| 659 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 660 | __slots__ |
| Georg Brandl | 6c82b6c | 2007-08-17 16:54:59 +0000 | [diff] [blame] | 661 | A declaration inside a :term:`new-style class` that saves memory by |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 662 | pre-declaring space for instance attributes and eliminating instance |
| 663 | dictionaries. Though popular, the technique is somewhat tricky to get |
| 664 | right and is best reserved for rare cases where there are large numbers of |
| 665 | instances in a memory-critical application. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 666 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 667 | sequence |
| Georg Brandl | 6c82b6c | 2007-08-17 16:54:59 +0000 | [diff] [blame] | 668 | An :term:`iterable` which supports efficient element access using integer |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 669 | indices via the :meth:`__getitem__` special method and defines a |
| 670 | :meth:`len` method that returns the length of the sequence. |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 671 | Some built-in sequence types are :class:`list`, :class:`str`, |
| 672 | :class:`tuple`, and :class:`unicode`. Note that :class:`dict` also |
| 673 | supports :meth:`__getitem__` and :meth:`__len__`, but is considered a |
| 674 | mapping rather than a sequence because the lookups use arbitrary |
| Georg Brandl | 6c82b6c | 2007-08-17 16:54:59 +0000 | [diff] [blame] | 675 | :term:`immutable` keys rather than integers. |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 676 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 677 | slice |
| Georg Brandl | 968a3e5 | 2007-12-02 18:17:50 +0000 | [diff] [blame] | 678 | An object usually containing a portion of a :term:`sequence`. A slice is |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 679 | created using the subscript notation, ``[]`` with colons between numbers |
| 680 | when several are given, such as in ``variable_name[1:3:5]``. The bracket |
| 681 | (subscript) notation uses :class:`slice` objects internally (or in older |
| 682 | versions, :meth:`__getslice__` and :meth:`__setslice__`). |
| 683 | |
| Georg Brandl | 9a05373 | 2008-12-05 15:29:39 +0000 | [diff] [blame] | 684 | special method |
| 685 | A method that is called implicitly by Python to execute a certain |
| 686 | operation on a type, such as addition. Such methods have names starting |
| 687 | and ending with double underscores. Special methods are documented in |
| 688 | :ref:`specialnames`. |
| 689 | |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 690 | statement |
| 691 | A statement is part of a suite (a "block" of code). A statement is either |
| Georg Brandl | bcc55d6 | 2013-10-06 11:57:13 +0200 | [diff] [blame] | 692 | an :term:`expression` or one of several constructs with a keyword, such |
| Éric Araujo | a8f66dd | 2011-08-19 01:27:00 +0200 | [diff] [blame] | 693 | as :keyword:`if`, :keyword:`while` or :keyword:`for`. |
| 694 | |
| 695 | struct sequence |
| 696 | A tuple with named elements. Struct sequences expose an interface similiar |
| 697 | to :term:`named tuple` in that elements can either be accessed either by |
| 698 | index or as an attribute. However, they do not have any of the named tuple |
| 699 | methods like :meth:`~collections.somenamedtuple._make` or |
| 700 | :meth:`~collections.somenamedtuple._asdict`. Examples of struct sequences |
| 701 | include :data:`sys.float_info` and the return value of :func:`os.stat`. |
| Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 702 | |
| Skip Montanaro | 9feab31 | 2008-09-15 02:19:53 +0000 | [diff] [blame] | 703 | triple-quoted string |
| 704 | A string which is bound by three instances of either a quotation mark |
| 705 | (") or an apostrophe ('). While they don't provide any functionality |
| 706 | not available with single-quoted strings, they are useful for a number |
| 707 | of reasons. They allow you to include unescaped single and double |
| 708 | quotes within a string and they can span multiple lines without the |
| 709 | use of the continuation character, making them especially useful when |
| 710 | writing docstrings. |
| 711 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 712 | type |
| 713 | The type of a Python object determines what kind of object it is; every |
| 714 | object has a type. An object's type is accessible as its |
| Serhiy Storchaka | ad16b72 | 2013-10-09 14:02:14 +0300 | [diff] [blame] | 715 | :attr:`~instance.__class__` attribute or can be retrieved with |
| 716 | ``type(obj)``. |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 717 | |
| R David Murray | 5618aaa | 2012-08-15 11:15:39 -0400 | [diff] [blame] | 718 | universal newlines |
| 719 | A manner of interpreting text streams in which all of the following are |
| 720 | recognized as ending a line: the Unix end-of-line convention ``'\n'``, |
| 721 | the Windows convention ``'\r\n'``, and the old Macintosh convention |
| 722 | ``'\r'``. See :pep:`278` and :pep:`3116`, as well as |
| 723 | :func:`str.splitlines` for an additional use. |
| 724 | |
| Donald Stufft | 527d4ac | 2014-11-20 09:38:31 -0500 | [diff] [blame] | 725 | virtual environment |
| 726 | A cooperatively isolated runtime environment that allows Python users |
| 727 | and applications to install and upgrade Python distribution packages |
| 728 | without interfering with the behaviour of other Python applications |
| 729 | running on the same system. |
| 730 | |
| Skip Montanaro | f02c5f3 | 2008-09-15 02:03:05 +0000 | [diff] [blame] | 731 | virtual machine |
| 732 | A computer defined entirely in software. Python's virtual machine |
| 733 | executes the :term:`bytecode` emitted by the bytecode compiler. |
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 734 | |
| Georg Brandl | 437e6a3 | 2007-08-17 06:27:11 +0000 | [diff] [blame] | 735 | Zen of Python |
| 736 | Listing of Python design principles and philosophies that are helpful in |
| 737 | understanding and using the language. The listing can be found by typing |
| 738 | "``import this``" at the interactive prompt. |