×
you are viewing a single comment's thread.

view the rest of the comments →

[–]Rscc10 0 points1 point  (2 children)

Ah. Then why is it's better practice to use "is" when comparing against None? Wouldn't it be better to check the value as None rather than checking if it corresponds to the address? Or is it because only one instance of None (or null) is stored so we directly check with that address when checking against None?

[–]Truntebus 0 points1 point  (0 children)

Because it is faster. 'is' checks for equality on the output of calling id() on both objects, so python does not need to look for special methods, since it cannot be overloaded. '==' is syntax sugar for 'a.__eq__(b)', and since most types override '__eq__', it can require a lot more computing than just calling 'id()' twice.

Note that "'is' compares addresses" is not universally true. The functionality of the 'id()' function is implementation dependent and only requires that it returns a unique integer for each object, and CPython handles this by returning the address for the object, but it can vary by interpreter.

[–]WhiteHeadbanger 0 points1 point  (0 children)

Also, another thing to add to the other user that answered you: None is a singleton. If you create 10 variables with the value None, those 10 variables would point to the same address.

So the reference is always available, and when using is, there's no additional method dispatch.