@@ -381,10 +381,6 @@ decorator directly, but override implementation in terms of
381
381
decorators, but they could still use a C equivalent of
382
382
``try_array_function_override ``. If performance is not a concern, they could
383
383
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.
388
384
- ``np.einsum `` does complicated argument parsing to handle two different
389
385
function signatures. It would probably be best to avoid the overhead of
390
386
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
799
795
update the ``array_function_dispatch `` decorator to add them as function
800
796
attributes.
801
797
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
+
802
823
Discussion
803
824
----------
804
825
0 commit comments