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

Mr. Joe titanix88 at gmail.com
Sun May 12 19:23:16 EDT 2013


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 -
"""
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function

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

    def __eq__(self, other):
        return self.val == other.val

x = DemoClass('a')
y = DemoClass('a')

print("x == y: {0}".format(x == y))
print("x != y: {0}".format(x != y))
print("not x == y: {0}".format(not x == y))
"""

In python3, the output is as expected:
"""
x == y: True
x != y: False
not x == y: False
"""

In python2.7.3, the output is:
"""
x == y: True
x != y: True
not x == y: False
"""
Which is not correct!!

Thanks in advance for clarifications.
Regards,
TB



More information about the Python-list mailing list