[Numpy-discussion] Revised NEP-18, __array_function__ protocol

Stephan Hoyer shoyer at gmail.com
Thu Jun 28 14:04:19 EDT 2018


On Wed, Jun 27, 2018 at 12:50 PM Stephan Hoyer <shoyer at gmail.com> wrote:

> One concern this does raise is how to handle methods like those on
> RandomState, even though methods like random_like() don't currently exist.
> Distribution objects from scipy.stats could have similar use cases.
>
> So perhaps it's worth "future proofing" the interface by passing `obj` and
> `method` to __array_function__ rather than only `func`. It is slower to
> call a func via func.__call__ than func, but only very marginally (~100 ns
> in my tests).
>

I did a little more digging, and turned up the __self__ and __func__
attributes of bound methods:
https://stackoverflow.com/questions/4679592/how-to-find-instance-of-a-bound-method-in-python

So we might need another decorator function, but it seems that the current
interface would actually suffice just fine for overriding methods. I'll
update the NEP with some examples. It will look something like:

def __array_function__(self, func, types, args, kwargs):
  ...
  if isinstance(func, types.MethodType):
    object = func.__self__
    unbound_func = func.__func__
    ...

Given that functions are the most common case, I think it's best to keep
with `func` as the main interface, but it's good to know that this does not
preclude overriding methods.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180628/c44a436f/attachment.html>


More information about the NumPy-Discussion mailing list