[Baypiggies] Discussion for newbies/beginner night talks - stan

Chad Netzer chad.netzer at gmail.com
Wed Feb 14 20:39:36 CET 2007

On 2/14/07, Dennis Reinhardt <DennisR at dair.com> wrote:
> At 11:03 PM 2/13/2007, Drew Perttula wrote:
>  >Some notes:
>  >The python lists and tuples in the xml-creating code will simply
>  >disappear in the output. T.p["foo", "bar"] renders to "<p>foobar</p>".
> Hmmm... I thought the x[y] syntax was doing an element access on dict or
> list x to find element y.  If x is an function, then I am guessing that
> x[y] is shorthand for
>         z = [y]
>         x(z)
> or something like that.

Hmmm.  I'm not sure what you mean here.  If x is a function, then it
is not subscriptable.  I suppose it may be possible to fiddle with the
function object's __dict__ to try to make it subscriptable, but it
would be an unusual hack.

However, if x is an object from a user created class X, then x[y] is
syntactic sugar for:

X.__getitem__(x, key)

where key may be just about anything; a integer, a string, a list of
strings, etc.  Of course, your object should be prepared to handle the
key, which in Drew's T.p["foo", "bar"] example, seems to mean
consolidating a sequence.


> A snippet from Dennis:
>  >>                  html += "<td><a href=edt_main.py?slct=edit"
>  >>                  html +=     "&class=tree_open32"
>  >>                  html +=     "&index=%s"       % keye
>  >>                  html +=     "&act=%s"         % act
>  >>                  html +=     "><%s></a></td>" % hovered_img(ht_obj, index)
> The equivalent snippet from Drew
>  >                  cols = [T.td[T.a(href=("edt_main.py?slct=edit"
>  >                                         "&class=tree_open32"
>  >                                         "&index=", keye,
>  >                                         "&act=", act)
>  >                                   )[hovered_img(ht_obj, index)]],
> I guess the nesting take some getting used to.  Stripping all the contained
> wording, Drew's version above becomes
>         cols = [[(())[()]],
> where the opening "[" is not balanced because it is closed later>  Compare
> the equivalent stripped version of Dennis's code
>         html += "<td><a><%s></a></td>"

I think to be fair, I'd say Drew's stripped version is something like:

            cols = [T.td[T.a(href="")], blahblahblah]

ie. the html structure is more than the parens or square brackets.
And building the href in place
adds to the visual complexity in both examples.

It seems to me, just like when deciding whether to use an Expat or DOM
parser, I'd choose a method based on how I want to manipulate the
structure of the document before finally generating it.


More information about the Baypiggies mailing list