问题描述
显然(在未来"中)将不再使用以下内容:
Apparantly it will (in the 'future') not be possible anymore to use the following:
import numpy as np
np.array([0,1,2]) == None
> False
> FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
这也打破了numpy数组的延迟加载模式:
This also breaks the lazy loading pattern for numpy arrays:
import numpy as np
def f(a=None):
if a == None:
a = <some default value>
<function body>
还有什么其他可能性可以让您仍然使用延迟初始化?
What other possibilities allow you to still use lazy initialization?
推荐答案
您在寻找is
:
if a is None:
a = something else
问题在于,通过使用==
运算符,如果输入元素a
是一个numpy数组,则numpy将尝试对元素进行明智的比较,并告知您无法对其进行比较.
The problem is that, by using the ==
operator, if the input element a
is a numpy array, numpy will try to perform an element wise comparison and tell you that you cannot compare it.
对于a
一个numpy数组,a == None
给出错误,而np.all(a == None)
不给出错误(但不执行您期望的操作).取而代之的是a is None
将起作用,而与a
的数据类型无关.
For a
a numpy array, a == None
gives error, np.all(a == None)
doesn't (but does not do what you expect). Instead a is None
will work regardless the data type of a
.
这篇关于与`None`的比较将导致元素对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!