问题描述
有人可以解释以下脚本输出背后的逻辑吗?
Can someone explain the logic behind the output of the following script?
import numpy
if(numpy.dtype(numpy.float64) == None):
print "Surprise!!!!"
谢谢:)
推荐答案
如果要比较python中None
的任意对象,则需要使用:
if you want to compare an arbitrary object against exactly None
in python you need to use:
object is None
在这种情况下,任何对象都可能会覆盖其比较运算符,从而无法执行您期望的操作.
Like in this case any object may override its comparison operator to not do what you are expecting.
为什么,在dtypes上下文中,dtype('float64')等同于None,就像dtypes等同于typestrings一样
As for why, dtype('float64') is equivalent to None in the context of dtypes in the same way dtypes are equivalent to typestrings
np.dtype('i4') == 'i4'
True
平等不是身份.
关于为什么dtype(None) == dtype('float64')
的原因,numpy中的许多函数都具有dtype=None
关键字参数.在大多数情况下,这意味着默认dtype为dtype(None)
.一个示例是np.zeros
.但也有例外可以从参数推断出dtype时,例如np.arange(10)
的情况,其中默认dtype将是整数类型(我认为是np.intp
).
As for why dtype(None) == dtype('float64')
, many functions in numpy have dtype=None
keyword arguments. In most cases this means default dtype which is dtype(None)
. An example is np.zeros
. But there are exceptions, e.g. when the dtype can be inferred from the arguments, like in the case of np.arange(10)
where the default dtype will be of integer type (np.intp
I think).
这篇关于为什么numpy.dtype('float64')很特殊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!