list of lists

Peter Otten __peter__ at web.de
Tue Sep 16 16:37:04 EDT 2003


Dave Kuhlman wrote:

> It's a simple concept, once you grasp it, but for those new to
> Python, it may be worth emphasizing -- You can concatenate
> operators (to the right, at least) and these operators will
> operate on the run-time value produced by the expression
> to which they are applied.  For example (read from bottom up):
> 
> 
>     getArray()[3].formatter()
>        ^    ^  ^ ^          ^
>        |    |  | |          |
>        |    |  | |          +--- (5) call function retrieved from
>        |    |  | |                   attribute
>        |    |  | +-------------- (4) access attribute of object indexed
>        |    |  |                     from array
>        |    |  +---------------- (3) index element of array returned by
>        |    |                        function call
>        |    +------------------- (2) call function retrieved from name
>        |                             binding
>        +------------------------ (1) retrieve value bound to variable
> 
> 
> It is also worth thinking about what is meant by saying that this
> evaluation is *dynamic*.  For example, if the object returned by
> the function call to getArray (above) is not indexable, then the []
> operator will fail.
> 
> And, the only limiting factor is confusion.

Nice explanation. I take the occasion to warn newbies that the sort() method 
is a showstopper in this scheme:

getArray().sort()[3].formatter() #BAD CODE, DON'T USE
             ^
             |
             + -- sort() returns None

By the way, is there any Python programmer who has not made this error at
least once?

Peter




More information about the Python-list mailing list