Why does the round go up ndigits = None for integers, but not for float?

Why round()does it behave differently for int and float when ndigitsexplicitly set to None?

Console test in Python 3.5.1:

>>> round(1, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object cannot be interpreted as an integer
>>> round(1.0, None)
1

Update:

I reported this as an error ( Issue No. 27936 ) that was fixed and closed. @PadraicCunningham and @CraigBurgler were correct.

The fix was included in the following final versions:

+4
source share
1 answer

From the source code for float_roundto floatobjects.cin 3.5:

float_round(PyObject *v, PyObject *args)
    ... 
    if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
        return NULL;
    ...
    if (o_ndigits == NULL || o_ndigits == Py_None) {
        /* single-argument round or with None ndigits:
         * round to nearest integer */
        ...

|| o_ndigits == Py_None ndigits=None , round .

3.4 :

float_round(PyObject *v, PyObject *args)
    ... 
    if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
        return NULL;
    ...
    if (o_ndigits == NULL) {
            /* single-argument round: round to nearest integer */
    ...

|| o_ndigits == Py_None , , ndgits=None int, TypeError round(1.0, None) 3.4.

o_ndigits == Py_None long_round longobject.c 3.4, 3.5, TypeError round(1, None) 3.4, 3.5

                treat ndigits=None as      resolve
Version/Type    single-argument call    round(n, None) 
 ----------       ---------------        -----------
  3.4/float              No               TypeError
  3.4/long               No               TypeError
  3.5/float              Yes               round(n)
  3.5/long               No               TypeError
+3

Source: https://habr.com/ru/post/1653324/


All Articles