The new Python match
Just in Case
Lines 24-33 define make3Dpoint
, where the match
statement resides, in this case to pt
. The first case
statement on line 26 is looking for two variables in a tuple. If a tuple with two elements is received, this case matches, and x
and y
are assigned to the variables of the tuple. The function closes by returning a Point3D
instance, passing x
and y
, along with
, because a z
value was not received.
The second case
statement on line 28 works the same way, except it matches a three-member tuple. The third case
statement on line 30 begins to get even more interesting. Here, the case
matches if pt
is an instance of Point2D
and x
and y
are defined. In this case, x
and y
will be assigned to their values from the instance of Point2D
that is currently being evaluated; then, a Point3D
is created with the existing x
and y
, with z
as
. This x
and y
mapping derives from __match_args__
.
The final case
statement checks for an instance of Point3D
and has three positional arguments. An underscore (_
) is a match
wildcard. In that case, it has already been handed a Point3D
, so it just returns it unmodified.
Line 35 defines pointList
as a list
, and then lines 36-39 append
different sized tuples and class instances to the list of things to be processed. Line 41 loops over pointList
; line 42 calls make3Dpoint
which has the match
statement; and line 43 calls the print
method on the resulting 3D point (Figure 1).
Nine lines of code have normalized four different styles of input and received uniform objects. With this new syntax, you can take all sorts of input and pass them through a single function that returns consistent objects for further work.
Guards
The match
statement also includes an additional check called a guard, which is a conditional statement that can further evaluate the input being checked in a case
. Consider the "guess a number" game (Listing 3). Here, the program chooses a number between 0 and 100, and you must figure it out in as few guesses as possible.
Listing 3
numberGuess.py
01 import random 02 03 target = random.randint ( 0 , 100 ) 04 guessCount = 0 05 gameOver = False 06 07 def checkGuess ( guess ): 08 global target 09 global guessCount 10 global gameOver 11 12 match guess: 13 case ( i ) if i < 0 or i > 100: 14 print ( "Guess out of range, game is from 0 to 100" ) 15 case ( i ) if i < target: 16 print ( "Higher!" ) 17 guessCount += 1 18 case ( i ) if i > target: 19 print ( "Lower!" ) 20 guessCount += 1 21 case ( i ) if i == target: 22 print ( "Got it!" ) 23 gameOver = True 24 25 while gameOver == False: 26 print ( "You've guessed {0} times".format ( guessCount ) ) 27 guess = int ( input ( "Your guess:" ) ) 28 checkGuess ( guess )
The code begins with an import
of the random
library and then creates the random integer target
between 0 and 100 and initializes a couple of other variables: guessCount
, the number of guesses that have been made, and gameOver
, which indicates whether the game is finished.
Guesses
The checkGuess
function (lines 7-23) contains the match
statement, but first, lines 8-10 use the global
keyword to bring in the variables defined earlier so they are accessible inside the function without passing them in each time.
Line 12 starts the match
on guess
, which is the player's guess. Notice that all of the case
statements use i
instead of guess
, and because match
includes variable assignment, guess
becomes i
.
You'll also notice that each case
checks i
but then is followed by if
. These if
statements are called guards, and they create extra conditions for the item to match. The first case
(line 13) checks whether i
is less than 0 or greater than 100. If so, a Guess out of range message is displayed.
The second (line 15) and third (line 18) case
statements determine whether the guess is too high or too low, display an appropriate message, increment guessCount
, and exit the match
.
The last case
(line 21) checks to see whether the correct number has been guessed. If so, it prints the Got it! message and sets gameOver
to True
to exit the main loop.
Line 25 sets up the main loop (Figure 2), watching for gameOver
to become True
. Until then, it prints the number of guesses (line 26), asks for a guess (line 27), and checks it with the checkGuess
function (line 28).
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.