Explanation of list reference

Chris Angelico rosuav at gmail.com
Sat Feb 15 07:57:40 CET 2014


On Sat, Feb 15, 2014 at 5:48 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Sat, 15 Feb 2014 17:07:17 +1100, Chris Angelico wrote:
>
>> 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?
>
> You may be right. I will have to think about it a little more. Or a lot
> more. Ah wait, I got it: I chose a bad example for the expression. Here
> is a better one:
>
> myobj.alist[12]["some key"].attribute
>
> I think it is fair to call that both an expression and a reference.

Sure. If it helps, the part that's a reference is the ".attribute" at
the end; the rest is an expression which determines what object's
attribute you're looking at. Same with the earlier parts; the [12] is
a form of reference (albeit one that requires another object). But
yes, this is an expression, and it evaluates to a reference.

In any case, your main point is still valid: each of those forms will
yield a reference to something. (Aside from the possibility of raising
KeyError. As an expression, it has to yield a reference to an object.)

ChrisA



More information about the Python-list mailing list