为什么int(maxint)给出long,而int(int(maxint))给出int?这是一个 NumPy 错误吗?
·
问题:为什么int(maxint)给出long,而int(int(maxint))给出int?这是一个 NumPy 错误吗?
不言自明(我在 Windows 上):
>>> import sys, numpy
>>> a = numpy.int_(sys.maxint)
>>> int(a).__class__
<type 'long'>
>>> int(int(a)).__class__
<type 'int'>
为什么调用一次int
给我一个long
,而调用它两次给我一个int
?
这是错误还是功能?
解答
这个问题特定于 Numpy 和 Python 2。在 Python 3 中,没有单独的int
和long
类型。
该行为是由于 numpy.带有一个参数的int(x)
通过调用PyNumber_Int(x)
将x
转换为数字。PyNumber_Int
然后专门为int
子类](https://github.com/python/cpython/blob/2.7/Objects/abstract.c#L1632)采用[路径,因为numpy.int_
返回的int64
是int
的子类:
m = o->ob_type->tp_as_number;
if (m && m->nb_int) { /* This should include subclasses of int */
/* Classic classes always take this branch. */
PyObject *res = m->nb_int(o);
if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
PyErr_Format(PyExc_TypeError,
"__int__ returned non-int (type %.200s)",
res->ob_type->tp_name);
Py_DECREF(res);
return NULL;
}
return res;
}
现在,此代码调用a->ob_type->tp_as_number->nb_int
,它在numpy/core/src/umath/scalarmath.c.src中实现。这是为不同类型参数化的代码的位置;这一个用于<typename>_int
方法,用于填充nb_int
方法槽。它有以下一个if
:
if(LONG_MIN < x && x < LONG_MAX)
return PyInt_FromLong(x);
两个运算符都应该是<=
。有了<
,LONG_MIN
和LONG_MAX
都没有通过条件,而是在第 1432 行处将它们转换为PyLong
:
return @func@(x);
在int_
的情况下,@func@
被PyLong_FromLongLong
替换。因此,返回long(sys.maxint)
。
现在,由于sys.maxint
仍然可以由int
表示,因此int(long(sys.maxint))
返回int
;同样int(sys.maxint + 1)
返回一个long
。
更多推荐
所有评论(0)