Python Adopts Pattern Matching - Kitchen Sink Next
Wednesday, 10 February 2021

Python is a remarkable language, but its latest addition is making some programmers thing that maybe it has lost its way - although it really does depend on what you think that way is. Pattern matching is a functional programming construct and Python is only slightly functional.

 An important part, but not the only part, of being functional is having a type system. Pattern matching is essentially extending conditionals to test type, although this is a simplification. Mostly found in functional languages, in the recent past mainstream multiparadigm languages such as C#, Rust and Swift have added pattern matching and now, it seems, that Python is joining the club.

pfsbanner

What you might find surprising is that Python isn't a typed language and yet pattern matching is all about type. In this case, however, this doesn't seem to be a problem as the pattern matching is very general. For example, the tutorial on Python pattern matching gives:

match command.split():
    case ["quit"]:
        print("Goodbye!")
        quit_game()
    case ["look"]:
        current_room.describe()
    case ["get", obj]:
        character.get(obj, current_room)
    case ["go", direction]:
        current_room = current_room.neighbor(direction)
    # The rest of your commands go here

That is, command.split divides up a command into words and the case statement matches on the basis of how many words and what the words are. You can see that this is a very compact notation. Look at the ["get",obj] which only matches two-word commands and only if the first is "get" and passes the second on as obj.

When you first see it, this is very attractive. It seems to be Pythonic in the sense that it has a low surprise factor - even a novice will get it. However, things rapidly get much more sophisticated. You can use wildcards, multiple values, guard values and so on. It all reminds me very much of regular expressions - they start out seeming simple and powerful and end up being complex and mystifying. I'm not at all sure that pattern matching really is Pythonic in the true sense - but then Python allows full regular expressions with no training wheels.

If you read the months of discussion that has gone into this adoption, you will start to realize the many programmers hold strong views on both sides of the fence. There are many edge cases that have not-entirely-convincing fixes and Python has grown to be a big language and there may be interactions we haven't thought about.

Personally I don't think that pattern matching is a bad thing to add to Python, but I can't say that I would argue for it. My reason is that in years of writing Python I haven't needed it and I don't think it would have made much of an impact on my code readability. It seems to be a non-essential with only minor advantages. It does fit in with the life-cycle of a language, however: first its small, compact and lovable; then it adds the parts that a lot of programmers have been missing; then it adds the rest, including the kitchen sink, and settles to a bloated old age waiting for a new compact and simple language to replace it.

python3

 

  • Mike James is the author of Programmer's Python: Everything is an Object published by I/O Press as part of the  I Programmer Library. With the subtitle "Something Completely Different" the book is for those who want to understand the deeper logic in the approach that Python 3 takes to classes and objects.

 

More Information

Acceptance of Pattern Matching PEPs 634, 635, 636, Rejection of PEPs 640 and 642

Related Articles

C# 7 Features

JDK 14 Released

Python - Dead Batteries Included?

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


The Feds Want Us To Move On From C/C++
13/11/2024

The clamour for safe programming languages seems to be growing and becoming official. We have known for a while that C and C++ are dangerous languages so why has it become such an issue now and is it  [ ... ]



DuckDB And Hydra Partner To Get DuckDB Into PostgreSQL
11/11/2024

The offspring of that partnership is pg_duckdb, an extension that embeds the DuckDB engine into the PostgreSQL database, allowing it to handle analytical workloads.


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1871962587>

 

Last Updated ( Wednesday, 10 February 2021 )