Skip to content

Commit de8567e

Browse files
bpo-26701: Improve documentation for the rounding special methods. (GH-6054)
(cherry picked from commit 496431f) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 1028ca4 commit de8567e

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

Doc/reference/datamodel.rst

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,28 +2299,17 @@ left undefined.
22992299
.. method:: object.__complex__(self)
23002300
object.__int__(self)
23012301
object.__float__(self)
2302-
object.__round__(self, [,n])
23032302

23042303
.. index::
23052304
builtin: complex
23062305
builtin: int
23072306
builtin: float
2308-
builtin: round
23092307

23102308
Called to implement the built-in functions :func:`complex`,
2311-
:func:`int`, :func:`float` and :func:`round`. Should return a value
2309+
:func:`int` and :func:`float`. Should return a value
23122310
of the appropriate type.
23132311

23142312

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-
23242313
.. method:: object.__index__(self)
23252314

23262315
Called to implement :func:`operator.index`, and whenever Python needs to
@@ -2336,6 +2325,23 @@ left undefined.
23362325
the same value.
23372326

23382327

2328+
.. method:: object.__round__(self, [,ndigits])
2329+
object.__trunc__(self)
2330+
object.__floor__(self)
2331+
object.__ceil__(self)
2332+
2333+
.. index:: builtin: round
2334+
2335+
Called to implement the built-in function :func:`round` and :mod:`math`
2336+
functions :func:`~math.trunc`, :func:`~math.floor` and :func:`~math.ceil`.
2337+
Unless *ndigits* is passed to :meth:`!__round__` all these methods should
2338+
return the value of the object truncated to an :class:`~numbers.Integral`
2339+
(typically an :class:`int`).
2340+
2341+
If :meth:`__int__` is not defined then the built-in function :func:`int`
2342+
falls back to :meth:`__trunc__`.
2343+
2344+
23392345
.. _context-managers:
23402346

23412347
With Statement Context Managers

0 commit comments

Comments
 (0)