The new Python match

Exploring the new Python match statement, Python’s implementation of switch/case.

If you’ve decided to learn Python [1] and have any experience with other programming languages, you’ll quickly notice that the ubiquitous switch statement is nowhere to be found. Soon, though, that will no longer be the case (sort of). Python 3.10 is slated to be released in October 2021 and includes the new match command [2] [3].

Switcheroo

The function of switch is akin to trying to find a particular office in an office building. As you walk down the hallway, you look at each door to see if it displays the number or name in which you are interested. When you find it, you stop searching and go inside. In C (and indeed many other languages) switch allows you to compare a value against a set of others (Listing 1).

The switch starts the compare operation, with the value you want to check passed in. The sets of curly braces then contain your case statements.

[…]

Use Express-Checkout link below to read the full article (PDF).

Posted by Contributor