[Python-Dev] Acquire/release functionality (Was: Extended Function syntax)

Bernhard Herzog bh@intevation.de
03 Feb 2003 12:58:12 +0100


Paul Moore <lists@morpheus.demon.co.uk> writes:

> So, to offer a unified proposal, I'd suggest the following syntax:
> 
> with [ var1 [, var2, ... ] = ] expr1 [, expr2 , ... ]:
>     suite
> 
> This is equivalent to
> 
> var1, var2, ... = expr1, expr2, ...
> 
> if hasattr(var1, "__enter__"):
>     var1.__enter__()
> if hasattr(var2, "__enter__"):
>     var2.__enter__()
> ...
> 
> try:
>     try:
>         suite
> 
>     except:
>         # Handwave here for now
> 
> finally:
>     ...
>     var2.__exit__()
>     var1.__exit__()

IMO if you want multiple controllers for a with statement you should
just nest the withs:

with var1 = expr1:
    with var2 = expr2:
        suite

This makes the order in which the methods of the varN will be called
obvious.

> In this, any varN can be omitted and a dummy hidden variable is
> used. 

Meaning something like

with var1, , var3 = expr1, expr2, expr3:
    suite

I guess. Urgs. You can't normally omit assignment targets on the left
hand side of a sequence unpacking in python. I don't see why this should
be allowed for with. Nested withs handle this in a cleaner way as well,
IMO.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/