Annoying behaviour of the != operator

Robert Kern rkern at ucsd.edu
Wed Jun 8 15:02:15 EDT 2005


Jordan Rastrick wrote:

> Are there any other reasonable examples people can give where it makes
> sense for != and == not to be each other's complement?

__eq__ and __ne__ implement *rich* comparisons. They don't have to 
return only True or False.

In [1]:import Numeric

In [2]:a = Numeric.array([1, 2, 3, 4, 5])

In [3]:b = Numeric.array([1, 0, 4, 0, 5])

In [4]:a == b
Out[4]:array([1, 0, 0, 0, 1],'b')

In [5]:a != b
Out[5]:array([0, 1, 1, 1, 0],'b')

In [6]:(a != b) == (not (a == b))
Out[6]:array([1, 0, 0, 0, 1],'b')

In [7]:not (a == b)
Out[7]:False

In [8]:not (a != b)
Out[8]:False

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list