]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-26701: Add documentation for __trunc__ (GH-6050)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 10 Mar 2018 15:10:45 +0000 (07:10 -0800)
committerNick Coghlan <ncoghlan@gmail.com>
Sat, 10 Mar 2018 15:10:45 +0000 (01:10 +1000)
`int` fails back to `__trunc__` is `__int__` isn't defined, so cover
that in the docs.
(cherry picked from commit 308eab979d153f1ab934383dc08bc4546ced8b6c)

Co-authored-by: Eric Appelt <eric.appelt@gmail.com>
Doc/library/functions.rst
Doc/library/math.rst
Doc/reference/datamodel.rst

index bc4203081ed0457735a61db2793425865f15da3d..9cb6b0e1b5ca9f8dfa66ed81c21ed6bafd22b8ae 100644 (file)
@@ -717,8 +717,11 @@ 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* is a number, return
-   :meth:`x.__int__() <object.__int__>`.  For floating point numbers, this
-   truncates towards zero.
+   :meth:`x.__int__() <object.__int__>`. If *x* defines
+   :meth:`x.__trunc__() <object.__trunc__>` but not
+   :meth:`x.__int__() <object.__int__>`, then return
+   if :meth:`x.__trunc__() <object.__trunc__>`.  For floating point numbers,
+   this truncates towards zero.
 
    If *x* is not a number or if *base* is given, then *x* must be a string,
    :class:`bytes`, or :class:`bytearray` instance representing an :ref:`integer
index da2b8cc586279150561e5650fa99b7f099148ece..0f36a3fa99eb3b82c0bcb1042beed748a1bbde48 100644 (file)
@@ -179,7 +179,7 @@ Number-theoretic and representation functions
 
    Return the :class:`~numbers.Real` value *x* truncated to an
    :class:`~numbers.Integral` (usually an integer). Delegates to
-   ``x.__trunc__()``.
+   :meth:`x.__trunc__() <object.__trunc__>`.
 
 
 Note that :func:`frexp` and :func:`modf` have a different call/return pattern
index c95f4a970930370702b9b95f56eff2c3ce6a4c8a..90f3a2aa0dcdd98d358a675d42db8580c8ea9b39 100644 (file)
@@ -2312,6 +2312,15 @@ left undefined.
    of the appropriate type.
 
 
+.. method:: object.__trunc__(self)
+
+   Called to implement :meth:`math.trunc`.  Should return the value of the
+   object truncated to a :class:`numbers.Integral` (typically an
+   :class:`int`).  If a class defines :meth:`__trunc__` but not
+   :meth:`__int__`, then :meth:`__trunc__` is called to implement the
+   built-in function :func:`int`.
+
+
 .. method:: object.__index__(self)
 
    Called to implement :func:`operator.index`, and whenever Python needs to