[Numpy-discussion] Scalar-ndarray arguments passed to not_equal

Robert Kern robert.kern@gmail....
Thu Feb 11 14:47:31 CST 2010


On Thu, Feb 11, 2010 at 14:40, Keith Goodman <kwgoodman@gmail.com> wrote:
> On Thu, Feb 11, 2010 at 12:32 PM, Friedrich Romstedt
> <friedrichromstedt@gmail.com> wrote:
>>> Hey! You broke my numpy  :)
>>>
>>>>> def addbug(x, y):
>>>   ...:     return x - y
>>>   ...:
>>>>> old_funcs = np.set_numeric_ops(add=addbug)
>>>>> np.array([1]) + np.array([1])
>>>   array([0])
>> Yea, that's what I meant.  Great.
>>
>> :-) :-)
>
> Who needs to type np.dot when you can do:
>
>>> def dotmult(x, y):
>   ....:     return np.dot(x, y)
>   ....:
>>> old_funcs = np.set_numeric_ops(multiply=dotmult)
>>>
>>> np.array([1, 2, 3]) * np.array([1, 2, 3])
>   14
>
> I can see many bugs coming my way...

Context managers can help:


from __future__ import with_statement

from contextlib import contextmanager

import numpy as np


@contextmanager
def numpy_ops(**ops):
    old_ops = np.set_numeric_ops(**ops)
    try:
        yield
    finally:
        np.set_numeric_ops(**old_ops)


with numpy_ops(multiply=...):
    print np.array([1, 2, 3]) * np.array([1, 2, 3])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list