python 'is not' operator

Python

Python Problem Overview


I notice there is a comparison operator is not. Should I literally translate it into

!= 

instead of

== not

Python Solutions


Solution 1 - Python

To expand on what Ignacio said:

a == b and a != b test whether two objects have the same value. You can override an object's __eq__ and __ne__ methods to determine what that means.

a is b and a is not b test whether two objects are the same thing. It's like doing id(a) == id(b)

Solution 2 - Python

It's not relational comparison, it's identity. And it translates to not (A is B).

Solution 3 - Python

python 2.7.3 documentation, 5.9. Comparisons:

> The operators <, >, ==, >=, <=, and != compare the values of two objects.

and about operator is in the same chapter:

>The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

Solution 4 - Python

A != B

means that "A is not equal to B", not "A is equal to not B".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnosView Question on Stackoverflow
Solution 1 - PythonThomas KView Answer on Stackoverflow
Solution 2 - PythonIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 3 - Python2r2wView Answer on Stackoverflow
Solution 4 - PythonzsalzbankView Answer on Stackoverflow