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

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue May 14 01:21:47 EDT 2013


On Mon, 13 May 2013 21:17:41 +0000, Alister 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.

I think that is sad. If I read "not (x == y)" I would assume that the 
person writing the code didn't understand Python syntax or know its 
standard operators.

The exception is if x or y was some unusual type where the __ne__ method 
does something different to the logical reverse of the __eq__ method. But 
if that is the case, I would expect a comment warning about it. If x and 
y were standard objects like floats, strings, etc. then writing out 
not (x == y) in full is as sensible as writing out these in full:

x + -y  # instead of x - y

alist.insert(len(alist), 99)  # instead of alist.append(99)

# instead of adict.clear()
for key in list(adict.keys()):
    del adict[key]

x+x+x+x+x+x  # instead of x*6


I'm sure any experienced Python programmer can come up with a 
hypothetical scenario where you would need to write things out "the long 
way", but that doesn't mean that we should do so all the time.


> 2 lines from the zen stand out here:-
> 
> Explicit is better than implicit.
> in the face of ambiguity refuse the temptation to guess.

!= is explicit.

There is no ambiguity that needs to be guessed.



-- 
Steven



More information about the Python-list mailing list