1

This is not a question of when to use is and when to use ==. I know that is checks if id of objects match and == checks if the values match.

Please have a look at the following.

>>> x = 3
>>> y = 3
>>> x is y
True
>>> m = 3.0
>>> n = 3.0
>>> m is n
False
>>> x = "spam"
>>> y = "spam"
>>> x is y
True
>>> a = ("s","p","a","m")
>>> b = ("s","p","a","m")
>>> a is b
False

You will notice that for the str- and int-variables, they are always automatically referring to the same object. But that is not the case for the tuple or float.

I am looking for an explanation why that is the case. Of course, I can go through all the types in this way, and write down when this happens, and when it does not. That is not an explanation.

This also happens with bool and NoneType. But it does not happen with function. And so on. Its easy to check all of these, but that doesn't give any insight. I have searched google for 'unique objects in python', and not really getting anything there. I also looked on YouTube. I ruled out that this has anything to do with immutability because both integers and floats are immutable.

The behavior changes if I do something like this

>>> def same(x, y):
...     u = x
...     v = y
...     return u is v
...
>>> same(1.0, 1.0)
True
6
  • 5
    There IS NO answer, other than "this is how CPython happens to do things". There is no guarantee that immutable objects will or will not be reused (mutable objects will of course always be newly created). You would get different answers with different integers or strings, and with different Python versions, or different Python build options. Any program that depends on the exact behavior of object reuse is utterly broken. Commented Mar 26, 2024 at 22:09
  • 3
    There are only a few language-guaranteed singletons, e.g. None, True, and False, also the ... object, maybe some others, but that's it. Everything you are seeing above is the result of implementation details, usually, the result of the runtime optimizing something, but they are not facts that can be relied on. One such example is the small integer cache. Commented Mar 26, 2024 at 22:17
  • 2
    or, in the case of same(1.0,1.0) that expression is being compiled as a single code block in the REPL, so the compiler optimizes these to a single object. Again, an implementation detail, but not something that can or should be relied on. you could get the same behavior with 1.0 is 1.0 Commented Mar 26, 2024 at 22:18
  • 1
    They're due to runtime specific optimizations: string pools and integer caches. Runtimes for many languages do this in an attempt to strike a balance between the CPU cost of reallocating and the RAM cost of caching. Python 3.11.7 caches all strings and integers [-5, 256]. Commented Mar 26, 2024 at 22:44
  • 2
    also relevant: stackoverflow.com/a/70723987/13944524 Commented Mar 26, 2024 at 23:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.