[Numpy-discussion] Scalar-ndarray arguments passed to not_equal
Keith Goodman
kwgoodman@gmail....
Thu Feb 11 15:16:39 CST 2010
On Thu, Feb 11, 2010 at 12:47 PM, Robert Kern <robert.kern@gmail.com> wrote:
> 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])
Yes, much cleaner.
The problem I have now is that I don't know where to place the line of
code that changes the meaning of numpy's equal. I don't know when
someone will do
numpy array == myarray
The execution of that line goes to Numpy, not my class. And I don't
want to overload Numpy's comparison operators in my entire module.
More information about the NumPy-Discussion
mailing list