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'
        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?

1 Like

“finally” implies that the clause will always be executed when the code
leaves the match statement, no matter which case branch was taken.

Just as in try…finally, the finally clause is always executed (if
anything is executed at all).

1 Like

This is true, which means finally is not a good choice… default maybe? Whatever is most explicit.

1 Like

I believe adding a new case after a wildcard match is a syntax error.

1 Like

case _ is already close to as explicit as it is possible to get. It is
a case, and it is the wildcard case.

Changing that to “finally” or “default” or “else” or “otherwise” etc is
less explicit, because it is no longer explicity stating that this is a

I think that, rather than “most explicit”, you are asking for wildcards
to use a word rather than a symbol. But symbols can be just as explicit
as words:

x + y

x plus y

are equally explicit. Also, we can be too explicit:

if the boolean expression given by cond or flag is True then execute the following block of Python code:


if cond or flag:

I think that having a symbol for wildcards is explicit enough and
adding a new keyword doesn’t add enough value to be worth it.

1 Like

Those who really want something different can just write case default: instead of case _: (as long as they’re not surprised when the subject is bound to default upon hitting that case). :wink: