[Numpy-discussion] Revised NEP-18, array_function protocol (original) (raw)
Marten van Kerkwijk m.h.vankerkwijk at gmail.com
Sat Jun 30 12:52:19 EDT 2018
- Previous message (by thread): [Numpy-discussion] Revised NEP-18, __array_function__ protocol
- Next message (by thread): [Numpy-discussion] Revised NEP-18, __array_function__ protocol
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi Hameer,
I think the override on dtype
would work - after all, the override is
checked before anything is done, so one can just pass in self
if one
wishes (or some helper class that contains both self
and any desired
further information.
But, as you note, it would not cover everything, and your array_reference
idea definitely makes things more uniform. Indeed, it would allow one to
implement things like np.zeros_like
using np.zero
, which seems quite
nice.
Still, I'm not sure whether this should be included in the present NEP or is best done separately after, with a few concrete examples of where it would be useful.
All the best,
Marten
On Sat, Jun 30, 2018 at 10:40 AM, Hameer Abbasi <einstein.edison at gmail.com> wrote:
Hi Marten,
Sorry, I had clearly misunderstood. It would indeed be nice for overrides to work on functions like
zeros
orarange
as well, but it seems strange to change the signature just for that. As a possible alternative, should we perhaps generally check for overrides ondtype
?While this very clearly makes sense for something like astropy, it has a few drawbacks: - Other duck arrays such as Dask need more information than just the dtype. For example, Dask needs chunk sizes, XArray needs axis labels, and pydata/sparse needs to know the type of the reference array in order to make one of the same type. The information in a reference array is a strict superset of information in the dtype. - There’s a need for a separate protocol, which might be a lot harder to work with for both NumPy and library authors. - Some things, like numpy.random.RandomState, don’t accept a dtype argument. As for your concern about changing the signature, it’s easy enough with a decorator. We’ll need a separate decorator for array generation functions. Something like: def arraygenerationfunction(func): @functools.wraps(func) def wrapped(*args, **kwargs, arrayreference=np.NoValue): if arrayreference is not np.NoValue: success, result = tryarrayfunctionoverride(wrapped, [arrayreference], args, kwargs) if success: return result return func(*args, **kwargs) return wrapped Hameer Abbasi
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/20180630/4937f23b/attachment-0001.html>
- Previous message (by thread): [Numpy-discussion] Revised NEP-18, __array_function__ protocol
- Next message (by thread): [Numpy-discussion] Revised NEP-18, __array_function__ protocol
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]