Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Match/Case not condition

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 this was rejected confirming it is not possible!
https://www.python.org/dev/peps/pep-0622/#negative-match-patterns

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading