Match/Case not condition

Viewed 420

How do I use a not in the new match/case structure?

a = 5
match a:
    case not 10:
        print(a)

This produces an error. How would I correctly syntax this?

1 Answers

I don't think you can use not in structural pattern matching, an alternative is to capture the values you need and then use the default _ case to be the 'not' expression.

a = 5
match a:
    case 10:
        print(a)
    case _:
        print("not 10")

EDIT: I was curious and did some research, turns out a negative match was rejected. https://www.python.org/dev/peps/pep-0622/#negative-match-patterns

Related