I used numpy to do large scale data analysis, with lots of matrix implementations (e.g., dot
, count_nonzero
, linalg.svd
). After %pru
Recent versions of NumPy support an __array_function__ hook that objects can implement to customize what arbitrary NumPy callables do when called on them. Support is disabled by default in 1.16, enabled by default in 1.17, and expected to eventually be enabled unconditionally.
implement_array_function
is the dispatcher that calls either a default implementation or an __array_function__
hook, to implement __array_function__
support. As designed, it is intended to be called once for literally every single call to a public NumPy callable, including calls happening within NumPy, and it has to do a lot of method lookups. Hopefully future optimization work will reduce some of this overhead.
You can see additional details in NEP 18, and you can check the function's docstring with help(numpy.core._multiarray_umath.implement_array_function)
:
Help on built-in function implement_array_function in module numpy.core._multiarray_umath:
implement_array_function(...)
Implement a function with checks for __array_function__ overrides.
All arguments are required, and can only be passed by position.
Arguments
---------
implementation : function
Function that implements the operation on NumPy array without
overrides when called like ``implementation(*args, **kwargs)``.
public_api : function
Function exposed by NumPy's public API originally called like
``public_api(*args, **kwargs)`` on which arguments are now being
checked.
relevant_args : iterable
Iterable of arguments to check for __array_function__ methods.
args : tuple
Arbitrary positional arguments originally passed into ``public_api``.
kwargs : dict
Arbitrary keyword arguments originally passed into ``public_api``.
Returns
-------
Result from calling ``implementation()`` or an ``__array_function__``
method, as appropriate.
Raises
------
TypeError : if no implementation is found.