RE: __eq__ and __ne__

Discussion in 'Python' started by Tim Peters, Jul 16, 2003.

  1. Tim Peters

    Tim Peters Guest

    [Tim]
    >>>> Since the richcmp operators aren't constrained to return scalars,
    >>>> an attempt to define one in terms of the others seemed futile.
    >>>> Instead they're 6 independent methods.


    [Shane Hathaway]
    >>> Ok. Thanks for finding the docs. I guess I'll stick to my
    >>> boilerplate code. I think some guidance should be added to the
    >>> Python docs, though, explaining that whenever you define __eq__,
    >>> you very likely ought to define __ne__ also.


    Doc patches are always welcome <wink>.

    [Jeremy Hylton]
    >> I believe C++ has the same behavior when you overload logical
    >> operators. So at least people familiar with the same behavior in
    >> other languages won't be surprised.


    [Shane]
    > Actually, C++ generates a compiler error if you try to do that. (See
    > the attached program.) Java doesn't run into this because it has a
    > simpler interface: the only operator you can override is equals(). So
    > in this case, C++ is fully explicit and Java is fully implicit, while
    > Python makes a guess. If we can't make it implicit like Java, then it
    > seems preferable for Python to raise an exception, similar to C++,
    > when you use the != operator with an instance that implements __eq__
    > but not __ne__.


    With a clean slate, I'd agree. If Python 3 supports *only* richcmps, I
    expect that's what Guido will do there. The current comparison mish-mash is
    at least half backward-compatibility warts. This is something that needs to
    be simplified rather than complicated (it's already too complicated to
    understand in all cases!), but simplification probably can't be done anymore
    without breaking somebody's code.
     
    Tim Peters, Jul 16, 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. Shane Hathaway

    __eq__ and __ne__

    Shane Hathaway, Jul 8, 2003, in forum: Python
    Replies:
    0
    Views:
    373
    Shane Hathaway
    Jul 8, 2003
  2. Tim Peters

    RE: __eq__ and __ne__

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

    hash and __eq__

    Aaron Brady, May 30, 2009, in forum: Python
    Replies:
    14
    Views:
    665
    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:
    138
    INADA Naoki
    Dec 7, 2012
  5. Tim Delaney
    Replies:
    0
    Views:
    106
    Tim Delaney
    Feb 18, 2013
Loading...

Share This Page