I saw this question here and now I'm curious. How does the is operator behave in python in comparison to the === sign in JS?
3 Answers
No, they are not the same. is in Python checks if two objects have the same id in Python, ie. they are the same, even in memory. Something you can do to check is this:
>>> a='foo'
>>> a is 'foo'
True
>>> id(a)
44434088
>>> id('foo')
44434088
>>> a=[1]
>>> a is [1]
False
>>> id(a)
45789792
>>> id([1])
4469824
15 Comments
id(a) and id('foo') being the same is something that frequently confuses people when they're trying to understand identity in python. The fact that 'foo' is interned by the interpreter is not really an entry level python topic :-)id values assigned?is will only return true if it's the comparing the exact same object in memory.'foo' and a are clearly not the same object, so why does their comparison return true?is on two strings which are equivalent will return. With that said, it's a pretty niche use-case. Generally is comparisons aren't done on strings (or small integers which are the other common objects that can exhibit this strange behavior). Equality is generally good enough. If you really need to do reference checking, it's more common to do that on named sentinel objects (FOO = object() rather than FOO = 'foo').In Javascript, the == operator will do implicit type conversion when comparing for equality so, for instance, [] == "" will return true. The === operator is used to check equality without type conversion ([] === "" returns false.)
In Python, the is keyword checks reference equality. So x is y will only return true if x and y both point to the same object in memory. For instance:
x = [1, 2, 3]
y = [1, 2, 3]
z = x
x is y # False
x is z # True
Some gotchas that may result from this are the suggested check for null, x is None. None always points to the same space in memory, so you can be assured that x is None will always return True if x has a None value (and False otherwise).
You may also come across some quirks, like the following:
x = 1
y = 1
x is y # True
The above is a result of a non-standard behaviour in CPython (the Python interpreter you're probably using) where small integers are all assigned to specific objects when the program starts up. You can check that this doesn't work for larger numbers:
x = 1000
y = 1000
x is y # False
Unless you're checking for None or if you specifically want to make sure two variables are pointing to the same location in memory, you should use == instead of is.
Comments
No. is in Python is seemingly much stricter than === in JS.
In JS:
For value types (numbers):
a === breturns true if a and b have the same value and are of the same typeFor reference types:
a === breturns true if a and b reference the exact same objectFor strings:
a === breturns true if a and b are both strings and contain the exact same characters
In Python:
Without contradiction, except id(obj1) is the same as id(obj2), both objects are not identical, and obj1 is obj2 will evaluate to False.
isand ==, I want to know how it relates to===.