[Python-ideas] "while ... try" - block or "for ... try" - block
Steven D'Aprano
steve at pearwood.info
Thu Jan 12 01:00:20 CET 2012
Manuel Bärenz wrote:
> I propose two new control flows for Python:
>
> "while ... try":
>
> while expr try:
> suite1
> except SomeException:
> suite2
> else:
> suite3
>
> This executes suite1 as long as handled exceptions are thrown and expr
> is True.
This conflates two naturally distinct operations, looping and exception
handling, into a single construct that tries to do both. "Loop" is a natural
operation. "Catch exceptions" is a natural operation. "Loop and catch
exceptions" is not, it is two operations and so should be written as two
operations.
It isn't obvious that handled exceptions remain in the loop. Because there are
two equally good behaviours -- handled exceptions remain in the loop, or
handled exceptions exit the loop -- people will get confused by the construct
and repeatedly be surprised it doesn't do what they expect.
Your proposed syntax doesn't allow the user to write code which couldn't be
written before, it adds no new power or expressiveness to Python. The sole
advantage is that it saves one line and one indent level, which is a trivial
advantage: there are no shortages of either newlines or indents, and if anyone
is writing such deeply nested code that they are regularly worried about
indents, their code almost certainly is in desperate need of refactoring.
Disadvantages include that it increases complexity of the language: the
compiler becomes more complex, there is another feature for people to learn.
Every time somebody writes a loop with a try, they will have to decide whether
to write it the old way or the new way. The try clause can be easily missed
while skimming code, making the construct inelegant. The syntax also clashes
with existing while...else.
What about nested try blocks? If we introduce this, will people try writing this?
while expr try try:
...
except InnerException:
...
...
except OuterException: ...
This seems like a reasonable thing to do. I know that's exactly what I'd try.
It also raises one special case above all other cases. An arbitrary loop
containing a try block looks like this:
while expr:
preamble # 0 or more lines before the try block
try block
postscript # 0 or more lines after the try block
Your proposed syntax only covers the case where both the preamble and the
postscript are 0 lines. What is so special about that case that it needs extra
syntax? It's not that common. Existing syntax is consistent, natural and
obvious. Your proposal looks like you've picked two block constructs, a while
loop and a try block, and nailed them together.
-1 on this suggestion.
[...]
> One further enhancement: If expr is encountered to be False, some
> special exception "NoMoreTriesException" could be raised. It can be
> catched in the same "while ... try" block.
That use-case is already handled in existing Python by the while...else form.
There is no need to add an additional built-in exception for this.
--
Steven
More information about the Python-ideas
mailing list