0

When declared an integer to a variable, both the integer and variable returns the same id value. But, in case of float, it returns a different id value. What is the reason behind it?

As below,

a=10
>>> id(a)
48068132
>>> id(10)
48068132
>>> b=10.1
>>> id(b)
50912320
>>> id(10.1)
50912336
azro
  • 53,056
  • 7
  • 34
  • 70
  • 2
    It's not guaranteed that different numbers with same value will be references to same object. – Olvin Roght Jan 05 '22 at 18:33
  • The short answer is that small integers (such as 10 in your example) are special. The long answer is in the duplicate link at the top. – John Gordon Jan 05 '22 at 18:40
  • "When declared an integer to a variable, both the integer and variable returns the same id value." No, that isn't necessarily true. There is *never any such guarantee*. Also note, sometimes, you can get the same behavior from `float` objects (e.g. put it in a function, if you are in a REPL to force the literals to be compiled in the same block) – juanpa.arrivillaga Jan 05 '22 at 18:42

0 Answers0