Explanation of list reference

Chris Angelico rosuav at gmail.com
Sat Feb 15 01:07:17 EST 2014


On Sat, Feb 15, 2014 at 4:36 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> References can be names like `mystring`, or list items `mylist[0]`, or
> items in mappings `mydict["key"]`, or attributes `myobject.attr`, or even
> expressions `x+y*(1-z)`.

I agree with most of what you've said, but I'm not sure I like that
last bit. The expression evaluates to an object, yes, but it's not
itself a reference... is it? It has references to x, y, 1, and z, but
it's not itself a reference to anything. If you take a reference and
assign it to a name, and take that same reference and assign it to
another name, I would expect those two names to now refer to the same
object:

>>> lst = [1000, 2000, 3000]
>>> x = lst[1]
>>> y = lst[1]
>>> x is y
True

But with expressions, it's not so, and new objects may be created. The
expression isn't a reference to its result, but it can yield an
object, which it (naturally) does by reference.

ChrisA



More information about the Python-list mailing list