Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Differences of "!=" operator behavior in python3 and python2 [ bug? ]

Reply
Thread Tools

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

 
 
Mr. Joe
Guest
Posts: n/a
 
      05-12-2013
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
 
Reply With Quote
 
 
 
 
Fábio Santos
Guest
Posts: n/a
 
      05-13-2013
On 13 May 2013 11:04, "Alister" <(E-Mail Removed)> wrote:
> this looks to me like an issue with operator precidence
>
> you code is evaluating as (Not x) == y
> rather than not (x == y)


I can say for sure that the precedence is as expected. I always use "not
.... == ..." Instead of !=.

 
Reply With Quote
 
 
 
 
Ned Batchelder
Guest
Posts: n/a
 
      05-13-2013

On 5/13/2013 1:26 PM, Fábio Santos wrote:
>
>
> On 13 May 2013 11:04, "Alister" <(E-Mail Removed)
> <(E-Mail Removed)>> wrote:
> > this looks to me like an issue with operator precidence
> >
> > you code is evaluating as (Not x) == y
> > rather than not (x == y)

>
> I can say for sure that the precedence is as expected. I always use
> "not ... == ..." Instead of !=.
>
>


If you don't mind my asking, why do you do that?

--Ned.


 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      05-13-2013
On Tue, May 14, 2013 at 4:08 AM, Ned Batchelder <(E-Mail Removed)> wrote:
>
> On 5/13/2013 1:26 PM, Fábio Santos wrote:
>
>
> On 13 May 2013 11:04, "Alister" <(E-Mail Removed)> wrote:
>> this looks to me like an issue with operator precidence
>>
>> you code is evaluating as (Not x) == y
>> rather than not (x == y)

>
> I can say for sure that the precedence is as expected. I always use "not ....
> == ..." Instead of !=.
>
>
>
> If you don't mind my asking, why do you do that?


I think it's fairly obvious. Like the stumpy-tailed dog from "The
Loaded Dog" [1], he's saving up the != operator in case he needs it
later.

ChrisA

[1] Full text here. http://jendi.bowmeow.com.au/loadeddog1.html
 
Reply With Quote
 
Fábio Santos
Guest
Posts: n/a
 
      05-13-2013
I think it is more readable. When doing more complicated statements I use
!= instead, but when it's a single test I prefer not … ==

It's a personal thing. It may also have to do with the fact that I didn't
know python had != when I was a novice.
On 13 May 2013 19:08, "Ned Batchelder" <(E-Mail Removed)> wrote:

>
> On 5/13/2013 1:26 PM, Fábio Santos wrote:
>
>
> On 13 May 2013 11:04, "Alister" <(E-Mail Removed)> wrote:
> > this looks to me like an issue with operator precidence
> >
> > you code is evaluating as (Not x) == y
> > rather than not (x == y)

>
> I can say for sure that the precedence is as expected. I always use "not
> ... == ..." Instead of !=.
>
>
> If you don't mind my asking, why do you do that?
>
> --Ned.
>
>


 
Reply With Quote
 
Fábio Santos
Guest
Posts: n/a
 
      05-13-2013
On Mon, May 13, 2013 at 10:17 PM, Alister <(E-Mail Removed)> wrote:
> On Mon, 13 May 2013 19:28:29 +0100, Fábio Santos wrote:
>
>> I think it is more readable. When doing more complicated statements I
>> use != instead, but when it's a single test I prefer not … ==
>>
>> It's a personal thing. It may also have to do with the fact that I
>> didn't know python had != when I was a novice.
>> On 13 May 2013 19:08, "Ned Batchelder" <(E-Mail Removed)> wrote:
>>

>
> I would then still write it as not (x == y) to make it clear to myself &
> avoid any possible confusion although I think that X != Y is much
> cleaner.
> 2 lines from the zen stand out here:-
>
> Explicit is better than implicit.
> in the face of ambiguity refuse the temptation to guess.
>


And here I was, thinking I was being pythonic. I hope other people
using my code will be able to understand it well, not just myself, so
using the most common idioms should be the best way to go.


--
Fábio Santos
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      05-13-2013
On 13/05/2013 22:17, Alister wrote:
> On Mon, 13 May 2013 19:28:29 +0100, Fábio Santos wrote:
>
>> I think it is more readable. When doing more complicated statements I
>> use != instead, but when it's a single test I prefer not … ==
>>
>> It's a personal thing. It may also have to do with the fact that I
>> didn't know python had != when I was a novice.
>> On 13 May 2013 19:08, "Ned Batchelder" <(E-Mail Removed)> wrote:
>>

>
> I would then still write it as not (x == y) to make it clear to myself &
> avoid any possible confusion although I think that X != Y is much
> cleaner.
> 2 lines from the zen stand out here:-
>
> Explicit is better than implicit.
> in the face of ambiguity refuse the temptation to guess.
>
> there are many features of Python (& other languages) i did not now when
> I started but have adopted once I understood what they were & how they
> worked. then again use what you are most comfortable with.
>
> Practicality beats purity
>


I much prefer the alternative <> for != but some silly people insisted
that this be removed from Python3. Just how stupid can you get?

--
If you're using GoogleCrapâ„¢ please read this
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      05-13-2013
On 05/13/2013 06:53 PM, Mark Lawrence wrote:
> On 13/05/2013 22:17, Alister wrote:
>> On Mon, 13 May 2013 19:28:29 +0100, Fábio Santos wrote:
>>
>>> I think it is more readable. When doing more complicated statements I
>>> use != instead, but when it's a single test I prefer not … ==
>>>
>>> It's a personal thing. It may also have to do with the fact that I
>>> didn't know python had != when I was a novice.
>>> On 13 May 2013 19:08, "Ned Batchelder" <(E-Mail Removed)> wrote:
>>>

>>
>> I would then still write it as not (x == y) to make it clear to myself &
>> avoid any possible confusion although I think that X != Y is much
>> cleaner.
>> 2 lines from the zen stand out here:-
>>
>> Explicit is better than implicit.
>> in the face of ambiguity refuse the temptation to guess.
>>
>> there are many features of Python (& other languages) i did not now when
>> I started but have adopted once I understood what they were & how they
>> worked. then again use what you are most comfortable with.
>>
>> Practicality beats purity
>>

>
> I much prefer the alternative <> for != but some silly people insisted
> that this be removed from Python3. Just how stupid can you get?
>


So which special methods should the <> operator call? By rights it
ought to call both __gt__ and __lt__ and return True if either of them
is True.


--
DaveA
 
Reply With Quote
 
Andrew Berg
Guest
Posts: n/a
 
      05-13-2013
On 2013.05.13 17:53, Mark Lawrence wrote:
> I much prefer the alternative <> for != but some silly people insisted
> that this be removed from Python3.

It's not removed from Python 3, though:

Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:30:21) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import barry_as_FLUFL
>>> 3 <> 2

True

--
CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      05-13-2013
On Mon, May 13, 2013 at 5:27 PM, Andrew Berg <(E-Mail Removed)> wrote:
> On 2013.05.13 17:53, Mark Lawrence wrote:
>> I much prefer the alternative <> for != but some silly people insisted
>> that this be removed from Python3.

> It's not removed from Python 3, though:
>
> Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:30:21) [MSC v.1600 64 bit (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from __future__ import barry_as_FLUFL
>>>> 3 <> 2

> True


I'd like to see â‰* added as a synonym for !=, even if nobody uses it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: is the same betweent python3 and python3.2? Andrew Berg Python 0 06-16-2012 11:11 AM
running Python2 Python3 parallel concurrent harrismh777 Python 3 04-05-2011 06:22 AM
Python3.0 has more duplication in source code than Python2.5 Terry Python 20 02-08-2009 11:21 PM
Re: install python2.4 on FreeBSD and keep using python2.3 Xiao Jianfeng Python 1 11-18-2005 04:34 AM
install python2.4 on FreeBSD and keep using python2.3 Ksenia Marasanova Python 2 11-17-2005 06:53 PM



Advertisments