-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Description
Feature or enhancement
Proposal:
I'm proposing a new control flow keyword—similar in concept to finally
—that would be used after a chain of if
and elif
statements. The idea is for this block to run only if one of the conditions was met (i.e. one of the if
/elif
blocks executed). If none of the conditions were true, the block would not run.
Since finally
is the closest existing concept I could think of, I’ll refer to it in the example like this:
if cond1:
# do A
elif cond2:
# do B
finally:
# do C (Only runs if cond1 or cond2 matched)
# do D (Basically always runs, if conditions where met or not)
Currently, to achieve this behavior in Python, you'd have to do something like:
matched = False
if cond1:
# do A
matched = True
elif cond2:
# do B
matched = True
if matched:
# do C (Only runs if cond1 or cond2 matched)
# do D (Basically always runs, if conditions where met or not)
I'm not sure if finally
is the best name for this, since it's already associated with try/except blocks. But I’m confident that if the idea has merit, the Python community can come up with a better keyword.
There’s also a question of how this would interact with an else
clause after the if
/elif
chain—maybe it’s incompatible, or maybe it could be made to work.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response