[Python-ideas] Make-statement [Re: Different interface for namedtuple?]

Nick Coghlan ncoghlan at gmail.com
Mon Mar 7 09:31:26 CET 2011


On Mon, Mar 7, 2011 at 5:24 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
>
> On Mar 6, 2011, at 10:06 PM, Bruce Leban wrote:
>
> . Personally, the xml writer seems like a reasonable use to me.
>
> I'm surprised that you like the XML writer.  To me it seems much more
> awkward to type the python code than the XML it generates:
> w = XmlWriter.Create(System.Console.Out,XmlWriterSettings(Indent=True))
> x = XWriter(w)
>
> with x.element('tableofcontents'):
> with x.element('section',{'page' : '10'}):
> x.text('Introduction')
> with x.element('section',{'page' : '12'}):
> x.text('Main topic')
> with x.element('section',{'page' : '14'}):
> x.text('Extra topic')

However, appropriate use of keyword arguments would clean that up quite a bit:

def toc(**kwds):
    return x.element('tableofcontents', kwds)

def section(**kwds):
    return x.element('section', kwds)

with toc():
    with section(page='10'):
        x.text(intro_text)
    with section(page='12'):
        x.text(Main topic)
    with section(page='14'):
        x.text(Extra topic)

The CM style also lets you do a lot of nice things like providing
required-by-schema default values, as well as providing conveniences
to make within-document cross references easier to create.

I don't think the linked page is a particularly great exemplar of the
style, but you *can* do good things with the approach.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list