Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45212 > unrolled thread

Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ]

Started byChristian Heimes <christian@python.org>
First post2013-05-13 01:39 +0200
Last post2013-05-13 01:39 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ] Christian Heimes <christian@python.org> - 2013-05-13 01:39 +0200

#45212 — Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ]

FromChristian Heimes <christian@python.org>
Date2013-05-13 01:39 +0200
SubjectRe: Differences of "!=" operator behavior in python3 and python2 [ bug? ]
Message-ID<mailman.1605.1368401958.3114.python-list@python.org>
Am 13.05.2013 01:23, schrieb Mr. Joe:
> I seem to stumble upon a situation where "!=" operator misbehaves in
> python2.x. Not sure if it's my misunderstanding or a bug in python
> implementation. Here's a demo code to reproduce the behavior -
> """

Python 2.7 doesn't use the negation of __eq__ when your class doesn't
provide a __ne__ function. Just add a print() to your __eq__ method and
you'll notice the different.

You have to provide both:


class DemoClass(object):
    def __init__(self, val):
        self.val = val

    def __eq__(self, other):
        if not isinstance(other, DemoClass):
            return NotImplemented
        return self.val == other.val

    def __ne__(self, other):
        if not isinstance(other, DemoClass):
            return NotImplemented
        return self.val != other.val

or

    def __ne__(self, other):
        result = self.__eq__(other)
        if result is NotImplemented:
             return NotImplemented
        return not result


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web