[Tutor] Clarification questions about how Python uses references.

boB Stepp robertvstepp at gmail.com
Wed Jun 23 18:18:51 EDT 2021


I continue to attempt to refine my understanding of how Python uses
identifiers to reference objects.  Below is an interpreter session
that I have a couple of questions about:

>>> a = 'banana'
>>> b = 'banana'
>>> a is b
True
>>> a = 1000000000
>>> b = 1000000000
>>> a is b
False
>>> a = 'This is a much longer string than previously.  I wonder what the result will be?'
>>> b = 'This is a much longer string than previously.  I wonder what the result will be?'
>>> a is b
False

The integer example is as expected.  I know that Python caches "small"
integers for reuse.  The exact ranges were discussed in a previous
thread.

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?

On to lists.  My current understanding is that lists don't actually
contain the objects themselves, but, instead, references to those
objects.  Is this correct?  How could I prove this to myself in the
interpreter?  Does this translate to tuples and sets?  Even though
tuples are immutable they can contain mutable objects.  Playing around
in the interpreter it looks like even if sets contain tuples, no
mutable elements can be in the tuples.  Is this in general correct?

TIA!
boB Stepp


More information about the Tutor mailing list