|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.
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.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Wednesday, 10 February 2021 )|