1

I am wondering why

a = "abcdghf23"
b = "abcdghf23"

c = range(100)
d = range(100)

e = []
f = []

a is b retruns True

while c is d returns False

and e is f returns False

actually the question is why id of a and b are the same but id of the rest is different.

they are all iterables.

a b c d are immutable

e and f are mutable

Cannot figure out the pattern and the cause of this

Amin Ba
  • 1,603
  • 1
  • 13
  • 38
  • 1
    Python can cache the strings because it knows they are immutable. Its a special case for the compiler. It has no such knowledge of the `range` object. – tdelaney Jan 19 '23 at 04:19
  • 4
    Honestly, the `is` operator is nearly useless and should be tossed from your thinking. Virtually the ONLY legitimate use is in the test `if x is None:`. – Tim Roberts Jan 19 '23 at 04:21
  • `range` is a type. `c` and `d` are *instances* of that type, and those instances are independent, even if created with the same argument. – chepner Jan 19 '23 at 12:21

0 Answers0