List semantics [was Re: Slices time complexity]

Gregory Ewing greg.ewing at canterbury.ac.nz
Wed May 20 04:54:00 EDT 2015


Steven D'Aprano wrote:

> Code snippet 1:
> 
>     x = [[1,2],[1,2]]
> 
> creates a list bound to the name "x", containing a list containing ints 1 
> and 2, and a second independent list also containing ints 1 and 2.

Using the word "contains" here is misleading, because
it conjures a mental picture of physical containment,
which is exactly what you *don't* want to suggest.

>     z = [1,2]
>     y = [z, z]
> 
> creates a list bound to the name "z", ints 1 and 2; then it creates a second 
> list, bound to name "y", containing the first list "z" twice.

At this point the student thinks, "Um... what? How
can an object contain another object *twice*?"
If he's still thinking in physical terms, this
sentence is nonsensical.

It gets even worse with:

x = [1, 2]
x[1] = x

Now you have to say that the list contains *itself*
in some weird Tardis-like fashion!

I can't think of any way to dispel the confusion
verbally without using some word like "reference"
or "pointer" or something with an equivalent meaning.
Something that suggests a level of indirectness.

> A diagram may help, especially for more complicated situations,

Seems to me that a diagram is far and away the
easiest and most effective way to convey what's
really going on. It's difficult to do that with
words alone, because English isn't really equipped
to talk about it precisely enough.

-- 
Greg



More information about the Python-list mailing list