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