[Numpy-discussion] Suggestion: special-case np.array(range(...)) to be faster

Ralf Gommers ralf.gommers at gmail.com
Sun Feb 14 09:36:05 EST 2016


On Sun, Feb 14, 2016 at 9:21 AM, Antony Lee <antony.lee at berkeley.edu> wrote:

> re: no reason why...
> This has nothing to do with Python2/Python3 (I personally stopped using
> Python2 at least 3 years ago.)  Let me put it this way instead: if
> Python3's "range" (or Python2's "xrange") was not a builtin type but a type
> provided by numpy, I don't think it would be controversial at all to
> provide an `__array__` special method to efficiently convert it to a
> ndarray.  It would be the same if `np.array` used a
> `functools.singledispatch` dispatcher rather than an `__array__` special
> method (which is obviously not possible for chronological reasons).
>
> re: iterable vs iterator: check for the presence of the __next__ special
> method (or isinstance(x, Iterable) vs. isinstance(x, Iterator) and not
> isinstance(x, Iterable))
>

I think it's good to do something about this, but it's not clear what the
exact proposal is. I could image one or both of:

  - special-case the range() object in array (and asarray/asanyarray?) such
that array(range(N)) becomes as fast as arange(N).
  - special-case all iterators, such that array(range(N)) becomes as fast
as deque(range(N))

or yet something else?

Ralf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160214/154ee1c8/attachment.html>


More information about the NumPy-Discussion mailing list