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

Marten van Kerkwijk m.h.vankerkwijk at gmail.com
Thu Jun 28 16:11:07 EDT 2018


> 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__
>     ...
>
>
For C classes like the ufuncs, it seems `__self__` is defined for methods
as well (at least, `np.add.reduce.__self__` gives `np.add`), but not a
`__func__`. There is a `__name__` (="reduce"), though, which means that I
think one can still retrieve what is needed (obviously, this also means
`__array_ufunc__` could have been simpler...)

-- Marten
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180628/a13db381/attachment.html>


More information about the NumPy-Discussion mailing list