summaryrefslogtreecommitdiff
path: root/numpy/doc/subclassing.py
diff options
context:
space:
mode:
authorMarten van Kerkwijk <mhvk@astro.utoronto.ca>2017-04-03 21:01:19 -0400
committerCharles Harris <charlesr.harris@gmail.com>2017-04-27 13:37:50 -0600
commit5f9252cc299149d213aa95dfc89b1131060a1289 (patch)
tree49e5b15f132a32a7e74eaa5b55f2acf57b687133 /numpy/doc/subclassing.py
parent0ede0e9c2619c7540d44311a3979a0890e1c3ec9 (diff)
downloadnumpy-5f9252cc299149d213aa95dfc89b1131060a1289.tar.gz
DOC: implement many smaller and bigger changes suggested in review.
Diffstat (limited to 'numpy/doc/subclassing.py')
-rw-r--r--numpy/doc/subclassing.py33
1 files changed, 18 insertions, 15 deletions
diff --git a/numpy/doc/subclassing.py b/numpy/doc/subclassing.py
index c42d5e330..7877432b5 100644
--- a/numpy/doc/subclassing.py
+++ b/numpy/doc/subclassing.py
@@ -431,20 +431,21 @@ The signature of ``__array_ufunc__`` is::
def __array_ufunc__(ufunc, method, *inputs, **kwargs):
- - *ufunc* is the ufunc object that was called.
- - *method* is a string indicating which Ufunc method was called
- (one of ``"__call__"``, ``"reduce"``, ``"reduceat"``,
- ``"accumulate"``, ``"outer"``, ``"inner"``).
- - *inputs* is a tuple of the input arguments to the ``ufunc``.
- - *kwargs* is a dictionary containing the optional input arguments
- of the ufunc. If given, any ``out`` arguments, both positional
- and keyword, are passed as a :obj:`tuple` in *kwargs*.
+ - *ufunc* is the ufunc object that was called.
+ - *method* is a string indicating how the Ufunc was called, either
+ ``"__call__"`` to indicate it was called directly, or one of its
+ :ref:`methods<ufuncs.methods>`: ``"reduce"``, ``"accumulate"``,
+ ``"reduceat"``, ``"outer"``, or ``"at"``.
+ - *inputs* is a tuple of the input arguments to the ``ufunc``
+ - *kwargs* contains any optional or keyword arguments passed to the
+ function. This includes any ``out`` arguments, which are always
+ contained in a tuple.
A typical implementation would convert any inputs or ouputs that are
instances of one's own class, pass everything on to a superclass using
``super()``, and finally return the results after possible
back-conversion. An example, taken from the test case
-``test_ufunc_override_with_super`` in ``core/tests/test_umath.pu``, is the
+``test_ufunc_override_with_super`` in ``core/tests/test_umath.py``, is the
following.
.. testcode::
@@ -462,9 +463,9 @@ following.
else:
args.append(input_)
- outputs = kwargs.pop('out', [])
+ outputs = kwargs.pop('out', None)
out_no = []
- if outputs:
+ if outputs is not None:
out_args = []
for j, output in enumerate(outputs):
if isinstance(output, A):
@@ -474,9 +475,11 @@ following.
out_args.append(output)
kwargs['out'] = tuple(out_args)
- info = {key: no for (key, no) in (('inputs', in_no),
- ('outputs', out_no))
- if no != []}
+ info = {}
+ if in_no:
+ info['inputs'] = in_no
+ if out_no:
+ info['outputs'] = out_no
results = super(A, self).__array_ufunc__(ufunc, method,
*args, **kwargs)
@@ -488,7 +491,7 @@ following.
return results
results = (results,)
- if outputs == []:
+ if outputs is None:
outputs = [None] * len(results)
results = tuple(result.view(A) if output is None else output
for result, output in zip(results, outputs))