[Python-Dev] anonymous blocks

Guido van Rossum gvanrossum at gmail.com
Tue Apr 19 21:24:25 CEST 2005


> See the thread "pre-PEP: Suite-Based Keywords" (shamless plug)
> (an earlier, similar proposal is here:
> http://groups.google.co.uk/groups?selm=mailman.403.1105274631.22381.python-list
> %40python.org ).
> 
> In short, if doFoo is defined like:
> 
> def doFoo(func1, func2):
>         pass
> 
> You would be able to call it like:
> 
> doFoo(**):
>         def func1(a, b):
>                 return a + b
>         def func2(c, d):
>                 return c + d
> 
> That is, a suite can be used to define keyword arguments.

I'm still not sure how this is particularly solving a pressing problem
that isn't solved by putting the function definitions in front of the
call. I saw the first version of the proto-PEP and didn't think that
the motivating example (keeping the getx/setx methods passed to a
property definition out of the class namespace) was all that valuable.

Two more issues:

(1) It seems that *every* name introduced in the block automatically
becomes a keyword argument. This looks like a problem, since you could
easily need temporary variables there. (I don't see that a problem
with class bodies because the typical use there is only method and
property definitions and the occasional instance variable default.)

(2) This seems to be attaching a block to a specific function call but
there are more general cases: e.g. you might want to assign the return
value of doFoo() to a variable, or you might want to pass it as an
argument to another call.

*If* we're going to create syntax for anonymous blocks, I think the
primary use case ought to be cleanup operations to replace try/finally
blocks for locking and similar things. I'd love to have syntactical
support so I can write

blahblah(myLock):
    code
    code
    code

instead of

myLock.acquire()
try:
    code
    code
    code
finally:
    myLock.release()

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list