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. . . .
Completely different.
However note that:
In this case it condition was
True
because the compiler is free to intern string literals, and thus reuse the same object, and it does do that with small strings. However there is no guarantee as to when this happens of if this happens at all and the behaviour changes between versions and implementations.A realiable
False
output should be:Or anything that actually computes the strings.
Python Part
Output
Note: In most of the Python implementations, compile time Strings are interned.
Another example,
Output
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 returnTrue
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 returnTrue
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 returnTrue
)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
Final Conclusion
We can NOT compare Python's
is
operator and JavaScript's===
operator, because Python'sis
operator does only the last item in the Strict Equality Comparison Algorithm.Python's
is
keyword compares references (and so is about identity) while===
does a minimal amount of coercion (and is therefore concerned with equality, at least in the case of primitives) so they are different.As I understand it, things that are concerned with identity are concerned with uniqueness from the runtime's point of view (do these two variables point to the same address in memory) while equality is concerned with the uniqueness of the contents of the variables (are these two variables equivalent, regardless of where they are placed in memory relative to each other).