[Tutor] Clarification questions about how Python uses references.

Peter Otten __peter__ at web.de
Thu Jun 24 03:37:22 EDT 2021


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

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



More information about the Tutor mailing list