Ceci est une ancienne révision du document !
As many of you know or have at least heard, Python 3.10 is well on its way. According to python.org (https://www.python.org/dev/peps/pep-0619/), the final date for release is slated for October 4, 2021 with feature freeze scheduled for May 3, 2021. Here is the release schedule as of April 2021: • 3.10.0 alpha 7: Tuesday, 2021-04-06 Expected: • 3.10.0 beta 1: Monday, 2021-05-03 (No new features beyond this point.) • 3.10.0 beta 2: Tuesday, 2021-05-25 • 3.10.0 beta 3: Thursday, 2021-06-17 • 3.10.0 beta 4: Saturday, 2021-07-10 • 3.10.0 candidate 1: Monday, 2021-08-02 • 3.10.0 candidate 2: Monday, 2021-09-06 (if necessary) • 3.10.0 final: Monday, 2021-10-04
Just because we have over 5 months before the actual release is due, that doesn’t mean that you can’t start getting prepared and excited about the upcoming new version. Some of the changes that are supposed to be coming are: • PEP 604 – Allow writing union types as X | Y • PEP 612 – Parameter Specification Variables • PEP 626 – Precise line numbers for debugging and other tools • PEP 597 – Add optional EncodingWarning • PEP 618 – Add Optional Length-Checking To zip • PEP 644 – Require OpenSSL 1.1.1 or newer • PEP 632 – Deprecate distutils module • PEP 613 – Explicit Type Aliases And I’ve saved the best for last - • PEP 634, PEP 635 Structural Pattern Matching
Each of the new features that are listed above have the PEP (Python Enhancement Proposals) numbers with them so you can easily do a web search for the ones you are interested in exploring. For the rest of the article, I will be discussing the new Pattern Matching feature. If you want to try out any of these things, hopefully you have pyenv installed. That way you can simply do a pyenv install 3.10-dev to play with version 3.10. There are other ways to try out 3.10, but honestly pyenv is the way to go! If you would rather check out another way, try looking at https://towardsdatascience.com/all-the-important-features-and-changes-in-python-3-10-e3d1fe542fbf and search for “Installing Alpha/Beta Version”. They provide a way to load version 3.10 alongside your existing Python version(s).
Pattern Matching You might be wondering why I’m making such a point to talk about this single item. Well, a small history lesson is in order here. For many years, programmers have been asking for Python to support Select Case (VB) or Switch Case (C#) type statements. They have been saying that it makes code much easier to read. An example of such a statement in Visual Basic would be something like this (below). Each time the subject has come up, those who make decisions about Python have said that there is no need for that. The standard if | elif | else structure works just fine (below).
While the Visual Basic structure might look a bit more efficient, both are (in my humble opinion) equally easy to read. That having been said, I came from a Visual Basic background and I have to admit that when I was first learning Python, I too wondered why such a simple solution like the Select Case type testing wasn’t supported. However, I got over it and learned the Python way. Python 3.10 introduces something called Structural Pattern Matching. At first glance, it is the same thing as the Select Case for VB and Switch Case for C#. Here’s the same code as above, but done with the Pattern Matching. (Top right. By the way, this won’t work without being in Python 3.10.)
When it runs, it returns: 0 Not between 1 and 10, inclusive 1 is between 1 and 5, inclusive 2 is between 1 and 5, inclusive 3 is between 1 and 5, inclusive 4 is between 1 and 5, inclusive 5 is between 1 and 5, inclusive 6 is between 6 and 8, inclusive 7 is between 6 and 8, inclusive 8 is between 6 and 8, inclusive 9 is equal to 9 or 10 10 is equal to 9 or 10 11 Not between 1 and 10, inclusive Honestly, in this example, it doesn’t look that much better than the Python if | elif version. That’s because while you can do simple things, there is no pattern to match in that case, really.
On the other hand, let’s look at an example of how to make this new functionality really shine. We’ll throw together a really limited and simple text adventure game. Surely you remember those, unless you are younger than 30 years old or so. The typical text adventure drops the player in the middle of a forest (usually after a night of enjoying copious amounts of adult beverages), or into a dark room inside of a castle. Since Ronnie is Scotish and both sides of my family tree shares that same root, I’ll pick the first option for our adventure. We’ll make our player a male human (since we all know that female humans are smarter than to enjoy copious amounts of adult beverages, right?).
Now, our player is in a forest with a bit of a headache. There are only four directions that he can move; North, West, South and East. We will assume that the paths available to him won’t normally run him into a tree. I mean, he already has a hangover – let’s not make it any worse. Scattered throughout his journey, he will stumble over things like a sword, an axe, some gold, some silver, some diamonds, flint and steel to start a fire, and so on. I won’t be nice enough to let him find some aspirin, just because he should suffer some for his evening’s indulgences. He also will possibly run into a monster or two. Now that we’ve defined his world somewhat, let’s start looking at writing some code. We’ll need to create a couple of lists to handle the objects in the game that the player can deal with and to “hold” the items he’s picked up (above).
Our main loop (below) will be fairly simple. As long as the game_running variable is True, then we will continue to loop. At the top of the loop, we ask the user what they want to do and get their response. We send that to the function work_command. Inside the work_command function, we will use the new match case function. We’ll start pretty simply (top right). I’ve defined the global variables here that I think might be used. When we start the match case section, we set the match statement to look at the command that has been split apart with the .split() command. This way, the case statements can look for commands like “quit” as well as “go North” and even things more complicated like “get Axe”. Let’s add another case for moving towards the North.
case[“North”] | [“go”, “North”]: message = “Ok. Going North” print(message) So in this case (no pun intended), if the user enters the command “North” or “go North”, it will fall into the above case. This allows us to handle multiple options. However, we can even get more complex. In the next version, we can mix the “go” command with an set of directions that will allow us to encapsulate the go command with a direction, use the pattern of ‘North’ | ‘South’ | ‘East’ | ‘West’, assign that to the variable direction, and test that to handle any of the four options.
Then at the bottom of the test for the four directions, we can make a test, just in case the player decided to get cute, and enter something like “go Up”. The *wth wildcard will catch anything that isn’t part of our four directions. We also can handle things like this (next page, top left). In this instance, the user can type “get Axe” or “pick up Sword” or even “pick shovel up”. So when the program hits this part of the code, the object is then added to the players “bag o’ infinite stuff” by the append command. When the player wants to see the contents of his bag, he just enters “inventory” which displays the contents of the list.
Pick up Axe ~~~~~~~~~~~~~~~~~~~~~~~~~ You currently have 1 objects in your bag: bag of stuff Axe ~~~~~~~~~~~~~~~~~~~~~~~~~ I have to be honest with you, the examples that I have provided here only scratch the surface of the Structural Pattern Matching feature and are very simplistic. For more information and examples, you can check out the following web sites… https://www.python.org/dev/peps/pep-0636/#adding-conditions-to-patterns https://mathspp.com/blog/pydonts/pattern-matching-tutorial-for-pythonic-code
As things get a bit further along with the beta version, I’m sure you will see a large amount of web information about all of the version 3.10 features – especially the Pattern Matching feature. I’ll put the Python code that I provided in the article on my github repository at https://github.com/gregwa1953/FCM-168, and I’ll make the text adventure game code closer to usable as a skeleton for you if you want to learn. Until next time, as always; stay safe, healthy, positive and creative!