{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Mixin defining all operator special methods using __array_ufunc__."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This class implements the special methods for almost all of Python's builtin operators defined in the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "operator",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "operator",
                "version": "*",
                "kind": "api",
                "path": "operator"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " module, including comparisons ("
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "=="
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": ">"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", etc.) and arithmetic ("
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "+"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "*"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "-"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", etc.), by deferring to the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "__array_ufunc__"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " method, which subclasses must implement."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "It is useful for writing classes that do not inherit from "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "numpy.ndarray",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "module",
                "path": "numpy:ndarray"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", but that should support arithmetic and numpy universal functions like arrays as described in "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "nep-0013-ufunc-overrides",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "docs",
                "path": "nep-0013-ufunc-overrides"
              },
              "kind": "docs"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "As a trivial example, consider this implementation of an "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ArrayLike"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " class that simply wraps a NumPy array and ensures that the result of any arithmetic operation is also an "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ArrayLike"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " object:"
            }
          ]
        },
        {
          "__type": "Blockquote",
          "__tag": 4059,
          "children": [
            {
              "__type": "Code",
              "__tag": 4050,
              "value": ">>> import numbers\n>>> class ArrayLike(np.lib.mixins.NDArrayOperatorsMixin):\n...     def __init__(self, value):\n...         self.value = np.asarray(value)\n...\n...     # One might also consider adding the built-in list type to this\n...     # list, to support operations like np.add(array_like, list)\n...     _HANDLED_TYPES = (np.ndarray, numbers.Number)\n...\n...     def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):\n...         out = kwargs.get('out', ())\n...         for x in inputs + out:\n...             # Only support operations with instances of\n...             # _HANDLED_TYPES. Use ArrayLike instead of type(self)\n...             # for isinstance to allow subclasses that don't\n...             # override __array_ufunc__ to handle ArrayLike objects.\n...             if not isinstance(\n...                 x, self._HANDLED_TYPES + (ArrayLike,)\n...             ):\n...                 return NotImplemented\n...\n...         # Defer to the implementation of the ufunc\n...         # on unwrapped values.\n...         inputs = tuple(x.value if isinstance(x, ArrayLike) else x\n...                     for x in inputs)\n...         if out:\n...             kwargs['out'] = tuple(\n...                 x.value if isinstance(x, ArrayLike) else x\n...                 for x in out)\n...         result = getattr(ufunc, method)(*inputs, **kwargs)\n...\n...         if type(result) is tuple:\n...             # multiple return values\n...             return tuple(type(self)(x) for x in result)\n...         elif method == 'at':\n...             # no return value\n...             return None\n...         else:\n...             # one return value\n...             return type(self)(result)\n...\n...     def __repr__(self):\n...         return '%s(%r)' % (type(self).__name__, self.value)",
              "execution_status": null
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "In interactions between "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ArrayLike"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " objects and numbers or numpy arrays, the result is always another "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ArrayLike"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ":"
            }
          ]
        },
        {
          "__type": "Blockquote",
          "__tag": 4059,
          "children": [
            {
              "__type": "Code",
              "__tag": 4050,
              "value": ">>> x = ArrayLike([1, 2, 3])\n>>> x - 1\nArrayLike(array([0, 1, 2]))\n>>> 1 - x\nArrayLike(array([ 0, -1, -2]))\n>>> np.arange(3) - x\nArrayLike(array([-1, -1, -1]))\n>>> x - np.arange(3)\nArrayLike(array([1, 1, 1]))",
              "execution_status": null
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Note that unlike "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "numpy.ndarray"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ArrayLike"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " does not allow operations with arbitrary, unrecognized types. This ensures that interactions with ArrayLike preserve a well-defined casting hierarchy."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/numpy/lib/mixins.py",
  "item_line": 60,
  "item_type": "class",
  "aliases": [
    "numpy.lib.mixins.NDArrayOperatorsMixin"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "NDArrayOperatorsMixin"
  },
  "references": null,
  "qa": "numpy.lib.mixins:NDArrayOperatorsMixin",
  "arbitrary": [],
  "local_refs": []
}