Explanation of list reference
On Mon, Feb 17, 2014 at 5:21 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> So before you ask: for-loops aren't things (values). Neither are while-
> loops, try...except blocks, pass statements, del statements, etc. Nor are
> names and other references -- I believe that there is a practice in
> certain areas of computer science to call them "l-values", which despite
> the name are not values at all -- at least not in Python.
Usually an l-value is something that can go on the left side of
assignment, and an r-value is something that can go on the right side
of assignment. (In most languages, every l-value is also an r-value,
In C, for instance, any simple variable is an l-value, except for
those that are constants (you can't assign to an array). A
dereferenced pointer is also an l-value, as is a structure element
reference. An expression usually isn't (unless it results in a
dereferenced pointer). C++ adds references. You can always assign to
those (the assignment "happens" to the referred-to "thing").
Python says that these things can be assigned to: names, attributes
(with dot), and items (with square brackets); I think that's it. Those
are Python's l-values. Anything that evaluates to an object reference
is an r-value, with the possible exception of function-local names
that haven't been assigned to (does it count as "not an r-value" if
trying to read it raises an exception?).
Python's statements aren't values at all, and can't be referred to.
(Though some of them, like class and def, do result in values or name
bindings. But you can't refer to the def statement, only to the
function object that it creates.)