Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=python.org; s=200901; t=1368401956; bh=dS9v/YKhqrYoeyvdKT6aJyg6gLuO35gsygCeV9YhBDo=; h=To:From:Subject:Date:Message-ID:References:Mime-Version: Content-Type:Content-Transfer-Encoding:In-Reply-To; b=HSczk8I2pQSX2QeGBzwgm4fgkIPUE+pHK9lnUCajIiqU775h6oK05tdCSaUN/c6Br Wm1ufhMOznXfRkm2Tjr0816eGlOo7c3JqQ4gI39ANY84zr201idt3mQTAYHcqW9kx5 uV14gYCIkLxAQg28sgdSvCbBVsfFKZzhiMbh8d7k= X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; '"""': 0.07; 'subject:bug': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'val': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; '2.7': 0.14; '__ne__': 0.16; '__ne__(self,': 0.16; 'from:name:christian heimes': 0.16; 'negation': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reproduce': 0.16; 'subject:python3': 0.16; 'from:addr:python.org': 0.16; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'code': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'add': 0.35; 'method': 0.36; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; "you'll": 0.62; 'skip:n 10': 0.64; 'provide': 0.64; 'situation': 0.65; 'behavior': 0.77; 'different.': 0.84; 'mr.': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Christian Heimes Subject: Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ] Date: Mon, 13 May 2013 01:39:03 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: f049070034.adsl.alicedsl.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 In-Reply-To: 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368401958 news.xs4all.nl 15950 [2001:888:2000:d::a6]:59504 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45212 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