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

Alister alister.ware at ntlworld.com
Mon May 13 05:59:03 EDT 2013


On Mon, 13 May 2013 05:23:16 +0600, Mr. Joe wrote:

> 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

this looks to me like an issue with operator precidence

you code is evaluating as (Not x) == y
rather than not (x == y)

why the difference between 2.7 & 3.X is beyond my knowledge of the 
language

The other explanations seem to detail things at low level but it is all 
Greek to me 


-- 
Nice guys finish last.
		-- Leo Durocher



More information about the Python-list mailing list