pythonidentity-operator

is compare id but id should be the same between -5 and 256, how come it's not if x = 3 and y = 5?


a = 10 # range -5 to 256
b = 10 # range -5 to 256
print(id(a))
print(id(b))
print(a is b)

# OK THAT'S FINE 

BUT

a = 10 # range -5 to 256
b = 10 # range -5 to 256
print(id(a))
print(id(b)) # same memory adress
print(a is b)

# That's working

c = 3 # range -5 to 256
d = 5 # range -5 to 256
print(id(c))
print(id(d)) 
print(c is d) 

NOT same memory adress O_o False because... meeeehhh the range should be fine !


Solution

  • With the following illustration, you should understand why:

    for the number between -5 and 256

    and why:

    for other numbers outside of this special range

    enter image description here