replacing `else` with `then` in `for` and `try`
Steve D'Aprano
steve+python at pearwood.info
Thu Nov 2 01:04:05 EDT 2017
On Thu, 2 Nov 2017 12:50 pm, Ben Bacarisse wrote:
> Steve D'Aprano <steve+python at pearwood.info> writes:
>
>> On Thu, 2 Nov 2017 08:12 am, Alexey Muranov wrote:
>>
>>> what do you think about the idea of replacing "`else`" with "`then`" in
>>> the contexts of `for` and `try`?
[...]
> Re-using finally would not need a new keyword and might be close enough
> in meaning.
Reusing finally would be *completely* wrong.
The semantics of `finally` is that it should be executed no matter[1] how you
exit the previous block. E.g. if we write:
try:
return 1
finally:
print("exiting")
then "exiting" is printed. Replace the return with a raise, and the same
applies. Reusing `finally` in for and while loops would imply the similar
behaviour:
for i in range(100):
return i
finally:
print("exiting")
should print "exiting", when in fact it does not. Likewise if you replace the
return with a break.
[1] Within the bounds of normal processing. There are ways to halt Python
without executing any subsequent code, namely os._exit, and os.abort dumps
core to exit immediately.
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list