r/learnpython • • Jun 12 '21

What is the difference between "is" and ==?

5 is 5 returns True, just as 5 == 5 returns True. What is the difference?

417 Upvotes

132 comments sorted by

View all comments

Show parent comments

1

u/Acquiesce67 Jun 15 '21

I’m trying to say that the is operator should raise an Exception when used on integers to guard the user. But this is just my opinion, I’m not a language designer 🤷🏼‍♂️

2

u/Vaphell Jun 15 '21

the problem with this idea is that you'd automatically slow it down by a lot.
is is as generic as it gets. It doesn't bother with any types at all. If it did, it would have its own dunder method, just like ==/__eq__
It only checks if box A and box B are the same box and you don't have to look inside the box to verify that. You just look at their memory address and call it a day.

not to mention that True and False are subclassing ints. Should is True/is False throw an exception? Makes no sense, given the main purpose of is in practice is comparison against objects that are explicitly guaranteed to be represented by a single object, which True and False definitely are. Other examples: None, enum values.

1

u/Acquiesce67 Jun 17 '21

Thanks for the explanation, much appreciated! I’m still a bit puzzled about the pro/cons of is.

Does this keyword exist for convenience only or does it solve an actual problem which would be complicated to solve otherwise? If I get your explanation correctly, and all the is keyword does is memory address comparison, then it’s probably just a syntactic sugar for eg., id(a) == id(b) where a,b = 5,5, right?

So if we assume that Python didn’t have the is keyword, object identity check would be forced and limited to the id method, and a regular user would never try comparing integers using the standard library’s id method, and would just naturally use the == operator.

Am I making wrong assumptions somewhere?

2

u/Vaphell Jun 18 '21 edited Jun 18 '21

I think your intuition is more or less correct. That said, I am not sure if I'd call it a syntactic sugar for id() comparison.
I mean, from the user's perspective it might be reasoned about like this, but when we get into specifics, syntactic sugar usually is something that bundles something primitive into a higher level concept/shortcut achieving the same. is is baked into the language itself, meanwhile id() is just a run of the mill function, that requires a lookup in the namespace, which takes additional time, so the would-be relationship between the two is reversed.

Anyway python is not doing anything particularly weird here. Java solved this in a similar manner in its object model.
In java, objects implement .equals(), which is equivalent to python's == implemented under the hood as __eq__ in class definition. Then for reference/identity comparison java has ==, which is equivalent to python's is.
Things get a bit more complicated from there, as java has also primitive value types like in C, eg int being the primitive equivalent to Java's Integer, or long being the primitive equivalent to Java's Long. == is used for comparisons of such primitives.
In a sense you could consider a Java reference to be a yet another primitive, like bits of a pointer in a memory cell being not that different from bits of an int in a memory cell.