summaryrefslogtreecommitdiff
path: root/Doc/reference/datamodel.rst
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2016-06-11 13:18:56 -0700
committerBenjamin Peterson <benjamin@python.org>2016-06-11 13:18:56 -0700
commit5a991226e84e935f2a50deec664eaf3860893354 (patch)
treee674175fd5bc642be83ceb674d57e303b26f4326 /Doc/reference/datamodel.rst
parent9997d0207649b71b5862d3c036c1f7dcacf4f7b1 (diff)
parent1ccde8e1ad13ea6943f6ef5ea4329c9827ba493e (diff)
downloadcpython-5a991226e84e935f2a50deec664eaf3860893354.tar.gz
merge 3.4
Diffstat (limited to 'Doc/reference/datamodel.rst')
-rw-r--r--Doc/reference/datamodel.rst286
1 files changed, 250 insertions, 36 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 1560ad0d32..71d695f96f 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -616,6 +616,16 @@ Callable types
exception is raised and the iterator will have reached the end of the set of
values to be returned.
+ Coroutine functions
+ .. index::
+ single: coroutine; function
+
+ A function or method which is defined using :keyword:`async def` is called
+ a :dfn:`coroutine function`. Such a function, when called, returns a
+ :term:`coroutine` object. It may contain :keyword:`await` expressions,
+ as well as :keyword:`async with` and :keyword:`async for` statements. See
+ also the :ref:`coroutine-objects` section.
+
Built-in functions
.. index::
object: built-in function
@@ -836,11 +846,9 @@ Internal types
definitions may change with future versions of the interpreter, but they are
mentioned here for completeness.
- Code objects
- .. index::
- single: bytecode
- object: code
+ .. index:: bytecode, object; code, code object
+ Code objects
Code objects represent *byte-compiled* executable Python code, or :term:`bytecode`.
The difference between a code object and a function object is that the function
object contains an explicit reference to the function's globals (the module in
@@ -1724,6 +1732,11 @@ After the class object is created, it is passed to the class decorators
included in the class definition (if any) and the resulting object is bound
in the local namespace as the defined class.
+When a new class is created by ``type.__new__``, the object provided as the
+namespace parameter is copied to a standard Python dictionary and the original
+object is discarded. The new copy becomes the :attr:`~object.__dict__` attribute
+of the class object.
+
.. seealso::
:pep:`3135` - New super
@@ -1743,11 +1756,11 @@ to remember the order that class variables are defined::
class OrderedClass(type):
- @classmethod
- def __prepare__(metacls, name, bases, **kwds):
+ @classmethod
+ def __prepare__(metacls, name, bases, **kwds):
return collections.OrderedDict()
- def __new__(cls, name, bases, namespace, **kwds):
+ def __new__(cls, name, bases, namespace, **kwds):
result = type.__new__(cls, name, bases, dict(namespace))
result.members = tuple(namespace)
return result
@@ -1990,6 +2003,7 @@ left undefined.
.. method:: object.__add__(self, other)
object.__sub__(self, other)
object.__mul__(self, other)
+ object.__matmul__(self, other)
object.__truediv__(self, other)
object.__floordiv__(self, other)
object.__mod__(self, other)
@@ -2006,15 +2020,16 @@ left undefined.
builtin: pow
builtin: pow
- These methods are called to implement the binary arithmetic operations (``+``,
- ``-``, ``*``, ``/``, ``//``, ``%``, :func:`divmod`, :func:`pow`, ``**``, ``<<``,
- ``>>``, ``&``, ``^``, ``|``). For instance, to evaluate the expression
- ``x + y``, where *x* is an instance of a class that has an :meth:`__add__`
- method, ``x.__add__(y)`` is called. The :meth:`__divmod__` method should be the
- equivalent to using :meth:`__floordiv__` and :meth:`__mod__`; it should not be
- related to :meth:`__truediv__`. Note that :meth:`__pow__` should be defined
- to accept an optional third argument if the ternary version of the built-in
- :func:`pow` function is to be supported.
+ These methods are called to implement the binary arithmetic operations
+ (``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :func:`divmod`,
+ :func:`pow`, ``**``, ``<<``, ``>>``, ``&``, ``^``, ``|``). For instance, to
+ evaluate the expression ``x + y``, where *x* is an instance of a class that
+ has an :meth:`__add__` method, ``x.__add__(y)`` is called. The
+ :meth:`__divmod__` method should be the equivalent to using
+ :meth:`__floordiv__` and :meth:`__mod__`; it should not be related to
+ :meth:`__truediv__`. Note that :meth:`__pow__` should be defined to accept
+ an optional third argument if the ternary version of the built-in :func:`pow`
+ function is to be supported.
If one of those methods does not support the operation with the supplied
arguments, it should return ``NotImplemented``.
@@ -2023,6 +2038,7 @@ left undefined.
.. method:: object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
+ object.__rmatmul__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
@@ -2038,14 +2054,14 @@ left undefined.
builtin: divmod
builtin: pow
- These methods are called to implement the binary arithmetic operations (``+``,
- ``-``, ``*``, ``/``, ``//``, ``%``, :func:`divmod`, :func:`pow`, ``**``,
- ``<<``, ``>>``, ``&``, ``^``, ``|``) with reflected (swapped) operands.
- These functions are only called if the left operand does not support the
- corresponding operation and the operands are of different types. [#]_ For
- instance, to evaluate the expression ``x - y``, where *y* is an instance of
- a class that has an :meth:`__rsub__` method, ``y.__rsub__(x)`` is called if
- ``x.__sub__(y)`` returns *NotImplemented*.
+ These methods are called to implement the binary arithmetic operations
+ (``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :func:`divmod`,
+ :func:`pow`, ``**``, ``<<``, ``>>``, ``&``, ``^``, ``|``) with reflected
+ (swapped) operands. These functions are only called if the left operand does
+ not support the corresponding operation and the operands are of different
+ types. [#]_ For instance, to evaluate the expression ``x - y``, where *y* is
+ an instance of a class that has an :meth:`__rsub__` method, ``y.__rsub__(x)``
+ is called if ``x.__sub__(y)`` returns *NotImplemented*.
.. index:: builtin: pow
@@ -2063,6 +2079,7 @@ left undefined.
.. method:: object.__iadd__(self, other)
object.__isub__(self, other)
object.__imul__(self, other)
+ object.__imatmul__(self, other)
object.__itruediv__(self, other)
object.__ifloordiv__(self, other)
object.__imod__(self, other)
@@ -2074,17 +2091,17 @@ left undefined.
object.__ior__(self, other)
These methods are called to implement the augmented arithmetic assignments
- (``+=``, ``-=``, ``*=``, ``/=``, ``//=``, ``%=``, ``**=``, ``<<=``, ``>>=``,
- ``&=``, ``^=``, ``|=``). These methods should attempt to do the operation
- in-place (modifying *self*) and return the result (which could be, but does
- not have to be, *self*). If a specific method is not defined, the augmented
- assignment falls back to the normal methods. For instance, if *x* is an
- instance of a class with an :meth:`__iadd__` method, ``x += y`` is equivalent
- to ``x = x.__iadd__(y)`` . Otherwise, ``x.__add__(y)`` and ``y.__radd__(x)``
- are considered, as with the evaluation of ``x + y``. In certain situations,
- augmented assignment can result in unexpected errors (see
- :ref:`faq-augmented-assignment-tuple-error`), but this behavior is in
- fact part of the data model.
+ (``+=``, ``-=``, ``*=``, ``@=``, ``/=``, ``//=``, ``%=``, ``**=``, ``<<=``,
+ ``>>=``, ``&=``, ``^=``, ``|=``). These methods should attempt to do the
+ operation in-place (modifying *self*) and return the result (which could be,
+ but does not have to be, *self*). If a specific method is not defined, the
+ augmented assignment falls back to the normal methods. For instance, if *x*
+ is an instance of a class with an :meth:`__iadd__` method, ``x += y`` is
+ equivalent to ``x = x.__iadd__(y)`` . Otherwise, ``x.__add__(y)`` and
+ ``y.__radd__(x)`` are considered, as with the evaluation of ``x + y``. In
+ certain situations, augmented assignment can result in unexpected errors (see
+ :ref:`faq-augmented-assignment-tuple-error`), but this behavior is in fact
+ part of the data model.
.. method:: object.__neg__(self)
@@ -2174,7 +2191,7 @@ For more information on context managers, see :ref:`typecontextmanager`.
.. seealso::
- :pep:`0343` - The "with" statement
+ :pep:`343` - The "with" statement
The specification, background, and examples for the Python :keyword:`with`
statement.
@@ -2254,6 +2271,203 @@ special methods (the special method *must* be set on the class
object itself in order to be consistently invoked by the interpreter).
+.. index::
+ single: coroutine
+
+Coroutines
+==========
+
+
+Awaitable Objects
+-----------------
+
+An :term:`awaitable` object generally implements an :meth:`__await__` method.
+:term:`Coroutine` objects returned from :keyword:`async def` functions
+are awaitable.
+
+.. note::
+
+ The :term:`generator iterator` objects returned from generators
+ decorated with :func:`types.coroutine` or :func:`asyncio.coroutine`
+ are also awaitable, but they do not implement :meth:`__await__`.
+
+.. method:: object.__await__(self)
+
+ Must return an :term:`iterator`. Should be used to implement
+ :term:`awaitable` objects. For instance, :class:`asyncio.Future` implements
+ this method to be compatible with the :keyword:`await` expression.
+
+.. versionadded:: 3.5
+
+.. seealso:: :pep:`492` for additional information about awaitable objects.
+
+
+.. _coroutine-objects:
+
+Coroutine Objects
+-----------------
+
+:term:`Coroutine` objects are :term:`awaitable` objects.
+A coroutine's execution can be controlled by calling :meth:`__await__` and
+iterating over the result. When the coroutine has finished executing and
+returns, the iterator raises :exc:`StopIteration`, and the exception's
+:attr:`~StopIteration.value` attribute holds the return value. If the
+coroutine raises an exception, it is propagated by the iterator. Coroutines
+should not directly raise unhandled :exc:`StopIteration` exceptions.
+
+Coroutines also have the methods listed below, which are analogous to
+those of generators (see :ref:`generator-methods`). However, unlike
+generators, coroutines do not directly support iteration.
+
+.. versionchanged:: 3.5.2
+ It is a :exc:`RuntimeError` to await on a coroutine more than once.
+
+
+.. method:: coroutine.send(value)
+
+ Starts or resumes execution of the coroutine. If *value* is ``None``,
+ this is equivalent to advancing the iterator returned by
+ :meth:`__await__`. If *value* is not ``None``, this method delegates
+ to the :meth:`~generator.send` method of the iterator that caused
+ the coroutine to suspend. The result (return value,
+ :exc:`StopIteration`, or other exception) is the same as when
+ iterating over the :meth:`__await__` return value, described above.
+
+.. method:: coroutine.throw(type[, value[, traceback]])
+
+ Raises the specified exception in the coroutine. This method delegates
+ to the :meth:`~generator.throw` method of the iterator that caused
+ the coroutine to suspend, if it has such a method. Otherwise,
+ the exception is raised at the suspension point. The result
+ (return value, :exc:`StopIteration`, or other exception) is the same as
+ when iterating over the :meth:`__await__` return value, described
+ above. If the exception is not caught in the coroutine, it propagates
+ back to the caller.
+
+.. method:: coroutine.close()
+
+ Causes the coroutine to clean itself up and exit. If the coroutine
+ is suspended, this method first delegates to the :meth:`~generator.close`
+ method of the iterator that caused the coroutine to suspend, if it
+ has such a method. Then it raises :exc:`GeneratorExit` at the
+ suspension point, causing the coroutine to immediately clean itself up.
+ Finally, the coroutine is marked as having finished executing, even if
+ it was never started.
+
+ Coroutine objects are automatically closed using the above process when
+ they are about to be destroyed.
+
+.. _async-iterators:
+
+Asynchronous Iterators
+----------------------
+
+An *asynchronous iterable* is able to call asynchronous code in its
+``__aiter__`` implementation, and an *asynchronous iterator* can call
+asynchronous code in its ``__anext__`` method.
+
+Asynchronous iterators can be used in an :keyword:`async for` statement.
+
+.. method:: object.__aiter__(self)
+
+ Must return an *asynchronous iterator* object.
+
+.. method:: object.__anext__(self)
+
+ Must return an *awaitable* resulting in a next value of the iterator. Should
+ raise a :exc:`StopAsyncIteration` error when the iteration is over.
+
+An example of an asynchronous iterable object::
+
+ class Reader:
+ async def readline(self):
+ ...
+
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ val = await self.readline()
+ if val == b'':
+ raise StopAsyncIteration
+ return val
+
+.. versionadded:: 3.5
+
+.. note::
+
+ .. versionchanged:: 3.5.2
+ Starting with CPython 3.5.2, ``__aiter__`` can directly return
+ :term:`asynchronous iterators <asynchronous iterator>`. Returning
+ an :term:`awaitable` object will result in a
+ :exc:`PendingDeprecationWarning`.
+
+ The recommended way of writing backwards compatible code in
+ CPython 3.5.x is to continue returning awaitables from
+ ``__aiter__``. If you want to avoid the PendingDeprecationWarning
+ and keep the code backwards compatible, the following decorator
+ can be used::
+
+ import functools
+ import sys
+
+ if sys.version_info < (3, 5, 2):
+ def aiter_compat(func):
+ @functools.wraps(func)
+ async def wrapper(self):
+ return func(self)
+ return wrapper
+ else:
+ def aiter_compat(func):
+ return func
+
+ Example::
+
+ class AsyncIterator:
+
+ @aiter_compat
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ ...
+
+ Starting with CPython 3.6, the :exc:`PendingDeprecationWarning`
+ will be replaced with the :exc:`DeprecationWarning`.
+ In CPython 3.7, returning an awaitable from ``__aiter__`` will
+ result in a :exc:`RuntimeError`.
+
+
+Asynchronous Context Managers
+-----------------------------
+
+An *asynchronous context manager* is a *context manager* that is able to
+suspend execution in its ``__aenter__`` and ``__aexit__`` methods.
+
+Asynchronous context managers can be used in an :keyword:`async with` statement.
+
+.. method:: object.__aenter__(self)
+
+ This method is semantically similar to the :meth:`__enter__`, with only
+ difference that it must return an *awaitable*.
+
+.. method:: object.__aexit__(self, exc_type, exc_value, traceback)
+
+ This method is semantically similar to the :meth:`__exit__`, with only
+ difference that it must return an *awaitable*.
+
+An example of an asynchronous context manager class::
+
+ class AsyncContextManager:
+ async def __aenter__(self):
+ await log('entering context')
+
+ async def __aexit__(self, exc_type, exc, tb):
+ await log('exiting context')
+
+.. versionadded:: 3.5
+
+
.. rubric:: Footnotes
.. [#] It *is* possible in some cases to change an object's type, under certain