diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-04-27 22:39:09 +0100 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-04-27 22:40:32 +0100 |
commit | e8c387cb8a105b9209474b6abf5c8feb0e92d830 (patch) | |
tree | c512022d29da804d566310fc01d63bc524ab1755 /numpy/lib/mixins.py | |
parent | e332ba4314bf874b1c7d17d82a2f1bf1766ece69 (diff) | |
download | numpy-e8c387cb8a105b9209474b6abf5c8feb0e92d830.tar.gz |
MAINT: Set the __name__ of generated methods
Barely useful, but means that the function name is helpful if printed.
Diffstat (limited to 'numpy/lib/mixins.py')
-rw-r--r-- | numpy/lib/mixins.py | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index 21e4b346f..b5231e372 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -17,42 +17,46 @@ def _disables_array_ufunc(obj): return False -def _binary_method(ufunc): +def _binary_method(ufunc, name): """Implement a forward binary method with a ufunc, e.g., __add__.""" def func(self, other): if _disables_array_ufunc(other): return NotImplemented return ufunc(self, other) + func.__name__ = '__{}__'.format(name) return func -def _reflected_binary_method(ufunc): +def _reflected_binary_method(ufunc, name): """Implement a reflected binary method with a ufunc, e.g., __radd__.""" def func(self, other): if _disables_array_ufunc(other): return NotImplemented return ufunc(other, self) + func.__name__ = '__r{}__'.format(name) return func -def _inplace_binary_method(ufunc): +def _inplace_binary_method(ufunc, name): """Implement an in-place binary method with a ufunc, e.g., __iadd__.""" def func(self, other): return ufunc(self, other, out=(self,)) + func.__name__ = '__i{}__'.format(name) return func -def _numeric_methods(ufunc): +def _numeric_methods(ufunc, name): """Implement forward, reflected and inplace binary methods with a ufunc.""" - return (_binary_method(ufunc), - _reflected_binary_method(ufunc), - _inplace_binary_method(ufunc)) + return (_binary_method(ufunc, name), + _reflected_binary_method(ufunc, name), + _inplace_binary_method(ufunc, name)) -def _unary_method(ufunc): +def _unary_method(ufunc, name): """Implement a unary special method with a ufunc.""" def func(self): return ufunc(self) + func.__name__ = '__{}__'.format(name) return func @@ -139,33 +143,36 @@ class NDArrayOperatorsMixin(object): # overrides NEP. # comparisons don't have reflected and in-place versions - __lt__ = _binary_method(um.less) - __le__ = _binary_method(um.less_equal) - __eq__ = _binary_method(um.equal) - __ne__ = _binary_method(um.not_equal) - __gt__ = _binary_method(um.greater) - __ge__ = _binary_method(um.greater_equal) + __lt__ = _binary_method(um.less, 'lt') + __le__ = _binary_method(um.less_equal, 'le') + __eq__ = _binary_method(um.equal, 'eq') + __ne__ = _binary_method(um.not_equal, 'ne') + __gt__ = _binary_method(um.greater, 'gt') + __ge__ = _binary_method(um.greater_equal, 'ge') # numeric methods - __add__, __radd__, __iadd__ = _numeric_methods(um.add) - __sub__, __rsub__, __isub__ = _numeric_methods(um.subtract) - __mul__, __rmul__, __imul__ = _numeric_methods(um.multiply) + __add__, __radd__, __iadd__ = _numeric_methods(um.add, 'add') + __sub__, __rsub__, __isub__ = _numeric_methods(um.subtract, 'sub') + __mul__, __rmul__, __imul__ = _numeric_methods(um.multiply, 'mul') if sys.version_info.major < 3: # Python 3 uses only __truediv__ and __floordiv__ - __div__, __rdiv__, __idiv__ = _numeric_methods(um.divide) - __truediv__, __rtruediv__, __itruediv__ = _numeric_methods(um.true_divide) + __div__, __rdiv__, __idiv__ = _numeric_methods(um.divide, 'div') + __truediv__, __rtruediv__, __itruediv__ = _numeric_methods( + um.true_divide, 'truediv') __floordiv__, __rfloordiv__, __ifloordiv__ = _numeric_methods( - um.floor_divide) - __mod__, __rmod__, __imod__ = _numeric_methods(um.mod) + um.floor_divide, 'floordiv') + __mod__, __rmod__, __imod__ = _numeric_methods(um.mod, 'mod') # TODO: handle the optional third argument for __pow__? - __pow__, __rpow__, __ipow__ = _numeric_methods(um.power) - __lshift__, __rlshift__, __ilshift__ = _numeric_methods(um.left_shift) - __rshift__, __rrshift__, __irshift__ = _numeric_methods(um.right_shift) - __and__, __rand__, __iand__ = _numeric_methods(um.bitwise_and) - __xor__, __rxor__, __ixor__ = _numeric_methods(um.bitwise_xor) - __or__, __ror__, __ior__ = _numeric_methods(um.bitwise_or) + __pow__, __rpow__, __ipow__ = _numeric_methods(um.power, 'pow') + __lshift__, __rlshift__, __ilshift__ = _numeric_methods( + um.left_shift, 'lshift') + __rshift__, __rrshift__, __irshift__ = _numeric_methods( + um.right_shift, 'rshift') + __and__, __rand__, __iand__ = _numeric_methods(um.bitwise_and, 'and') + __xor__, __rxor__, __ixor__ = _numeric_methods(um.bitwise_xor, 'xor') + __or__, __ror__, __ior__ = _numeric_methods(um.bitwise_or, 'or') # unary methods - __neg__ = _unary_method(um.negative) - __abs__ = _unary_method(um.absolute) - __invert__ = _unary_method(um.invert) + __neg__ = _unary_method(um.negative, 'neg') + __abs__ = _unary_method(um.absolute, 'abs') + __invert__ = _unary_method(um.invert, 'invert') |