precedence of [] vs .

Fredrik Lundh fredrik at pythonware.com
Fri Aug 15 09:33:38 CEST 2008


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).

</F>




More information about the Python-list mailing list