0

Why do 2 python variables share the same ID after they are initialised with the same value? Surely they are 2 independent objects...unless it is an efficiency saving?

name1="Steve"
name2="Steve"
id(name1), id(name2)

Outputs the same ID for both objects?!

smackenzie
  • 2,880
  • 7
  • 46
  • 99
  • 5
    CPython interns strings that are valid identifiers - this is an implementation detail, and shouldn't be relied upon. – jonrsharpe Nov 15 '16 at 17:58
  • 2
    And to add on to what @jonrsharpe said, it doesn't matter because strings are immutable, so it's not like you can accidentally change the object referred to by `name1` and alter the object referred to by `name2`. If you do `name1 = name1.lower()`, `name2` will still refer to the uppercase `"Steve"` because `name1` now refers to a different string. – Steven Rumbalski Nov 15 '16 at 18:00

0 Answers0