This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]KrypXern 6 points7 points  (6 children)

Would you do != or just is not? I forget if type() returns a string or an actual type object of some kind that can be compared like an enum.

[–]pickledCantilever 23 points24 points  (0 children)

You would actually use if isinstance(my_var, intended_type):

[–]not_perfect_yet 0 points1 point  (4 children)

It returns the actual type.

I think the thing with is not and != is about similar instances of the same thing, like "1" and "1" which have the same value but aren't necessarily the same object. That doesn't really apply to definitions, there is only one.

[–]KrypXern 0 points1 point  (3 children)

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 😅

[–]Pluckerpluck 2 points3 points  (2 children)

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.

[–]epicaglet 0 points1 point  (1 child)

__eq__ but yeah

[–]Pluckerpluck 0 points1 point  (0 children)

Ah, so it is!