Loading video player…

Using Structural Pattern Matching in Python (Overview)

Structural pattern matching is a powerful control flow construct invented decades ago that’s traditionally used by compiled languages, especially within the functional programming paradigm.

Most mainstream programming languages have since adopted some form of pattern matching, which offers concise and readable syntax while promoting a declarative code style. Although Python was late to join the party, it introduced structural pattern matching in the 3.10 release.

In this video course, you’ll:

  • Master the syntax of the match statement and case clauses
  • Explore various types of patterns supported by Python
  • Learn about guards, unions, aliases, and name binding
  • Extract values from deeply nested hierarchical data structures
  • Customize pattern matching for user-defined classes
  • Identify and avoid common pitfalls in Python’s pattern matching
Download

Course Slides (.pdf)

4.3 MB
Download

Sample Code (.zip)

4.1 KB

00:00 Welcome to using Structural Pattern Matching in Python. My name’s Joseph, and I will be your guide through the intricacies of one of Python’s newer features, structural pattern matching. Introduced in Python 3.10 back in late 2021, structural pattern matching is still quite new as a feature of the language.

00:17 In fact, Python was already 30 years old at that point. It’s nice to see people and programming languages, learning new things after 30, right? Anyway, being so relatively new of a feature pattern matching often goes overlooked by experienced and inexperienced Python developers alike.

00:34 And because it’s so versatile, many of its features are actually ignored or underused, which is a real shame. Pattern matching when used appropriately, can make your code so much cleaner and more maintainable.

00:46 If you haven’t been using structural pattern matching or maybe just not using it to its fullest, you’ve been missing out, but no longer because by the end of this course, you’ll master the syntax of the match statement and case clauses used in pattern matching.

00:59 You’ll understand the different types of patterns you can build in Python. You’ll also become skilled at leveraging advanced pattern matching techniques. You’ll develop a deep understanding of pattern matching’s limitations and when to apply it.

01:11 And finally, you’ll solidify what you’ve learned by working with practical examples of pattern matching, like using it for data validation, or to read and parse deeply nested hierarchical data structures.

01:22 Now, to get the most out of this course, you really should have a basic understanding of Python’s conditional statements, loops, functions, and classes.

01:30 And it would also help if you had some familiarity with built-in data structures like tuples, lists, and dictionaries. So meet me in the next lesson where we’ll get into the nitty gritty of structural pattern matching.

Become a Member to join the conversation.