precedence of [] vs .

Terry Reedy tjreedy at udel.edu
Fri Aug 15 14:36:04 EDT 2008



Fredrik Lundh wrote:
> Calvin Spealman wrote:e
> 
>> attribute access (foo.bar) binds more tightly than subscripting 
>> (foo[bar]).
> 
> no, they have the same binding power; here's the relevant part of the 
> grammar:
> 
>     trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
> 
> note however that "." only binds to a name, not a full expression (as 
> Carl noted).
> 
> the summary at http://docs.python.org/ref/summary.html is broken; the 
> source code for that page looks like this:
> 
>   ...
>   \hline
>     \lineii{\code{+}, \code{-}}{Addition and subtraction}
>   \hline
>     \lineii{\code{*}, \code{/}, \code{\%}}
>            {Multiplication, division, remainder}
>   \hline
>     \lineii{\code{+\var{x}}, \code{-\var{x}}}    {Positive, negative}
>     \lineii{\code{\~\var{x}}}            {Bitwise not}
>   \hline
>     \lineii{\code{**}}                {Exponentiation}
>   \hline
>     \lineii{\code{\var{x}.\var{attribute}}}    {Attribute reference}
>     \lineii{\code{\var{x}[\var{index}]}}    {Subscription}
>     \lineii{\code{\var{x}[\var{index}:\var{index}]}}    {Slicing}
>     \lineii{\code{\var{f}(\var{arguments}...)}}    {Function call}
>   \hline
>   ...
> 
> which indicates that the author intended "." and "[" to appear in the 
> same box, but got overruled by the Tex->HTML conversion tool.
> 
> (if someone has the bandwidth, please submit a documentation bug).

Already done: http://bugs.python.org/issue3558

I took the liberty of cutting and pasting your explanation, and added
""x.attribute, x[index], x[index:index], f(arguments...)"
will not fit in the Operator column.  I suggest adding
"[Next 4 have same precedence]                  Trailers"
with the next four lines indented 3 or 4 spaces in each column."

Feel free to add more if you think more is needed.

Terry Jan Reedy




More information about the Python-list mailing list