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