0

Why c and d have the same memory id?

class A:
    def __init__(self, number):
        self.n = number

a = A(1)
b = A(1)
id(a) == id(b)    # False

L1 = [1, 2, 3]
L2 = [1, 2, 3]
id(L1) == id(L2)  # False

but

c = 1
d = 1
id(c) == id(d)    # True   
Deduplicator
  • 43,322
  • 6
  • 62
  • 109
kubaSpolsky
  • 141
  • 9
  • 1
    enjoy coding in python and never use `id` again. – Jean-François Fabre Jan 24 '19 at 20:17
  • Can us `c is d` and still will be the same result `True`, even more, giving them value outside the array [-5, 256] it still gives `True`, why? Enjoy unswering me this question. – kubaSpolsky Jan 24 '19 at 20:32
  • 1
    that's because your python implementation caches integers outside this range. That's really an implementation detail since integers are immutable anyway. Stop bothering with that, it's not worth it. `id` is for debugging, and to be honest I have never used it. – Jean-François Fabre Jan 24 '19 at 20:36

0 Answers0