tough-to-explain Python

Ben Finney ben+python at
Tue Jul 7 20:06:07 EDT 2009

kj < at> writes:

> In <mailman.2796.1246997332.8015.python-list at> Chris Rebert <clp2 at> writes:
> >You might find the following helpful (partially):
> >
> Extremely helpful.  Thanks!  (I learned more from it than someone
> who will teach the stuff would care to admit...)

I have got very good results from teaching using the analogy of “paper
tags tied to physical objects” to describe Python's references to

The analogy allows illustration of many otherwise confusing facts:

  * an assignment is the act of putting a tag onto an object

  * a tag leads to exactly one object, and it can be re-tied to a
    different object

  * an object can have arbitrarily many tags on it

  * containers (e.g. lists, dicts, sets, etc.) don't contain objects,
    they contain tags leading to objects

  * passing arguments to a function always makes new tags leading to the
    objects passed in, and throws those new tags away once the function

  * etc.

The analogy can then be contrasted to how Python *doesn't* do it: named
boxes with one value per box. You can point out that many other
languages do use this model, so they should be aware of it, but Python
doesn't use it.

> I had not realized how *profoundly* different the meaning of the
> "=" in Python's
>   spam = ham

Access the object referenced by ‘ham’, and assigns the reference ‘spam’
to that object.

> is from the "=" in its
>   spam[3] = ham[3]

Access the object referenced by ‘ham[3]’, and assigns the reference
‘spam[3]’ to that object.

No, they're exactly the same. The only thing that's different is the
references you use; the assignment operates *exactly* the same in both

 \     “We are no more free to believe whatever we want about God than |
  `\         we are free to adopt unjustified beliefs about science or |
_o__)              history […].” —Sam Harris, _The End of Faith_, 2004 |
Ben Finney

More information about the Python-list mailing list