[Python-Dev] Re: anonymous blocks

Terry Reedy tjreedy at udel.edu
Fri Apr 22 04:30:22 CEST 2005


I do not know that I have ever needed 'anonymous blocks', and I have 
therefore not followed this discussion in detail, but I appreciate Python's 
beauty and want to see it maintained.  So I have three comments and 
yet-another syntax  proposal that I do not remember seeing (but could have 
missed).

1. Python's integration of for loops, iterators, and generators are, to me, 
a gem of program language design that distinguishes Python from other 
languages I have used.  Using them to not iterate but to do something else 
may be cute, but in a perverted sort of way.  I would rather have 
'something else' done some other way.

2. General-purpose passable block objects with parameters look a lot like 
general-purpose anonymous functions ('full lambdas').  I bet they would be 
used a such if at all possible.  This seems to me like the wrong direction.

3. The specific use-cases for Python not handled better by current syntax 
seem to be rather specialized: resource management around a block.  So I 
cautiously propose:

with <resource type> <specific resource>: <suite>

with the exact semantics dependent on <resource type>.  In particular:

with lock somelock:
    codeblock

could abbreviate and mean

somelock.acquire()
try:
    codeblock
finally:
    somelock.release()

(Guido's example).

with file somefile:
    codeblock

might translate to (the bytecode equivalent of)

if isinstance(somefile, basestring?):
    somefile = open(somefile,defaults)
codeblock
somefile.close

The compound keywords could be 'underscored' but I presume they could be 
parsed as is, much like 'not in'.

Terry J. Reedy





More information about the Python-Dev mailing list