And another message that was rejected (I sent from an unregistered email
address)
On Sat, Jul 27, 2019 at 1:49 AM Serhiy Storchaka <storchaka@gmail.com>
wrote:
26.07.19 21:52, Bruce Leban пише:
To put this in a simpler way: the proposal is to add an except clause that
applies ONLY to the direct operation of the with or for statement and not
to the block. That's an interesting idea.
The one thing I find confusing about your proposal is that the proposed
syntax does not imply the behavior. In a try statement, the except appears
at the end and after all possible statements that it could cover. The
proposal mimics that syntax but with different semantics. Something like
this would be much more clear what is going on:
for VARIABLE in EXPRESSION:
except EXCEPTION:
BLOCK
BLOCK
with EXPRESSION as VARIABLE:
except EXCEPTION:
BLOCK
BLOCK
while EXPRESSION:
except EXCEPTION:
BLOCK
BLOCK
Besides an unusual for Python layout (a clause has different indentation
than the initial clause of the statement to which it belongs) there is
other problem. The exception block is not the part of the "for" or "with"
block. After handling an exception in the "for" clause you do not continue
to execute the "for" block, but leave the loop. After handling an exception
in the "with" clause you do not continue to execute the "with" block and do
not call `__exit__` when leave it. To me, this syntax is much more
confusing than my initial proposition.
And I find it less confusing. And neither of those is the standard to use.
The goal is for syntax to imply semantics (which my proposal does and I do
not think yours does, given several people commenting that they thought it
applied to the entire loop) and to choose syntax that is more clear to more
people (which requires more than two peoples' opinions).
Consider how you would write this if everything was an expression in Python
and we had braces:
for VAR in ( EXPR except EXCEPTION: { BLOCK; break; } ):
BLOCK
I do agree that it is not obvious that the exception block breaks out of
the loop. I think in actual code it will be fairly obvious what's happening
as continuing into the loop when the loop expression through an expression
doesn't make sense. I'm open to alternatives. On the other hand, an except
clause at the bottom of the loop that does not apply to the loop body is
going to catch me every time I see it.
--- Bruce