Raymond Hettinger added the comment:

The different data types make different choices:

>>> from decimal import Decimal
>>> from fractions import Fraction
>>> (1).__round__(None)
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    (1).__round__(None)
TypeError: 'NoneType' object cannot be interpreted as an integer
>>> (1.0).__round__(None)
1
>>> Decimal(1).__round__(None)
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    Decimal(1).__round__(None)
TypeError: optional arg must be an integer
>>> Fraction(1, 1).__round__(None)
1
>>> from _pydecimal import Decimal
>>> Decimal(1).__round__(None)
1

For Fraction and _pydecimal, the behavior comes from using None as a 
placeholder (which is common and normal in pure python code).  For float there 
is explicit code to test for the None case.   For int, the None test was 
omitted (perhaps a mistake) and the error is raised by PyNumber_Index.

Looking through tests, only Lib/test/test_float.py tests for None being 
allowable.  Elsewhere, it seems to be an implementation detail.

The old Python 2 version of the round() function never let None be passed in 
(because it used PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round").  That 
logic seems to get lost in the Python 3 version when __round__ was introduced.

To resolve the differences, I think the round() function should explicitly 
check for None and replace it with zero before calling the underling __round__ 
functions where we would allow variable according the needs of the 
implementation.

----------
nosy: +rhettinger

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27936>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to