[Python-Dev] anonymous blocks

Barry Warsaw barry at python.org
Tue Apr 19 22:18:41 CEST 2005


On Tue, 2005-04-19 at 15:24, Guido van Rossum wrote:

> *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()

Indeed, it would be very cool to have these kind of (dare I say) block
decorators for managing resources.  The really nice thing about that is
when I have to protect multiple resources in a safe, but clean way
inside a single block.  Too many nested try/finally's cause you to
either get sloppy, or really ugly (or both!).

RSMotD (random stupid musing of the day): so I wonder if the decorator
syntax couldn't be extended for this kind of thing.

@acquire(myLock):
    code
    code
    code

-Barry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 307 bytes
Desc: This is a digitally signed message part
Url : http://mail.python.org/pipermail/python-dev/attachments/20050419/c2780ce7/attachment.pgp


More information about the Python-Dev mailing list