8000 Merge pull request #11616 from shoyer/nep-18-protocols-for-methods · numpy/numpy@e14db2f · GitHub
[go: up one dir, main page]

Skip to content

Commit e14db2f

Browse files
authored
Merge pull request #11616 from shoyer/nep-18-protocols-for-methods
DOC: add a brief note on "Protocols for methods" to NEP 18
2 parents 5a0a136 + 877c039 commit e14db2f

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

doc/neps/nep-0018-array-function-protocol.rst

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,6 @@ decorator directly, but override implementation in terms of
381381
decorators, but they could still use a C equivalent of
382382
``try_array_function_override``. If performance is not a concern, they could
383383
also be easily wrapped with a small Python wrapper.
384-
- The ``__call__`` method of ``np.vectorize`` can't be decorated with
385-
``@array_function_dispatch``, because we should pass a ``vectorize`` object
386-
itself as the ``func`` argument ``__array_function__``, not the unbound
387-
``vectorize.__call__`` method.
388384
- ``np.einsum`` does complicated argument parsing to handle two different
389385
function signatures. It would probably be best to avoid the overhead of
390386
parsing it twice in the typical case of no overrides.
@@ -799,6 +795,31 @@ If we want to include them in the future, the easiest way to do so would be to
799795
update the ``array_function_dispatch`` decorator to add them as function
800796
attributes.
801797

798+
Callable objects generated at runtime
799+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
800+
801+
NumPy has some APIs that define callable objects *dynamically*, such as
802+
``vectorize`` and methods on ``random.RandomState`` object. Examples can
803+
also be found in other core libraries in the scientific Python stack, e.g.,
804+
distribution objects in scipy.stats and model objects in scikit-learn. It would
805+
be nice to be able to write overloads for such callables, too. This presents a
806+
challenge for the ``__array_function__`` protocol, because unlike the case for
807+
functions there is no public object in the ``numpy`` namespace to pass into
808+
the ``func`` argument.
809+
810+
We could potentially handle this by establishing an alternative convention
811+
for how the ``func`` argument could be inspected, e.g., by using
812+
``func.__self__`` to obtain the class object and ``func.__func__`` to return
813+
the unbound function object. However, some caution is in order, because
814+
this would immesh what are currently implementation details as a permanent
815+
features of the interface, such as the fact that ``vectorize`` is implemented as a
816+
class rather than closure, or whether a method is implemented directly or using
817+
a descriptor.
818+
819+
Given the complexity and the limited use cases, we are also deferring on this
820+
issue for now, but we are confident that ``__array_function__`` could be
821+
expanded to accomodate these use cases in the future if need be.
822+
802823
Discussion
803824
----------
804825

0 commit comments

Comments
 (0)
0