这不是一个问题,而是一个好奇心。在
在64位linux上的解释器中,我可以执行
In [10]: np.int64 == np.int64
Out[10]: True
In [11]: np.int64 is np.int64
Out[11]: True
太好了,正是我所期待的。 但是我发现了numpy.core.numeric模块
^{pr2}$奇怪为什么数字.int64进去两次?让我们调查一下。在
In [23]: _typelessdata[0] is _typelessdata[-1]
Out[23]: False
In [24]: _typelessdata[0] == _typelessdata[-1]
Out[24]: False
In [25]: id(_typelessdata[-1])
Out[25]: 139990931572128
In [26]: id(_typelessdata[0])
Out[26]: 139990931572544
In [27]: _typelessdata[-1]
Out[27]: numpy.int64
In [28]: _typelessdata[0]
Out[28]: numpy.int64
哇哦,他们不一样。这是怎么回事?为什么有两个np.int64是的?在
Here是在
numeric.py
内构造_typelessdata
的行:intc
是一个与C兼容的(32位)有符号整数,int
是本机Python 整数,可能是32位或64位,具体取决于平台。在位{32位Python也是本机类型
^{pr2}$issubclass(intc, int)
返回True
,而intc
被附加到_typelessdata
, 结果是这样的:注意}。
_typelessdata[-1] is numpy.intc
,而不是{在64位系统中,},并且
int
是64位,因此issubclass(longlong, int)
返回{longlong
被附加到_typelessdata
,结果是:在这种情况下,正如Joe所指出的,
(_typelessdata[-1] is numpy.longlong) == True
。更大的问题是为什么} 的定义内
在同一个文件中:
_typelessdata
的内容是这样设置的。 我在numpy源代码中唯一能找到_typelessdata
的地方 实际使用的是this line在^{_typelessdata
的目的是确保np.array_repr
正确地打印dtype
恰好与(依赖于平台的)本机Python整数类型相同的数组的字符串表示。在例如,在32位系统中,
int
是32位:而在64位系统中,
int
是64位:上面一行中的}。在
arr.dtype.type in _typelessdata
检查可确保为适当的依赖于平台的本机整数dtypes
跳过打印{我不知道它背后的全部历史,但第二个}。在
int64
实际上是{numpy.longlong
应该是directly correspond to C's ^{long long
被指定为至少64位宽,但确切的定义由编译器决定。在我的猜测是
numpy.longlong
在大多数系统上是numpy.int64
的另一个实例,但是如果C编译器将long long
定义为比64位更宽的内容,则允许它是不同的。在相关问题 更多 >
编程相关推荐