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