Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 'is' vs JavaScript ===

Tags:

The Python use of 'is' seems to be similar to JavaScript '===' but not quite.

Here they talk about exact instances: http://www.learnpython.org/en/Conditions

here (for JS) they talk about "equal AND the same type." http://www.w3schools.com/js/js_comparisons.asp

SO can you have two different instances of (say) a string of "Bob" and have them not return true when compared using 'is'? Or is it infact the same as ===?

I am guessing this is related to strict vs non-strict typed languages. . . .

like image 889
R Claven Avatar asked Feb 14 '14 08:02

R Claven


People also ask

Does Python have a === operator?

The Equality operator (==) is a comparison operator in Python that compare values of both the operands and checks for value equality. Whereas the 'is' operator is the identity operator that checks whether both the operands refer to the same object or not (present in the same memory location).

Which is better between Python and JavaScript?

JavaScript is better for website development Hands down, JavaScript is undeniably better than Python for website development for one simple reason: JS runs in the browser while Python is a backend server-side language. While Python can be used in part to create a website, it can't be used alone.

Is there triple equal in Python?

So we would say that Python doesn't have an exact equivalent to the JavaScript == or === operators. The way Python uses == , without a === operator, is the norm.


2 Answers

Python Part

SO can you have two different instances of (say) a string of "Bob" and have them not return true when compared using 'is'? Or is it infact the same as ===?

a = "Bob" b = "{}".format("Bob") print a, b print a is b, a == b 

Output

Bob Bob False True 

Note: In most of the Python implementations, compile time Strings are interned.

Another example,

print 3 is 2+1 print 300 is 200+100 

Output

True False 

This is because, small ints (-5 to 256) in Python are cached internally. So, whenever they are used in the programs, the cached integers are used. So, is will return True for them. But if we choose bigger numbers, like in the second example, (300 is 200+100) it is not True, because they are NOT cached.

Conclusion:

is will return True only when the objects being compared are the same object, which means they point to the same location in memory. (It solely depends on the python implementation to cache/intern objects. In that case, is will return True)

Rule of thumb:

NEVER use is operator to check if two objects have the same value.


JavaScript Part

Other part of your question is about === operator. Lets see how that operator works.

Quoting from ECMA 5.1 Specs, The Strict Equality Comparison Algorithm is defined like this

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is Number, then
    1. If x is NaN, return false.
    2. If y is NaN, return false.
    3. If x is the same Number value as y, return true.
    4. If x is +0 and y is −0, return true.
    5. If x is −0 and y is +0, return true.
    6. Return false.
  5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. Return true if x and y refer to the same object. Otherwise, return false.

Final Conclusion

We can NOT compare Python's is operator and JavaScript's === operator, because Python's is operator does only the last item in the Strict Equality Comparison Algorithm.

7. Return true if x and y refer to the same object. Otherwise, return false. 
like image 71
thefourtheye Avatar answered Sep 21 '22 12:09

thefourtheye


Completely different.

>>> a = 'foo' >>> b = 'bar' >>> a + b is 'foobar' False >>> 1000 + 1 is 1001 False 
like image 41
Ignacio Vazquez-Abrams Avatar answered Sep 20 '22 12:09

Ignacio Vazquez-Abrams