Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'output': 0.05; '"""': 0.07; '-*-': 0.07; 'subject:bug': 0.07; 'utf-8': 0.07; 'advance': 0.07; 'coding:': 0.09; 'objects,': 0.09; 'val': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'bug': 0.12; '__future__': 0.16; '__ne__': 0.16; 'defined,': 0.16; 'other,': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'reproduce': 0.16; 'subject:python3': 0.16; 'wrote:': 0.18; 'result.': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'joe': 0.30; 'code': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'equal': 0.35; 'false': 0.36; 'thanks': 0.36; 'pm,': 0.38; 'sure': 0.39; 'skip:u 10': 0.60; 'different': 0.65; 'situation': 0.65; 'to:addr:gmail.com': 0.65; 'behavior': 0.77; 'mr.': 0.98 Date: Sun, 12 May 2013 19:35:39 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 To: "Mr. Joe" Subject: Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ] References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368401743 news.xs4all.nl 15935 [2001:888:2000:d::a6]:57125 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45211 On 5/12/2013 7:23 PM, 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 Python 3, if __ne__ isn't defined, "!=" will call __eq__ and negate the result. > In python2.7.3, the output is: > """ > x == y: True > x != y: True > not x == y: False > """ > Which is not correct!! 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. > Thanks in advance for clarifications. > Regards, > TB