[Python-Dev] Pre-PEP: with syntax (was: New syntax threads -- summary wanted)

Samuele Pedroni pedronis@bluewin.ch
Mon, 10 Feb 2003 16:36:49 +0100


>'with' [ var '=' ] expr ':'
>    suite
>
>    This statement is defined as being equivalent to the following
>    sequence of statements:
>
>var = expr
>
>if hasattr(var, "__enter__"):
>    var.__enter__()
>
>try:
>    suite
>
>finally:
>    if hasattr(var, "__exit__"):
>var.__exit__()
>
>    If the variable is omitted, an unnamed object is allocated on the
>    stack.  In that case, the suite has no access to the unnamed object.

I would not really depend when var is specified, that its binding is not
modified in suite,
I would store expr result away and use that:

var = _secret = expr

... rest of impl uses _secret not var ...

Less fragile.

regards