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
-
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.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs