This document provides an introduction and overview of structural pattern matching in Python. It discusses how pattern matching can be considered as switch statements on steroids, and demonstrates various pattern matching techniques including matching specific values, sequences, objects, attributes, enums, mappings, adding conditions, and more. Examples are provided throughout to illustrate each technique. The document is intended to help explain the capabilities and usage of Python's new pattern matching feature.
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Structural Pattern Matching in Python
1. Structural Pattern Matching
Haim Michael
September 20th
, 2022
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
https://youtu.be/xgRYrs_jy5E
life michael
33. Matching Builtin Types
We can use builtin types for validating the types of specific
parts in the expression we check.
data = {
'action': 'getbalance',
'sum':800,
'account': {
'accountid': 204714,
'owners_ids': (24537564, 51634545)
}
}
34. Matching Builtin Types
match data:
case {'action': str() as action, 'account': account}:
print(action)
print("account id: %s" % (account['accountid'],))
print("account owners are:")
print(account['owners_ids'])
case _:
print(data)