[Tutor] Clarification questions about how Python uses references.

boB Stepp robertvstepp at gmail.com
Thu Jun 24 13:27:03 EDT 2021


On Thu, Jun 24, 2021 at 2:38 AM Peter Otten <__peter__ at web.de> wrote:
>
> On 24/06/2021 00:18, boB Stepp wrote:
>
> > But the string example is new to me.  It appears that Python caches
> > smaller strings.  Is this true?  If yes, what might the caching limits
> > be?
>
> https://docs.python.org/3/library/sys.html?highlight=intern#sys.intern

Aha!  You cleared up something else I had meant to look up, but forgot
about.  On the main list Chris Angelico had used "interning" in a
thread and I did not know exactly what he was talking about.  I had
never heard this term used in a programming context before.

> I remembered that potential variable names are interned,
>
>  >>> a = "abc"
>  >>> b = "abc"
>  >>> a is b
> True
>  >>> a = "a b"
>  >>> b = "a b"
>  >>> a is b
> False
>
> but at least sequences of digits are also interned:
>
>  >>> a = "123"
>  >>> b = "123"
>  >>> a is b
> True
>  >>> a = "-123"
>  >>> b = "-123"
>  >>> a is b
> False
>
> There also seems to be a length limit:
>
>  >>> b = "b"*4096
>  >>> a = "b"*4096
>  >>> a is b
> True
>  >>> a = "b"*4097
>  >>> b = "b"*4097
>  >>> a is b
> False

That clears up a lot.  Thanks, Peter!

boB Stepp


More information about the Tutor mailing list