1

I use a = 'linux!', b = 'linux!', c = 'linux', d='linux',Why does this happen?

 Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'linux!'
>>> b = 'linux!'
>>> id(a)
2502413115552
>>> id(b)
2502414669112
>>> c = 'linux'
>>> d = 'linux'
>>> id(c)
2502413088616
>>> id(d)
2502413088616
>>>
jamylak
  • 120,885
  • 29
  • 225
  • 225

1 Answers1

4

Python can choose when to reuse immutable objects with equal values. The semantics of the language don't dictate whether a and b must be the same object or not.

Object reuse is an optimization with trade-offs. I believe strings are reused if they are likely to be identifiers, but that might just be a rumor.

Ned Batchelder
  • 345,440
  • 70
  • 544
  • 649