PEP 634 - underscore wildcard, or a keyword?

I love the new pattern matching with match/case, and anticipate using it heavily. What I’m not so keen on is the “match everything else” at the end (case _).

Why couldn’t a hard, “final” case be added such as this:

match myval:
    case 1:
        return 'One'
    case 2:
        return 'Two'
    finally:
        return 'No value given'

I feel like this has two benefits:

  1. It matches other control flow that has a “final” case, such as if/elif/else and try/except/finally.
  2. It could enforce that there are no further conditions added after it, that will never be reached… with case _, it would be easy to add another case beneath that without thinking.

case _ could still work, but something like finally would be available and more accepted? maybe?

2 Likes