3 ms·
Short strings in Python are interned, i.e. 'foo' always returns the same string, not just an identical one. There's some noise but the important part is that b
by sjs 17y ago
Short strings in Python are interned, i.e. 'foo' always returns the same string, not just an identical one.
There's some noise but the important part is that both x and y have the same address (0x37e300) and point to the same str object.
Python 2.6.4 (r264:75706, Nov 25 2009, 21:38:24)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x='foo'
>>> y='foo'
>>> x.__add__
<method-wrapper '__add__' of str object at 0x37e300>
>>> y.__add__
<method-wrapper '__add__' of str object at 0x37e300>
>>> x is y
True
and a counter-example just for fun:
>>> x='this is a longer string. i wonder what the limit is?!'
>>> y='this is a longer string. i wonder what the limit is?!'
>>> x.__add__
<method-wrapper '__add__' of str object at 0x35a840>
>>> y.__add__
<method-wrapper '__add__' of str object at 0x35a890>
>>> x is y
False