Hacker News new | ask | show | jobs
by bilboa 1952 days ago
Yes, the variable names in the case patterns are treated like variables on the left hand side of an assignment statement. Whatever value they may have had before is irrelevant. A pattern which consists solely of a variable name is a wildcard pattern that matches anything, just as it would be on the left side of an assignment.

Edit: It is possible to use constants or variables in a case pattern, but they have to be dotted names to not be treated like a capture pattern. So this would work for your example:

  class HttpStatus(Enum):
      NOT_FOUND = 404

  match status:
     case HttpStatus.NOT_FOUND:
         return "Not found"
1 comments

Do you know what would happen with:

    def _id(x): return x

    not_found = 404
    match status:
        case _id(not_found):
            return "Not found"