for -- else: what was the motivation?
Robert Latest
boblatest at yahoo.com
Mon Oct 10 12:43:16 EDT 2022
Chris Angelico wrote:
> Yes, I'm aware that code readability becomes irrelevant for
> short-duration projects. Beside the point. I'm wondering how important
> it really is to have the shortest block first.
I usually put the most expected / frequent / not negated block first if the
whole if/else statement is not "too long". Sometimes whatever you want to do
becomes pointless if a certain conditions is not met, in which case I do an
early break or return and have no else block at all.
> Given that for-else is an excellent, if rarely-used, construct
I knew it existed but coming from C I never thought to exploit it. I know I
wrote loops like this:
found = None
while not found:
found = search(something)
if found:
break
if not found:
complain()
Need to look into using "else" in these cases.
More information about the Python-list
mailing list