> It also interns many ints, and they can't be used as names at all. To clarify on "many ints", integers in the range of -5 to 256 (inclusive) are interned. This can be demonstrated with the following:
```py >>> a = 256 >>> b = 256 >>> a is b True >>> a = 257 >>> b = 257 >>> a is b False >>> a = -5 >>> b = -5 >>> a is b True >>> a = -6 >>> b = -6 >>> a is b False ``` On Wed, Dec 4, 2019 at 3:30 AM Steven D'Aprano <st...@pearwood.info> wrote: > On Wed, Dec 04, 2019 at 07:11:39PM +1300, Greg Ewing wrote: > > On 4/12/19 12:53 pm, Soni L. wrote: > > >Okay, sometimes it's also used for that. But the main use-case is for > > >lowering RAM usage for immutable objects. > > > > Citation needed. If that's true, why does Python intern > > names used in code, but not strings in general? > > py> s = "ab1234z" > py> t = "ab1234z" > py> s is t > True > > > CPython doesn't *just* intern names. Nor does it intern every string. > But it interns a lot of strings which aren't used as names, including > some which cannot be used as names: > > py> a = "+" > py> b = "+" > py> a is b > True > > It also interns many ints, and they can't be used as names at all. > > Here's a good explanation of interning in Python 2.7, including a great > example of how interning strings can reduce memory usage by 68%. > > > http://guilload.com/python-string-interning/ > > > > -- > Steven > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/H6WOEL5CJ67P36AE3H425EDY4MGWE7K2/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/UPN7C7I4I3QBJXZ4K3NX5B5IJECHBMC7/ Code of Conduct: http://python.org/psf/codeofconduct/