[Tutor] Clarification questions about how Python uses references.

Roel Schroeven roel at roelschroeven.net
Fri Jun 25 06:56:03 EDT 2021


boB Stepp schreef op 24/06/2021 om 0:18:
> I continue to attempt to refine my understanding of how Python uses
> identifiers to reference objects.
 > ...
> 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?

Have you seen Ned Batchelders presentation "Python Names and Values"? I 
think it offers a very good way of thinking about how Python works. You 
can find it here: https://nedbatchelder.com/text/names1.html

Something to keep in mind while reading that (or while watching the 
video), since you're asking about lists and other containers: somewhere 
halfway down the page Ned writes:

"All of the examples I’ve been using so far used names as references to 
values, but other things can be references. Python has a number of 
compound data structures each of which hold references to values: list 
elements, dictionary keys and values, object attributes, and so on. Each 
of those can be used on the left-hand side of an assignment, and all the 
details I’ve been talking about apply to them."

He doesn't mention that in the beginning to keep things simple, but it's 
an important concept to know about.

-- 
"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
         -- Franklin P. Jones

Roel Schroeven



More information about the Tutor mailing list