A Perfect Match

A Python 3.10 Brain Teaser

Miki Tebeka
The Pragmatic Programmers
2 min readOct 18, 2021

--

Photo by Sigmund on Unsplash

📚 Connect with us. Want to hear what’s new at The Pragmatic Bookshelf? Sign up for our newsletter. You’ll be the first to know about author speaking engagements, books in beta, new books in print, and promo codes that give you discounts of up to 40 percent.

Trying Out Matching

Python 3.10 introduced a cool new match operator. You decide to use it in the board game that you’re writing:

Then you try it out (got to love the REPL):

Hmm… 5 is way less than 100 (MAX_X), why this result?

Then you remember that match is structural pattern matching, not a switch statement — it matches by structure, not by value. So the first case will match any 2 item tuple. In your case, it will set MAX_X to 5 and MAX_Y to 100 and…

--

--