[Python-Dev] anonymous blocks

Guido van Rossum gvanrossum at gmail.com
Tue Apr 19 22:33:15 CEST 2005


> @acquire(myLock):
>     code
>     code
>     code

It would certainly solve the problem of which keyword to use! :-) And
I think the syntax isn't even ambiguous -- the trailing colon
distinguishes this from the function decorator syntax. I guess it
would morph '@xxx' into "user-defined-keyword".

How would acquire be defined? I guess it could be this, returning a
function that takes a callable as an argument just like other
decorators:

def acquire(aLock):
    def acquirer(block):
        aLock.acquire()
        try:
            block()
        finally:
            aLock.release()
    return acquirer

and the substitution of

@EXPR:
    CODE

would become something like

def __block():
    CODE
EXPR(__block)

I'm not yet sure whether to love or hate it. :-)

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


More information about the Python-Dev mailing list