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