[Tutor] Clarification questions about how Python uses references.

Cameron Simpson cs at cskk.id.au
Wed Jun 23 21:12:20 EDT 2021


On 23Jun2021 17:18, boB Stepp <robertvstepp at gmail.com> wrote:
>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?

Yes.

Unsure. Maybe read the source. I don't think the language specifies that 
this must occur.

Remember, _any_ immutable type can do this keep-just-one-copy approach.  
Just accept that it may happen. It almost never affects how you code.

>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?

Yes. Just like _any_ variable or container.

>How could I prove this to myself in the
>interpreter?

    >>> L1 = [1]
    >>> L2 = [2]
    >>> LL1 = [L1, L1]
    >>> LL1
    [[1], [1]]
    >>> LL3 = [L1, L2]
    >>> LL3
    [[1], [2]]
    >>> L1.append(2)
    >>> LL1
    [[1, 2], [1, 2]]
    >>> LL3
    [[1, 2], [2]]
    >>> LL1[0] is L1
    True

>Does this translate to tuples and sets?

Of course. They just store references.

>Even though
>tuples are immutable they can contain mutable objects.

Sure. Immutable means you can't change the references, not necessarily 
that the references are to other immutable things.

>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?

That's because set elements, like dict keys, need to be hashable and 
stable. Collisions in sets and dicts rely on the objects' hash functions 
staying the same, because the hash function governs which slot in the 
internal hash table contains the reference.

If the hash of an object changes, the interpreter will look in a 
different hash slot. badness ensures.

The explicitly immutable types like str or tuple have a __hash__ method.  
The contract you obey if you implement a __hash__ method yourself is 
that it is stable regardless of what happens to the object. So most 
mutable types don't provide a __hash__, which is how you know you can't 
use them in a set: try to put one in a set and when the set tries to 
access the hash code it fails.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list