On 02/11/2017 01:06, Steve D'Aprano wrote:
On Thu, 2 Nov 2017 08:12 am, Alexey Muranov wrote:
Hello,
what do you think about the idea of replacing "`else`" with "`then`" in
the contexts of `for` and `try`?
Yes, this, exactly!!!
(For while and for loops, but not try -- see below.)
I have argued this for many years. The current choice of "else" is painfully
misleading, and it causes people (including myself) to wrongly guess that
the "else" block runs only if the for/while block doesn't run at all:
# This is wrong!
for x in sequence:
...
else:
print("sequence is empty")
The actually semantics of "else" is that the block is UNCONDITIONALLY run
after the for/while loop completes,
/If/ it completes, and /when/ it completes. Otherwise why bother with
using 'else'? Just have the code immediately following the loop.
And there are some circumstances where the 'else' part is never executed.
But if people prefer a different keyword, then why not? I think 'then'
can be used, without impacting its use as an identifier, because it will
always be followed by ":". Of course you would need to allow both "else"
and "then" for backwards compatibility.
--
bartc
--
https://mail.python.org/mailman/listinfo/python-list