为什么numpy.dtype(numpy.float64)评估为False [英] Why does numpy.dtype(numpy.float64) evaluate to False

查看:344
本文介绍了为什么numpy.dtype(numpy.float64)评估为False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释以下脚本输出背后的逻辑吗?

Can someone explain the logic behind the output of the following script?

import numpy
if(numpy.dtype(numpy.float64):
    print "Expected"
else:
    print "Surprise!!!!"

特别考虑:

import numpy
if(object):
    print "Expected!"
else:
    print "Surprise"

谢谢:)

推荐答案

np.dtype没有定义__nonzero__,但确实定义了__len__.根据文档,这意味着当您在布尔上下文中使用它时,如果__len__返回非零值,则将得出True.但无论您传入哪种类型,它始终返回零:

np.dtype does not define __nonzero__, but it does define __len__. As per the documentation, this means when you use it in a boolean context, it will evaluate to True if __len__ returns non-zero. But it always returns zero, regardless of what type you pass in:

>>> bool(np.dtype(int))
False
>>> bool(np.dtype(float))
False
>>> bool(np.dtype(np.int8))
False

另一方面,复合数据类型的确返回非零,因此为True:

On the other hand, a compound data type does return nonzero, thus True:

>>> bool(np.dtype([('foo', int)]))
True

然后您可能会问,当具有单个元素的复合物1的长度为1时,为什么简单dtype的长度"为零.我想这与维数有关:具有简单dtype和一维大小的数组本身就是一维的,但是具有复合dtype和一维大小的数组可能被认为是二维的,而不管有多少个元素属于复合dtype.

You might then ask why the "length" of a simple dtype is zero, when the length of a compound one with a single element is one. I imagine that's something about dimensionality: an array with a simple dtype and one-dimensional size is itself one-dimensional, but an array with a compound dtype and one-dimensional size may be thought of as two-dimensional, regardless of how many elements are in the compound dtype.

这篇关于为什么numpy.dtype(numpy.float64)评估为False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆