1

Can someone please explain the following output to me? enter image description here

Are not x and y different objects in memory?

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Karl Neumann
  • 103
  • 1
  • 12
  • Numbers that are within -5 and 256 are representing the same object – U13-Forward Sep 21 '21 at 02:27
  • They aren't different objects. Both names are bound to the same object. – Tim Roberts Sep 21 '21 at 02:27
  • 1
    @TimRoberts In this case, yes, but it would be different for, say, `a = 300` and `b = 300`. They are asking about the reason why this is so. – Selcuk Sep 21 '21 at 02:30
  • This is telling you that they aren't. Also note, thinking about "memory location" is the wrong level of abstraction in Python. Here you have two references to some object, the ID is the same, during their overlapping lifetime, therefore, they are the same object. IOW, `x is y`. – juanpa.arrivillaga Sep 21 '21 at 02:34
  • Essentially, while you might *expect* these two be two different `int` objects, the Python runtime is free optimize immutable built-in objects and not actually create something new, because semantically, it should never make a difference. – juanpa.arrivillaga Sep 21 '21 at 02:35

0 Answers0