top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+1 vote
624 views

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 -

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!!

posted May 13, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

In Python 3, if __ne__ isn't defined, "!=" will call __eq__ and negate the result.

In Python 2, "!=" only calls __ne__. Since you don't have one defined, it's using the built-in object comparison, and since x and y are different objects, they are not equal to each other, so x != y is True.

answer May 13, 2013 by anonymous
+1 vote

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
answer May 13, 2013 by anonymous
+1 vote

The != operator is implemented by the __ne__ special method. In Python 3, the default implementation of __ne__ is to call __eq__ and return the opposite of whatever it returns. In Python 2, __ne__ calls the older __cmp__ method instead, which is no longer meaningful in Python 3.

answer May 13, 2013 by anonymous
Similar Questions
0 votes

I'm starting a new project from scratch so I think its finally a time to switch to the latest and greatest Python 3.4.

But I'm puzzled with MySQL support for Python 3. So far the only stable library I've found it pymysql.

All others are either abandoned work-in-progress projects or do not support Python 3:

  • mysqldb - Python 2.x only
  • mysql-ctypes - Python 2.x only
  • amysql - Python 2.x only
  • ultramysql - Python 2.x only
  • MySQL Connector/Python - new guy in block. Does anyone use it?
  • WebScaleSQL + MySQLdb1 - still in development, state unknown?

So what library do you use for MySQL access in Python 3? I'm specifically interested in async support (like in psycopg2 for PostgreSQL) since I'm planning to use Tornado.

0 votes

I need a command that will make threads created by "multiprocessing.Process()" wait for each other to complete. For instance, I want to do something like this:

job1 = multiprocessing.Process(CMD1())
job2 = multiprocessing.Process(CMD2())

jobs1.start(); jobs2.start()

PY_FUNC()

The command "PY_FUNC()" depends on the end result of the actions of CMD1() and CMD2(). I need some kind of wait command for the two threads that will not let the script continue until job1 and job2 are complete. Is this possible in Python3?

+2 votes

What's the problem with Python 3.x? It was first released in 2008, but web hosting companies still seem to offer Python 2.x rather.

For example, Google App Engine only offers Python 2.7. What's wrong?...

0 votes

I have read that asyncio is a great addition to Python 3. I have looked around and saw the related PEP which is quite big BTW but couldn't find a simple explanation for why this is such a great addition. Any simple example where it can be used?

It can be used to have a queue of tasks? Like threads? Maybe light weight threads? Those were my thoughts but the library reference clearly stated that this is single-threaded. So there should be some waiting time in between the tasks. Then what is good?

These are just jumbled thoughts that came into my mind while trying to make sense of usefulness of asyncio. Anyone can give a better idea?

...