precedence of [] vs .

Carl Banks pavlovevidence at gmail.com
Fri Aug 15 01:08:42 EDT 2008


On Aug 14, 7:17 pm, Michael Tobis <mto... at gmail.com> wrote:
> On Aug 14, 6:01 pm, "Calvin Spealman" <ironfro... at gmail.com> wrote:
>
>
>
> > attribute access (foo.bar) binds more tightly than subscripting (foo[bar]).
>
> That certainly looks right, and in retrospect I wonder that I even
> doubted it. But even the official docs seem to me to specify
> otherwise:
>
> http://docs.python.org/ref/summary.html


I think the summary is correct (am not going to bother to double-
check), but there's a subtle point you're missing.  Here is a
simplified explanation.


The syntax for subscript operator is:

expression [ expression ]


The syntax for attribute access is:

expression . symbol


The subtle point here is that the symbol that comes after the dot is
NOT an expression, and therefore can't serve as the first expression
in the subscript opreator rules.

The effect of this is that the grouping a.(b[c]) is impossible.


Carl Banks




More information about the Python-list mailing list