The structural pattern-matching syntax introduced in Python 3.10 “allows for powerful new programming techniques for decision-making in apps,” says Serdar Yegulalp in a recent article at InfoWorld.
“Python, for all its power and popularity, has long lacked a form of flow control found in other languages—a way to take a value and match it elegantly against one of a number of possible conditions,” he writes. “In C and C++, it’s the switch/case
construction; in Rust, it’s called pattern matching.”
In this tutorial, Yegulalp provides an overview of the concept, noting that “structural pattern matching introduces the match/case
statement and the pattern syntax to Python. The match/case
statement follows the same basic outline as switch/case
. It takes an object, tests the object against one or more match patterns, and takes an action if it finds a match.”
Yegulalp also explores the following scenarios within Python structural pattern matching:
- Matching against variables
- Matching against objects
- Identifying patterns
- Using pattern matching effectively
Read the complete article at InfoWorld (free registration may be required).
Comments