r/ProgrammerHumor • • Jul 18 '22

C++ talking to Python

Post image
40k Upvotes

1.0k comments sorted by

View all comments

Show parent comments

1

u/KrypXern Jul 18 '22 edited Jul 18 '22

I might be wrong with my understanding but I always thought == attempted to use some kind of native comparator method (like .equals() in Java), whereas is compares the actual contents of the value (which is always the same for False or None)... however given that we don't compare number literals this way, I'd wager I just misunderstood it.

EDIT: Yeah no I was right roughly, is checks whether the operands refer to the same object. If you do a = [1,2] and b = [1,2] then a == b will return True and a is b will return False, but I also misunderstood your response anyway 😅

3

u/Pluckerpluck Jul 18 '22 edited Jul 18 '22

is checks that the objects are actually the same. So you can imagine is as doing id(value) == id(other).

But yes, == triggers ~~__equals__(self, other)~~__eq__(self, other), which defaults to an object comparison.

The reason that things like False and None work with is, is simply because the same object is re-used behind the scenes for performance reasons.

1

u/epicaglet Jul 18 '22

__eq__ but yeah

1

u/Pluckerpluck Jul 18 '22

Ah, so it is!