precedence of [] vs .

Carl Banks pavlovevidence at
Fri Aug 15 07:08:42 CEST 2008

On Aug 14, 7:17 pm, Michael Tobis <mto... at> wrote:
> On Aug 14, 6:01 pm, "Calvin Spealman" <ironfro... at> wrote:
> > attribute access ( 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:

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