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