[Python-Dev] Acquire/release functionality (Was: Extended Function syntax)

Walter Dörwald walter@livinglogic.de
Mon, 03 Feb 2003 12:47:28 +0100


Paul Moore wrote:

> Guido van Rossum <guido@python.org> writes:
> 
> 
>>I'm now wondering if there aren't really only two interesting cases,
>>none of which require saving the thunk and calling it later:
>>
>>- synchronized-like, where the block should connect to its environment
>>
>>- property-like, where the block should introduce a new scope, and the
>>  caller should be able to inspect or control that scope's namespace
> 
> 
> I certainly haven't seen any use cases other than these two. In fact,
> pretty much all of the cases I am aware of are synchronised-like,
> which I've been referring to as acquire/release. The only
> property-like cases I'm aware of are properties themselves.

A use case I can think of is some kind of XML DOM API (Ruby does
something like that with it's cgi module, where node content is
created inside a block).

With the current Python you can have something like that:

    node = html.table(
       html.tr(
          html.th("foo"),
          html.td("bar")
       ),
       html.tr(
          html.th("spam"),
          html.td("eggs")
       ),
       border=0,
       cellpadding=3
    )

i.e. element classes that use * and ** arguments in their constructor
to collect content and attribute nodes.

The problem with this approach is, that the attributes are maximally
separated from their element and that control structures (like if) are 
not possible inside the constructor.

With blocks ifs and fors could look like this:

    node = html.table(border=0, cellpadding=0):
       html.tr():
          html.th(): "foo"
          html.td(): "bar"
       if cond:
          html.tr():
             html.th(): "spam"
             html.td(): "eggs"
       for i in xrange(10):
          html.tr():
             html.th(): i
             html.td(): i*i

Bye,
    Walter Dörwald