[Python-ideas] Fwd: About calling syntax

Scott Dial scott+python-ideas at scottdial.com
Thu Sep 11 11:02:05 CEST 2008


Zaur Shibzoukhov wrote:
> In current syntax the only available form (which is natural to me) is:
> 
> Element('root',
>    x='1', y='2', *[
>    Element('child1',
>       x='1', y='2', *[
>       Element(''grandchild1', x='4', y='6'),
>       Element(''grandchild2', x='7', y='8')
>    ]),
>    Element(''grandchild1', x='4', y='6')
> ])

That doesn't look so bad. I wonder what's so bad with give "Element" an
"add_child" method..

class Element(object):
    def __init__(self, name, **attrs):
        self.name = name
        self.attrs = attrs
        self.children = []

    def add_child(*children):
        self.children.extend(children)
        return self

Element('root', x='1', y='2').add_child(
    Element('child1', x='1', y='2').add_child(
        Element('grandchild1', x='4', y='6'),
        Element('grandchild2', x='7', y='8'),
	),
    Element('child2', x='1', y='2'),
)

Presumably you would want to have an add_child() already for other
purposes? Unless there is a compelling reason to not make "add_child()"
a composable function.. So, I don't see the need to start inventing
syntax for such a simple pattern.

-Scott

-- 
Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu



More information about the Python-ideas mailing list