Skip to content
Discussion options

You must be logged in to vote

Yes — the OR-pattern is the idiomatic form:

match role:
    case "admin":
        print("Full access")
    case "editor" | "author":
        print("Write access")
    case "guest":
        print("Read access")
    case _:
        print("No access")

Two things worth pinning while you're learning match:

  • case "editor" | "author": is an OR-pattern — matches if any alternative matches. This is the construct you reach for by default.
  • The common mistake is case ("editor", "author"): — parentheses without | form a tuple literal pattern, which matches an actual 2-tuple whose elements are those strings — i.e., it matches ("editor", "author"), never the string "editor". It fails silently in tests (…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dab00gieman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants