3

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?

1
  • 1
    How is that the same question? It makes no mention of JS anywhere, and while I know the difference between is and ==, I want to know how it relates to ===. Commented Jun 29, 2016 at 19:33

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

15 Comments

FWIW, I think that 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 :-)
So, is it like a hash of the object? How are id values assigned?
@Ares As in is will only return true if it's the comparing the exact same object in memory.
Yeah, but 'foo' and a are clearly not the same object, so why does their comparison return true?
@Ares -- Nope, there isn't a good way to tell what 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').
|
2

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

1

No. is in Python is seemingly much stricter than === in JS.

In JS:

For value types (numbers): a === b returns true if a and b have the same value and are of the same type

For reference types: a === b returns true if a and b reference the exact same object

For strings: a === b returns 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.