[Python-3000] Brainstorming: literal construction hooks

Guido van Rossum guido at python.org
Sat Apr 22 19:30:15 CEST 2006


On 4/22/06, Josiah Carlson <jcarlson at uci.edu> wrote:
> Nevow.stan has a very similar method for generating *ML with
> (attribute='value')[content], but I've personally found that using only
> function-call semantics to create children and attributes for *ML in
> Python is much more convenient; allowing one to use any of the following
> and get a reasonable method for *ML object re-use:
>     (attribute='value')(content)
>     (content)(attribute='value')
>     (content, attribute='value')

That makes sense, so forget the crazy idea of overloading __getitem__.
The problem with any of these is that  you either have to import (or
define, etc.) an object for each element that you want to name, so you
can write foo(x=1) to mean <foo x="1"/> (this is what my own parsexml
module does, see SF 1337648); or you have to use a helper function or
object, e.g. T.foo(x=1) as in the recipe below.

> A working system for handling this kind of *ML generation is available
> in the ASPN Python Cookbook here (the non-closing tags portion may not
> be correct for any *ML, even HTML):
>     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440563

Cool.

Unfortunately, all of these schemes break down if you have
non-identifier characters in attribute or element names, or if you
want to use namespaces, or if you want to insert entity references,
XML comments, or other funky stuff like DTDs or processing elements.
(I'm sure all of these can be hacked in one way or another but by the
time you've covered all that ground it won't be as pretty, and the
relative advantage over blending XML syntax is diminished.)

Also, an important aspect of JavaScript's E4X standard is a standard
object model. I believe they use a standard DOM style API. In Python
unfortunately we have many different object models for XML -- minidom,
ElementTree, and I believe the XML-sig has its own standard (4Suite?).
I could handle multiple implementations (quack!) but we'd have to pick
an API -- AFAIK ElementTree and the DOM API are incompatible.

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list