(What version of Python are you using, a 2.6 build? In 2.5 release, a file() is not a context manager, so 'with file() as f:' doesn't work. And in py3k the "except E, N" syntax is gone.)except blocks don't set up a new scope to me. [...second code example deleted...] says the same thing about the with statement to me.
So the intent for "except E as N" is that N does *not* outlive the "except" block. And indeed that's what happens in my month-old Py3k.try: try_body except E as N: except_body ...
gets translated to (in Python 2.5 terms)try: try_body except E, N: try: except_body finally: N = None del N ... http://www.python.org/dev/peps/pep-3110/
Clearly the original intent with "as" was that it was specifically *not* direct assignment. Python added a *new keyword*, specifically because this was not direct inline assignment. So I assert a Python programmer should not see "as" and think "= with the two sides swapped".So now the final hurdle was that the PEP 310 syntax: with VAR = EXPR: BLOCK1 would be deceptive, since VAR does *not* receive the value of EXPR. Borrowing from PEP 340, it was an easy step to: with EXPR as VAR: BLOCK1http://www.python.org/dev/peps/pep-0343/
a = b = file(...)But this does not:
if a = expression:Since nobody likes to type, I'm sure people have requested inline assignment zillions of times before. Since Python does not support it, I'm guessing Guido doesn't want it for some reason. Perhaps it's to save programmers from the inevitable heartache of typing "if a = x" when they meant "if a == x" (or vice-versa); if so then that's a little surprising, considering the "consenting adults" design tenet, but at least "if E as N" would not have that problem. If there was some other reason, then I bet "if E as N" doesn't address it.
# hooray, expression worked
# boo, expression failed