[Numpy-discussion] Equality not working as expected with ndarray sub-class

2013-07-04 Thread Thomas Robitaille
Hi everyone,

The following example:

import numpy as np

class SimpleArray(np.ndarray):

__array_priority__ = 1

def __new__(cls, input_array, info=None):
return np.asarray(input_array).view(cls)

def __eq__(self, other):
return False

a = SimpleArray(10)
print (np.int64(10) == a)
print (a == np.int64(10))

gives the following output

$ python2.7 eq.py
True
False

so that in the first case, SimpleArray.__eq__ is not called. Is this a
bug, and if so, can anyone think of a workaround? If this is expected
behavior, how do I ensure SimpleArray.__eq__ gets called in both
cases?

Thanks,
Tom

ps: cross-posting to stackoverflow
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Equality not working as expected with ndarray sub-class

2013-07-04 Thread Frédéric Bastien
Hi,

__array__priority wasn't checked for ==, !=, , =, , = operation. I
added it in the development version and someone else back-ported it to the
1.7.X branch.

So this will work with the next release of numpy.

I don't know of a workaround until the next release.

Fred


On Thu, Jul 4, 2013 at 9:06 AM, Thomas Robitaille 
thomas.robitai...@gmail.com wrote:

 Hi everyone,

 The following example:

 import numpy as np

 class SimpleArray(np.ndarray):

 __array_priority__ = 1

 def __new__(cls, input_array, info=None):
 return np.asarray(input_array).view(cls)

 def __eq__(self, other):
 return False

 a = SimpleArray(10)
 print (np.int64(10) == a)
 print (a == np.int64(10))

 gives the following output

 $ python2.7 eq.py
 True
 False

 so that in the first case, SimpleArray.__eq__ is not called. Is this a
 bug, and if so, can anyone think of a workaround? If this is expected
 behavior, how do I ensure SimpleArray.__eq__ gets called in both
 cases?

 Thanks,
 Tom

 ps: cross-posting to stackoverflow
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Equality not working as expected with ndarray sub-class

2013-07-04 Thread sebastian
On 2013-07-04 15:06, Thomas Robitaille wrote:
 Hi everyone,
 
 The following example:
 
 import numpy as np
 
 class SimpleArray(np.ndarray):
 
 __array_priority__ = 1
 
 def __new__(cls, input_array, info=None):
 return np.asarray(input_array).view(cls)
 
 def __eq__(self, other):
 return False
 
 a = SimpleArray(10)
 print (np.int64(10) == a)
 print (a == np.int64(10))
 
 gives the following output
 
 $ python2.7 eq.py
 True
 False
 
 so that in the first case, SimpleArray.__eq__ is not called. Is this a
 bug, and if so, can anyone think of a workaround? If this is expected
 behavior, how do I ensure SimpleArray.__eq__ gets called in both
 cases?
 

This should be working in all development versions. I.e. NumPy 1.7.2 
(which is not released yet).

- Sebastian

 Thanks,
 Tom
 
 ps: cross-posting to stackoverflow
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Equality not working as expected with ndarray sub-class

2013-07-04 Thread Frédéric Bastien
On Thu, Jul 4, 2013 at 9:12 AM, sebastian sebast...@sipsolutions.netwrote:

 On 2013-07-04 15:06, Thomas Robitaille wrote:
  Hi everyone,
 
  The following example:
 
  import numpy as np
 
  class SimpleArray(np.ndarray):
 
  __array_priority__ = 1
 
  def __new__(cls, input_array, info=None):
  return np.asarray(input_array).view(cls)
 
  def __eq__(self, other):
  return False
 
  a = SimpleArray(10)
  print (np.int64(10) == a)
  print (a == np.int64(10))
 
  gives the following output
 
  $ python2.7 eq.py
  True
  False
 
  so that in the first case, SimpleArray.__eq__ is not called. Is this a
  bug, and if so, can anyone think of a workaround? If this is expected
  behavior, how do I ensure SimpleArray.__eq__ gets called in both
  cases?
 

 This should be working in all development versions. I.e. NumPy 1.7.2
 (which is not released yet).


I think you mean: NumPy = 1.7.2

Fred
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion