Skip to content

Commit f34e0d6

Browse files
miss-islingtonappeltel
authored andcommitted
bpo-26701: Add documentation for __trunc__ (GH-6049)
`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 6826589 commit f34e0d6

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
@@ -731,8 +731,11 @@ are always available. They are listed here in alphabetical order.
731731

732732
Return an integer object constructed from a number or string *x*, or return
733733
``0`` if no arguments are given. If *x* is a number, return
734-
:meth:`x.__int__() <object.__int__>`. For floating point numbers, this
735-
truncates towards zero.
734+
:meth:`x.__int__() <object.__int__>`. If *x* defines
735+
:meth:`x.__trunc__() <object.__trunc__>` but not
736+
:meth:`x.__int__() <object.__int__>`, then return
737+
if :meth:`x.__trunc__() <object.__trunc__>`. For floating point numbers,
738+
this truncates towards zero.
736739

737740
If *x* is not a number or if *base* is given, then *x* must be a string,
738741
: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
@@ -203,7 +203,7 @@ Number-theoretic and representation functions
203203

204204
Return the :class:`~numbers.Real` value *x* truncated to an
205205
:class:`~numbers.Integral` (usually an integer). Delegates to
206-
``x.__trunc__()``.
206+
:meth:`x.__trunc__() <object.__trunc__>`.
207207

208208

209209
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
@@ -2377,6 +2377,15 @@ left undefined.
23772377
of the appropriate type.
23782378

23792379

2380+
.. method:: object.__trunc__(self)
2381+
2382+
Called to implement :meth:`math.trunc`. Should return the value of the
2383+
object truncated to a :class:`numbers.Integral` (typically an
2384+
:class:`int`). If a class defines :meth:`__trunc__` but not
2385+
:meth:`__int__`, then :meth:`__trunc__` is called to implement the
2386+
built-in function :func:`int`.
2387+
2388+
23802389
.. method:: object.__index__(self)
23812390

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

0 commit comments

Comments
 (0)