1

How Python does sys.intern() works.

>>> x = '123'
>>> y = '12'
>>> y = y+'3'
# assigned new location to save it. logical as y calculated at run time.
>>> x is y
False
>>> x == y
True
# logical it is now pointing to predefined '123'.
>>> intern(y) is x
True

My question is

# adding white space in string intern is not working.
>>> x = '123 4'
>>> intern('123 4') is x
False
>>> intern('123 4') is intern(x)
True

Why need to use intern on x?

Second question.

>>> id('123 4')== id('123 4')
True
# also returns true
>>> x = '123 4'; y ='123 4'; id(x) == id(y)
True

But this same thing returns false.

>>> x = '123 4'
>>> y = '123 4'
>>> id(x) == id(y)
False

Thanks in advance. :)

Ankit Sachdeva
  • 179
  • 1
  • 9
  • >>> intern('123 4') is intern(x) True Why i have to use intern on both? – Ankit Sachdeva Mar 04 '15 at 10:48
  • 1
    Because `intern` can only affect the string that was given to it. It can ensure that any *future* attempt to make a string that looks like `'123 4'` is pulled out of the pool, but that doesn't affect *other existing* string objects that happen to be equal. To make that work, it would have to search for every object in the program. – Karl Knechtel Mar 04 '15 at 10:57
  • >>> x = '123' >>> intern('123') is x Returns True. No needed to do intern here? Please suggest some answer on my second question asked above. – Ankit Sachdeva Mar 04 '15 at 11:05

0 Answers0