L O A D I N G
blog banner

Python 3.10 – what can we expect from it?

Image above shows how CASE statement can be implemented in Python, wait you cant make it work today but surely can after 4th October, that is after Python 3.10 is made available for all of us. In the 3.10 version, we will see some of the most fascinating features like structural pattern matching, parenthesized context managers, more typing, and improved error messages to make our life easier.

In 2020, Guido van Rossum, the creator of Python, committed the first documentation showing the new switch-statements, which have been named Structural Pattern Matching. Python is not using the switch keyword for its Structural Pattern Matching as other programming languages do, instead, it will be using match keywords. So it might be possible that we would refer to it as a Python match case statement instead of a Python switch case.

The new structural pattern matching will help us to match variables against one or even one of a set of possible values. This will no doubt be very powerful feature and will make it possible to write code for a variety of scenarios. One example is offcourse the code added as an image above. Lets look at another example:

Let’s try to use match case Statement and implement the weekday calculator as example:

def weekday(day):
    match day:
        case 1:
            return "Sunday"
        case 2:
            return "Monday"
        case 3:
            return "Tuesday"
        case 4:
            return "Wednesday"
        case 5:
            return "Thursday"
        case 6:
            return "Friday"
        case 7:
            return "Saturday"
        #wildcard case
         case _:
            return "Please Enter a Valid Day Number"

 print(weekday(1))   #Sunday
 print(weekday(4))  #Wednesday
 print(weekday(7))  #Saturday
 print(weekday(11)) #Please Enter a Valid Day Number

Impressed? There is more to come. Using, what is called as structural pattern matching, we can perform the same match-case logic even based on whether the structure of our comparison object matches a given pattern.

To demonstrate, lets take two dictionaries:

dict_a = {
    'id': 1,
    'meta': {
        'source': 'abc',
        'location': 'west'
    }
}

dict_b = {
    'id': 2,
    'source': 'def',
    'location': 'west'
}

 We could write a pattern to match dict_a like:

{
    'id': int,
    'meta': {'source': str,
             'location': str}
}

A pattern to match dict_b like:

 {    'id': int,
    'source': str,
    'location': str
}

We can now put both of these together in a match-case statement:

# loop through both dictionaries and a 'test'
for d in [dict_a, dict_b, 'test']:
   match d:
       case {'id': ident,
             'meta': {'source': source,
                      'location': loc}}:
           print(ident, source, loc)
       case {'id': ident,
             'source': source,
             'location': loc}:
           print(ident, source, loc)
       case _:
           print('no match')

Watch out for my next few articles in which I will cover:

  • Parenthesized context managers
  • More Python typing features
  • Display better error messages

Stay tuned. Do comment here if you liked this article.

Leave a Reply

Your email address will not be published.