[Python-Dev] anonymous blocks

Brian Sabbey sabbey at u.washington.edu
Tue Apr 19 22:46:16 CEST 2005


Guido van Rossum wrote:
>> @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)

Why not have the block automatically be inserted into acquire's argument 
list?  It would probably get annoying to have to define inner functions 
like that every time one simply wants to use arguments.  For example:

def acquire(block, aLock):
         aLock.acquire()
         try:
                 block()
         finally:
                 aLock.release()

@acquire(myLock):
 	code
 	code
 	code

Of course, augmenting the argument list in that way would be different 
than the behavior of decorators as they are now.

-Brian


More information about the Python-Dev mailing list