numpy 中这种奇怪的原因是什么all?
>>> import numpy as np
>>> np.all(xrange(10))
False
>>> np.all(i for i in xrange(10))
True
Numpy.all 不理解生成器表达式。
从文档
 numpy.all(a, axis=None, out=None)
    Test whether all array elements along a given axis evaluate to True.
    Parameters :    
    a : array_like
        Input array or object that can be converted to an array.
好的,不是很明确,所以让我们看一下代码
def all(a,axis=None, out=None):
    try:
        all = a.all
    except AttributeError:
        return _wrapit(a, 'all', axis, out)
    return all(axis, out)
def _wrapit(obj, method, *args, **kwds):
    try:
        wrap = obj.__array_wrap__
    except AttributeError:
        wrap = None
    result = getattr(asarray(obj),method)(*args, **kwds)
    if wrap:
        if not isinstance(result, mu.ndarray):
            result = asarray(result)
        result = wrap(result)
    return result
由于生成器表达式没有方法all,它最终调用_wrapit
In _wrapit,它首先检查最终调用生成器表达式的方法__array_wrap__generates AttributeErrorasarray
从文档numpy.asarray
 numpy.asarray(a, dtype=None, order=None)
    Convert the input to an array.
    Parameters :    
    a : array_like
        Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.
有据可查的关于接受的各种类型的输入数据,这绝对不是生成器表达式
最后,尝试
>>> np.asarray(0 for i in range(10))
array(<generator object <genexpr> at 0x42740828>, dtype=object)
奇怪的。当我尝试得到:
>>> np.all(i for i in xrange(10))
<generator object <genexpr> at 0x7f6e04c64500>
唔。
我不认为numpy 理解生成器表达式。尝试使用列表推导,你会得到:
>>> np.all([i for i in xrange(10)])
False