summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-06-01 21:05:48 (GMT)
committerGitHub <noreply@github.com>2019-06-01 21:05:48 (GMT)
commitbdbad71b9def0b86433de12cecca022eee91bd9f (patch)
tree147c8a3e08454a95e0e4900388d88f7b65430f63 /Doc/library
parent1a4d9ffa1aecd7e750195f2be06d3d16c7a3a88f (diff)
downloadcpython-bdbad71b9def0b86433de12cecca022eee91bd9f.zip
cpython-bdbad71b9def0b86433de12cecca022eee91bd9f.tar.gz
cpython-bdbad71b9def0b86433de12cecca022eee91bd9f.tar.bz2
bpo-20092. Use __index__ in constructors of int, float and complex. (GH-13108)
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/functions.rst21
1 files changed, 19 insertions, 2 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index 425a985..d5c9f18 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -318,6 +318,11 @@ are always available. They are listed here in alphabetical order.
:class:`int` and :class:`float`. If both arguments are omitted, returns
``0j``.
+ For a general Python object ``x``, ``complex(x)`` delegates to
+ ``x.__complex__()``. If ``__complex__()`` is not defined then it falls back
+ to :meth:`__float__`. If ``__float__()`` is not defined then it falls back
+ to :meth:`__index__`.
+
.. note::
When converting from a string, the string must not contain whitespace
@@ -330,6 +335,10 @@ are always available. They are listed here in alphabetical order.
.. versionchanged:: 3.6
Grouping digits with underscores as in code literals is allowed.
+ .. versionchanged:: 3.8
+ Falls back to :meth:`__index__` if :meth:`__complex__` and
+ :meth:`__float__` are not defined.
+
.. function:: delattr(object, name)
@@ -584,7 +593,8 @@ are always available. They are listed here in alphabetical order.
float, an :exc:`OverflowError` will be raised.
For a general Python object ``x``, ``float(x)`` delegates to
- ``x.__float__()``.
+ ``x.__float__()``. If ``__float__()`` is not defined then it falls back
+ to :meth:`__index__`.
If no argument is given, ``0.0`` is returned.
@@ -609,6 +619,9 @@ are always available. They are listed here in alphabetical order.
.. versionchanged:: 3.7
*x* is now a positional-only parameter.
+ .. versionchanged:: 3.8
+ Falls back to :meth:`__index__` if :meth:`__float__` is not defined.
+
.. index::
single: __format__
@@ -780,7 +793,8 @@ are always available. They are listed here in alphabetical order.
Return an integer object constructed from a number or string *x*, or return
``0`` if no arguments are given. If *x* defines :meth:`__int__`,
- ``int(x)`` returns ``x.__int__()``. If *x* defines :meth:`__trunc__`,
+ ``int(x)`` returns ``x.__int__()``. If *x* defines :meth:`__index__`,
+ it returns ``x.__index__()``. If *x* defines :meth:`__trunc__`,
it returns ``x.__trunc__()``.
For floating point numbers, this truncates towards zero.
@@ -812,6 +826,9 @@ are always available. They are listed here in alphabetical order.
.. versionchanged:: 3.7
*x* is now a positional-only parameter.
+ .. versionchanged:: 3.8
+ Falls back to :meth:`__index__` if :meth:`__int__` is not defined.
+
.. function:: isinstance(object, classinfo)