title: Understanding the difference between numpy.log and numpy.sum for custom array containers
body: <p>I am creating a custom container for a NumPy array by following <a href="https://docs.scipy.org/doc/numpy/user/basics.dispatch.html" rel="nofollow noreferrer">the instructions on the SciPy website</a>. I've written the following code which:</p>
  
  <ul>
  <li>creates a class, <code>NpContainer</code></li>
  <li>defines custom behavior for the functions <code>np.sum</code> and <code>np.log</code> to output a string.</li>
  </ul>
  
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  
  HANDLED_FUNCTIONS = {}
  
  class NpContainer:
      def __init__(self, val):
          self.val = val
  
      def __array__(self):
          return np.array(self.val)
  
      def __array_function__(self, func, types, args, kwargs):
          if func not in HANDLED_FUNCTIONS:
              raise NotImplementedError()
  
          return HANDLED_FUNCTIONS[func](*args, **kwargs)
  
  def implements(np_function):
      def decorator(func):
          HANDLED_FUNCTIONS[np_function] = func
          return func
      return decorator
  
  @implements(np.sum)
  def sum(a, **kwargs):
      return 'Sum Val: {}'.format(np.sum(a.val, **kwargs))
  
  @implements(np.log)
  def log(a, **kwargs):
      return 'Log Val: {}'.format(np.log(a, **kwargs))
  </code></pre>
  
  <p>I test the code using:</p>
  
  <pre class="lang-py prettyprint-override"><code>if __name__ == "__main__":
      container1 = NpContainer(val=np.array([1., 2.]))
  
      sum_result = np.sum(container1)
      print(sum_result)
      print(type(sum_result))
  
      log_result = np.log(container1)
      print(log_result)
      print(type(log_result))
  </code></pre>
  
  <p>Sum produces the expected result.</p>
  
  <pre><code>Sum Val: 3.0
  &lt;class 'str'&gt;
  </code></pre>
  
  <p>However <code>np.log</code> returns a NumPy array instead of a string.</p>
  
  <pre><code>[0.         0.69314718]
  &lt;class 'numpy.ndarray'&gt;
  </code></pre>
  
  <p>Does anyone know why <code>np.log</code> skips my custom-defined function? Any help is appreciated!</p>