Explanation of list reference

Ben Finney ben+python at benfinney.id.au
Sat Feb 15 05:20:36 CET 2014

Marko Rauhamaa <marko at pacujo.net> writes:

> Chris Angelico <rosuav at gmail.com>:
> > Distinguishing "small values" from "big values" leads to the obvious
> > question: Which is which? And why doesn't this work?
> This is related to the recent id(string) question on this forum.
> Unfortunately neither the "everything is a reference" model nor the
> "small/big" model help you predict the value of an "is" operator in the
> ambiguous cases.

You should never need to predict the result of an ‘is’ operation. (More
precisely, for *some* cases you can predict it, but for other cases you

The Python implementation is free to behave unpredictably for the state
of object identity. It may have some objects that conceptually may be
different (i.e. you'd predict the ‘is’ operation would return False)
actually be the same object (i.e. the ‘is’ operation would return True).

Exactly the same case may behave differently in this regard on other
Python implementations, or different versions of the same Python
implementation, or even exactly the same version of the Python
implementation under different circumstances.

The management of object identity is an implementation detail, not to be
relied on in your Python code. So, if your teaching method depends on
general principles for predicting object identity, you're already

> Explaining Python's memory model at some level is necessary right off
> the bat. However, it is far from easy to understand.

The use of the term “variable”, and all the implications that has for
“variables contain values” etc., is IMO too confusing, and its use in
the Python documentation is not helping this.

Rather, I recommend that people teaching Python should avoid the term
“variable” entirely, and use the term “reference” which much more
accurately implies Python's data model. An addressable item in a
container is a reference, as is a name. Assignment binds a reference to
a value. And so on.

This article by the Effbot corrects similar misconceptions
<URL:http://effbot.org/zone/python-objects.htm>, and is a valuable
approach to teaching the Python data model.

 \       “Theology is the effort to explain the unknowable in terms of |
  `\                         the not worth knowing.” —Henry L. Mencken |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list