I found out that in immutable classes, __new__ can return a cached reference to an existing object with the same value; this is what the int, str, and tuple types do for small values. This is one of the reasons why their __init__ does nothing.
So, cached objects will be reinitialized again and again. But how the following two fragments in behavior differ.
With a space at the end:
>>> a = 'string ' >>> b = 'string ' >>> a is b False >>>
Without space:
>>> c = 'string' >>> d = 'string' >>> c is d True >>>
Can someone please explain to me how space makes a difference?
python string immutability python-internals
James Sapam Jan 18 '14 at 11:03 2014-01-18 11:03
source share