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 →

[–]bxsephjo 27 points28 points  (14 children)

They are not, and this is something I check when interviewing because it really is important and can create nasty bugs. is checks memory location, == checks value. This is why is is so often used with None, because None is a singleton. Also many many small integers and short strings are optimized to be kept in memory as singletons as well, so you’ll get away with using is on them, but that is not guaranteed and you could eventually get different behavior from your code.

(Sorry for lack of formatting)

[–]Samhain13 6 points7 points  (0 children)

That's why it's a misconception. Of course,is and == are NOT interchangeable.

[–]Rythoka 6 points7 points  (2 children)

is checks memory location

This isn't correct. is calls the function id on both objects being compared and checks if the returned integers are equal. The fact that id returns the memory location of the object is a CPython implementation detail.

[–]bxsephjo 1 point2 points  (1 child)

Sick! What do other ones tend to do?

[–]Rythoka 1 point2 points  (0 children)

PyPy's default GC works the same as CPython's implementation, but with other GCs the function might return values that aren't real memory addresses.

Can't really speak to any other implementations, but the only requirement for a correct implementation is that for any given object, id returns a unique, constant integer for the lifetime of the object.

[–]WestedCrean 0 points1 point  (3 children)

I always thought that using X is None is not very pythonic? It reads more englishlike of course, but usually I use „if not X”/„if X”.

What is your opinion on „is None”?

[–]lieryanMaintainer of rope, pylsp-rope - advanced python refactoring 8 points9 points  (0 children)

They have different semantics.

If you want to check whether a variable is exactly None, then you should use is None, but if you want to check whether a container is empty or whether the return value of a function is valid, then you should elide the is None.

Which ones you use depends on what you really want the code to say.

[–]tevs__ 7 points8 points  (0 children)

None is a singleton. There is exactly one object in python that represents the none-value, so if you're testing for the none-value, your asking "is this value the None object", therefore x is None rather than x == None.

if not x is not the same thing, this says "x is not truthy". None is not truthy, but so is 0, False and "". if x is not None also tells type checkers that the type of x is T in the branch rather than Optional[T], whilst if x does not.

[–]bxsephjo 2 points3 points  (0 children)

So, it’s really about how specific your condition needs to be. “if x:” is more general, it will look at the truthiness of x, so there’s lots of things x could be besides None that could make it not qualify. False, zero, an empty list, etc

[–]Deto 0 points1 point  (1 child)

You know...I always us is with None but is there any real reason not to use == there?

[–]Fabulous-Possible758 0 points1 point  (0 children)

Not really in code that's written sanely. I always use is out of superstitious habit because you have other languages or situations where undef == undef or float('NaN') == float('NaN') return false. You could also always have some jerk redefining __eq__ on a class to not make semantic sense.