27
27
:ref:`2to3-reference`.
29
29
abstract base class
30
:ref:`abstract-base-classes` complement :term:`duck-typing` by
30
Abstract base classes complement :term:`duck-typing` by
31
31
providing a way to define interfaces when other techniques like
32
:func:`hasattr` would be clumsy. Python comes with many built-in ABCs for
32
:func:`hasattr` would be clumsy or subtly wrong (for example with
33
:ref:`magic methods <new-style-special-lookup>`). ABCs introduce virtual
34
subclasses, which are classes that don't inherit from a class but are
35
still recognized by :func:`isinstance` and :func:`issubclass`; see the
36
:mod:`abc` module documentation. Python comes with many built-in ABCs for
33
37
data structures (in the :mod:`collections` module), numbers (in the
34
38
:mod:`numbers` module), and streams (in the :mod:`io` module). You can
35
create your own ABC with the :mod:`abc` module.
39
create your own ABCs with the :mod:`abc` module.
38
42
A value passed to a function or method, assigned to a named local
59
63
Python source code is compiled into bytecode, the internal representation
60
of a Python program in the interpreter. The bytecode is also cached in
61
``.pyc`` and ``.pyo`` files so that executing the same file is faster the
62
second time (recompilation from source to bytecode can be avoided). This
63
"intermediate language" is said to run on a :term:`virtual machine`
64
that executes the machine code corresponding to each bytecode.
64
of a Python program in the CPython interpreter. The bytecode is also
65
cached in ``.pyc`` and ``.pyo`` files so that executing the same file is
66
faster the second time (recompilation from source to bytecode can be
67
avoided). This "intermediate language" is said to run on a
68
:term:`virtual machine` that executes the machine code corresponding to
69
each bytecode. Do note that bytecodes are not expected to work between
70
different Python virtual machines, nor to be stable between Python
66
73
A list of bytecode instructions can be found in the documentation for
67
74
:ref:`the dis module <bytecodes>`.
130
See :ref:`the documentation for function definition <function>` for more
137
The same concept exists for classes, but is less commonly used there. See
138
the documentation for :ref:`function definitions <function>` and
139
:ref:`class definitions <class>` for more about decorators.
134
142
Any *new-style* object which defines the methods :meth:`__get__`,
164
172
well-designed code improves its flexibility by allowing polymorphic
165
173
substitution. Duck-typing avoids tests using :func:`type` or
166
174
:func:`isinstance`. (Note, however, that duck-typing can be complemented
167
with :term:`abstract base class`\ es.) Instead, it typically employs
168
:func:`hasattr` tests or :term:`EAFP` programming.
175
with :term:`abstract base classes <abstract base class>`.) Instead, it
176
typically employs :func:`hasattr` tests or :term:`EAFP` programming.
171
179
Easier to ask for forgiveness than permission. This common Python coding
179
187
A piece of syntax which can be evaluated to some value. In other words,
180
an expression is an accumulation of expression elements like literals, names,
181
attribute access, operators or function calls which all return a value.
182
In contrast to many other languages, not all language constructs are expressions.
183
There are also :term:`statement`\s which cannot be used as expressions,
184
such as :keyword:`print` or :keyword:`if`. Assignments are also statements,
188
an expression is an accumulation of expression elements like literals,
189
names, attribute access, operators or function calls which all return a
190
value. In contrast to many other languages, not all language constructs
191
are expressions. There are also :term:`statement`\s which cannot be used
192
as expressions, such as :keyword:`print` or :keyword:`if`. Assignments
193
are also statements, not expressions.
188
196
A module written in C or C++, using Python's C API to interact with the
189
197
core and with user code.
200
An object exposing a file-oriented API (with methods such as
201
:meth:`read()` or :meth:`write()`) to an underlying resource. Depending
202
on the way it was created, a file object can mediate access to a real
203
on-disk file or to another other type of storage or communication device
204
(for example standard input/output, in-memory buffers, sockets, pipes,
205
etc.). File objects are also called :dfn:`file-like objects` or
208
There are actually three categories of file objects: raw binary files,
209
buffered binary files and text files. Their interfaces are defined in the
210
:mod:`io` module. The canonical way to create a file object is by using
211
the :func:`open` function.
214
A synonym for :term:`file object`.
192
217
An object that tries to find the :term:`loader` for a module. It must
193
218
implement a method named :meth:`find_module`. See :pep:`302` for
334
359
slowly. See also :term:`interactive`.
337
A container object capable of returning its members one at a
362
An object capable of returning its members one at a
338
363
time. Examples of iterables include all sequence types (such as
339
364
:class:`list`, :class:`str`, and :class:`tuple`) and some non-sequence
340
365
types like :class:`dict` and :class:`file` and objects of any classes you
403
428
the :term:`EAFP` approach and is characterized by the presence of many
404
429
:keyword:`if` statements.
431
In a multi-threaded environment, the LBYL approach can risk introducing a
432
race condition between "the looking" and "the leaping". For example, the
433
code, ``if key in mapping: return mapping[key]`` can fail if another
434
thread removes *key* from *mapping* after the test, but before the lookup.
435
This issue can be solved with locks or by using the EAFP approach.
407
438
A built-in Python :term:`sequence`. Despite its name it is more akin
408
439
to an array in other languages than to a linked list since access to
425
456
A container object that supports arbitrary key lookups and implements the
426
methods specified in the :class:`Mapping` or :class:`MutableMapping`
427
:ref:`abstract base classes <abstract-base-classes>`. Examples include
428
:class:`dict`, :class:`collections.defaultdict`,
457
methods specified in the :class:`~collections.Mapping` or
458
:class:`~collections.MutableMapping`
459
:ref:`abstract base classes <collections-abstract-base-classes>`. Examples
460
include :class:`dict`, :class:`collections.defaultdict`,
429
461
:class:`collections.OrderedDict` and :class:`collections.Counter`.
447
479
its first :term:`argument` (which is usually called ``self``).
448
480
See :term:`function` and :term:`nested scope`.
482
method resolution order
483
Method Resolution Order is the order in which base classes are searched
484
for a member during lookup. See `The Python 2.3 Method Resolution Order
485
<http://www.python.org/download/releases/2.3/mro/>`_.
488
See :term:`method resolution order`.
451
491
Mutable objects can change their value but keep their :func:`id`. See
452
492
also :term:`immutable`.
505
545
:term:`argument`.
508
Nickname for the next major Python version, 3.0 (coined long ago
509
when the release of version 3 was something in the distant future.) This
510
is also abbreviated "Py3k".
548
Nickname for the Python 3.x release line (coined long ago when the release
549
of version 3 was something in the distant future.) This is also
513
553
An idea or piece of code which closely follows the most common idioms
530
570
object drops to zero, it is deallocated. Reference counting is
531
571
generally not visible to Python code, but it is a key element of the
532
572
:term:`CPython` implementation. The :mod:`sys` module defines a
533
:func:`getrefcount` function that programmers can call to return the
573
:func:`~sys.getrefcount` function that programmers can call to return the
534
574
reference count for a particular object.
567
607
A statement is part of a suite (a "block" of code). A statement is either
568
608
an :term:`expression` or a one of several constructs with a keyword, such
569
as :keyword:`if`, :keyword:`while` or :keyword:`print`.
609
as :keyword:`if`, :keyword:`while` or :keyword:`for`.
612
A tuple with named elements. Struct sequences expose an interface similiar
613
to :term:`named tuple` in that elements can either be accessed either by
614
index or as an attribute. However, they do not have any of the named tuple
615
methods like :meth:`~collections.somenamedtuple._make` or
616
:meth:`~collections.somenamedtuple._asdict`. Examples of struct sequences
617
include :data:`sys.float_info` and the return value of :func:`os.stat`.
571
619
triple-quoted string
572
620
A string which is bound by three instances of either a quotation mark