Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45213 > unrolled thread

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

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2013-05-12 17:40 -0600
Last post2013-05-13 00:52 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ] Ian Kelly <ian.g.kelly@gmail.com> - 2013-05-12 17:40 -0600
    Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ] Rotwang <sg552@hotmail.co.uk> - 2013-05-13 00:52 +0100

#45213 — Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ]

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-05-12 17:40 -0600
SubjectRe: Differences of "!=" operator behavior in python3 and python2 [ bug? ]
Message-ID<mailman.1606.1368402067.3114.python-list@python.org>
On Sun, May 12, 2013 at 5:23 PM, Mr. Joe <titanix88@gmail.com> 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 -

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.

[toc] | [next] | [standalone]


#45214

FromRotwang <sg552@hotmail.co.uk>
Date2013-05-13 00:52 +0100
Message-ID<kmp9oi$6g1$1@dont-email.me>
In reply to#45213
On 13/05/2013 00:40, Ian Kelly wrote:
> On Sun, May 12, 2013 at 5:23 PM, Mr. Joe <titanix88@gmail.com> 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 -
>
> 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.

One should be aware, however, that this doesn't necessarily apply to 
classes inheriting from builtins other than object (a fact that recently 
bit me on the a***):

 >>> class spam:
	def __eq__(self, other):
		print('spam')
		return super().__eq__(other)

	
 >>> class eggs(list):
	def __eq__(self, other):
		print('eggs')
		return super().__eq__(other)

	
 >>> spam() != spam()
spam
spam
True
 >>> eggs() != eggs()
False

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web