Skip to content

Commit 1028ca4

Browse files
miss-islingtonappeltel
authored andcommitted
bpo-26701: Add documentation for __trunc__ (GH-6050)
`int` fails back to `__trunc__` is `__int__` isn't defined, so cover that in the docs. (cherry picked from commit 308eab9) Co-authored-by: Eric Appelt <eric.appelt@gmail.com>
1 parent 19b42fe commit 1028ca4

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Doc/library/functions.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,11 @@ are always available. They are listed here in alphabetical order.
717717

718718
Return an integer object constructed from a number or string *x*, or return
719719
``0`` if no arguments are given. If *x* is a number, return
720-
:meth:`x.__int__() <object.__int__>`. For floating point numbers, this
721-
truncates towards zero.
720+
:meth:`x.__int__() <object.__int__>`. If *x* defines
721+
:meth:`x.__trunc__() <object.__trunc__>` but not
722+
:meth:`x.__int__() <object.__int__>`, then return
723+
if :meth:`x.__trunc__() <object.__trunc__>`. For floating point numbers,
724+
this truncates towards zero.
722725

723726
If *x* is not a number or if *base* is given, then *x* must be a string,
724727
:class:`bytes`, or :class:`bytearray` instance representing an :ref:`integer

Doc/library/math.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Number-theoretic and representation functions
179179

180180
Return the :class:`~numbers.Real` value *x* truncated to an
181181
:class:`~numbers.Integral` (usually an integer). Delegates to
182-
``x.__trunc__()``.
182+
:meth:`x.__trunc__() <object.__trunc__>`.
183183

184184

185185
Note that :func:`frexp` and :func:`modf` have a different call/return pattern

Doc/reference/datamodel.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,6 +2312,15 @@ left undefined.
23122312
of the appropriate type.
23132313

23142314

2315+
.. method:: object.__trunc__(self)
2316+
2317+
Called to implement :meth:`math.trunc`. Should return the value of the
2318+
object truncated to a :class:`numbers.Integral` (typically an
2319+
:class:`int`). If a class defines :meth:`__trunc__` but not
2320+
:meth:`__int__`, then :meth:`__trunc__` is called to implement the
2321+
built-in function :func:`int`.
2322+
2323+
23152324
.. method:: object.__index__(self)
23162325

23172326
Called to implement :func:`operator.index`, and whenever Python needs to

0 commit comments

Comments
 (0)