[Python-Dev] anonymous blocks

Timothy Fitz firemoth at gmail.com
Sat Apr 23 05:42:49 CEST 2005


On 4/21/05, Guido van Rossum <gvanrossum at gmail.com> wrote: 
>    for dummy in synchronized(the_lock):
>        BODY
> 
> or perhaps even (making "for VAR" optional in the for-loop syntax)
> with
> 
>    in synchronized(the_lock):
>        BODY
> 
> Then synchronized() could be written cleanly as follows:
> 
>    def synchronized(lock):
>        lock.acquire()
>        try:
>            yield None
>        finally:
>            lock.release()

How is this different from:

def synchronized(lock):
  def synch_fn(block):
    lock.acquire()
    try:
      block()
    finally:
      lock.release()
  return synch_fn

@synchronized
def foo():
  BLOCK

True, it's non-obvious that foo is being immediately executed, but
regardless I like the way synchronized is defined, and doesn't use
yield (which in my opinion is a non-obvious solution)


More information about the Python-Dev mailing list