Proposal for new operators to python that add syntactic sugar for hierarcical data.

Ben Finney bignose+hates-spam at
Thu May 18 00:40:05 CEST 2006

"glomde" <tbrkic at> writes:

> With element tree package.
>   # build a tree structure
>   root = ET.Element("html")
>   head = ET.SubElement(root, "head")
>   title = ET.SubElement(head, "title")
>   title.text = "Page Title"
>   body = ET.SubElement(root, "body")
>   body.set("bgcolor", "#ffffff")
>   body.text = "Hello, World!"
> With syntactical sugar:
>   # build a tree structure
>   root = ET.Element("html")
>   *!*root:
>      *!*head("head"):
>          *!*title("title):
>               *=*text = "Page Title"
>      *!*body("body"):
>               *=*bgcolor = "#ffffff"
>               *=*text = "Hello, World!"

We already have syntax for building hierarchical data structures:
lists and/or tuples. If you want to define Node and Attribute classes,
you can already do so without adding new syntax.

> I think that with the added syntax you get better view of the html
> page.

I think indenting our existing data type syntax can do the same thing:

    root = Node("html", children=[
        Node("head", children=[
            Node("title", children=[
                "Page Title",
        Node("body", children=[
            Attribute("bgcolor", "white"),
            "Hello, World!",

Set up the __init__ for those classes to do whatever you would have
done with the syntax you proposed.

> Repeating things dissapears and you get indentation that corresponds
> to the tree.


> I think it is very pythonic IMHO.

Adding new punctuation to deal with a particular type is extremely

 \     "The cost of a thing is the amount of what I call life which is |
  `\       required to be exchanged for it, immediately or in the long |
_o__)                                    run."  -- Henry David Thoreau |
Ben Finney

More information about the Python-list mailing list