[Numpy-discussion] NEP 30 - Duck Typing for NumPy Arrays - Implementation
Stephan Hoyer
shoyer at gmail.com
Mon Sep 16 17:25:53 EDT 2019
On Mon, Sep 16, 2019 at 1:45 PM Peter Andreas Entschev <peter at entschev.com>
wrote:
> What would be the use case for a duck-array to implement __array__ and
> return a NumPy array? Unless I'm missing something, this seems
> redundant and one should just use array/asarray functions then. This
> would also prevent error-handling, what if the developer intentionally
> wants a NumPy-like array (e.g., the original array passed to the
> duckarray function) or an exception (instead of coercing to a NumPy
> array)?
>
Dask arrays are a good example. They will want to implement __duck_array__
(or whatever we call it) because they support duck typed versions of NumPy
operation. They also (already) implement __array__, so they can converted
into NumPy arrays as a fallback. This is convenient for moderately sized
dask arrays, e.g., so you can pass one into a matplotlib function.
>
> On Mon, Sep 16, 2019 at 9:25 PM Chris Barker <chris.barker at noaa.gov>
> wrote:
> >
> >
> >
> > On Mon, Aug 12, 2019 at 4:02 AM Peter Andreas Entschev <
> peter at entschev.com> wrote:
> >>
> >> Apologies for the late reply. I've opened a new PR
> >> https://github.com/numpy/numpy/pull/14257 with the changes requested
> >
> >
> > thanks!
> >
> > I've written a small PR on your PR:
> >
> > https://github.com/pentschev/numpy/pull/1
> >
> > Essentially, other than typos and copy editing, I'm suggesting that a
> duck-array could choose to implement __array__ if it so chooses -- it
> should, of course, return an actual numpy array.
> >
> > I think this could be useful, as much code does require an actual numpy
> array, and only that class itself knows how best to convert to one.
> >
> > -CHB
> >
> > --
> >
> > Christopher Barker, Ph.D.
> > Oceanographer
> >
> > Emergency Response Division
> > NOAA/NOS/OR&R (206) 526-6959 voice
> > 7600 Sand Point Way NE (206) 526-6329 fax
> > Seattle, WA 98115 (206) 526-6317 main reception
> >
> > Chris.Barker at noaa.gov
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at python.org
> > https://mail.python.org/mailman/listinfo/numpy-discussion
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190916/40c659bb/attachment-0001.html>
More information about the NumPy-Discussion
mailing list