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

Jim Jewett jimjjewett at gmail.com
Mon Mar 7 21:10:25 CET 2011

On Mon, Mar 7, 2011 at 11:33 AM, Guido van Rossum <guido at python.org> wrote:
>>>> On Sat, Mar 5, 2011 at 10:27 PM, Carl M. Johnson wrote:>

>>>> > People have made the with statement
>>>> > into an XML generator

>>>> > <http://langexplr.blogspot.com/2009/02/writing-xml-with-ironpython-xmlwriter.html>

> I don't think it's fine. The quoted example looks
> like it depends too much on implicit side effects.

What side effects?

> I find it useful when I can clearly visualize the
> alternative code, which typically involves extra flow
> control such as a try/finally block.

I had thought of the finally as the element-close tags...

> In this example it looks more like the
> alternative is just more calls.

Sure, but the close tag comes arbitrarily later, just like the close
of a file -- which was one of the canonical use cases.

> ... I just hacked together a prototype for an HTML
> generating library which uses nested function calls
> instead of nested with-statements to represent
> the nested structure of HTML. I think I like that
> approach better because it makes it easier to do
> some parts of the generation out of  order: I can
> construct various sub-lists of HTML elements
> and then splice them together. E.g.
> row1 = []
> row2 = []
> for x in <something>:
>  row1.append(libhtml.td(f1(x)))
>  row2.append(libhtml.td(f2(x)))
> t = libhtml.table(libhtml.tr(row1), libhtml.tr(row2))

I think the main appeal of the above library is that you *don't* have
to follow this pattern -- you can create the broad outline before you
have the innermost details.


More information about the Python-ideas mailing list