__eq__ and __ne__

Discussion in 'Python' started by Shane Hathaway, Jul 8, 2003.

  1. I was surprised by the following behavior. Apparently, the "!="
    operator does not fall back to using "not __eq__()". I tested this with
    Python 2.1, 2.2, and 2.2 with new-style classes and got the same results
    in every case.

    >>> class foo:

    .... def __eq__(self, other):
    .... return (other.__class__ is self.__class__
    .... and other.__dict__ == self.__dict__)
    ....
    >>> foo() == foo()

    1
    >>> foo() != foo()

    1

    I would expect the second test to yield "0". To compensate, I've
    started adding the following boilerplate code to all classes that define
    only __eq__:

    def __ne__(self, other):
    return not self.__eq__(other)

    Does this surprise anyone else? I have not yet found documentation on this.

    Shane
     
    Shane Hathaway, Jul 8, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Peters

    RE: __eq__ and __ne__

    Tim Peters, Jul 8, 2003, in forum: Python
    Replies:
    0
    Views:
    418
    Tim Peters
    Jul 8, 2003
  2. Tim Peters

    RE: __eq__ and __ne__

    Tim Peters, Jul 16, 2003, in forum: Python
    Replies:
    0
    Views:
    410
    Tim Peters
    Jul 16, 2003
  3. Aaron Brady

    hash and __eq__

    Aaron Brady, May 30, 2009, in forum: Python
    Replies:
    14
    Views:
    688
    Albert van der Horst
    Jun 4, 2009
  4. INADA Naoki

    Is __ne__ method autogenerated?

    INADA Naoki, Dec 7, 2012, in forum: Python
    Replies:
    3
    Views:
    154
    INADA Naoki
    Dec 7, 2012
  5. Tim Delaney
    Replies:
    0
    Views:
    111
    Tim Delaney
    Feb 18, 2013
Loading...

Share This Page