SlideShare a Scribd company logo
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
© 2021 Haim Michael All Rights Reserved 2
Introduction
 As of Python 3.10 we can use patterns matching. There are
three PEPs (Python Enhancement Proposals) describing this
feature:
PEP 634: The Specification
https://www.python.org/dev/peps/pep-0634
PEP 635: Motivation and Rationale
https://www.python.org/dev/peps/pep-0635
PEP 636: Patterns Matching Tutorial
https://www.python.org/dev/peps/pep-0636
© 2021 Haim Michael All Rights Reserved 3
Switch Statement on Steroids
 We can consider patterns matching as a switch statement on
steroids.
© 2021 Haim Michael All Rights Reserved 4
Matching Specific Values
 The simplest form of patterns matching in Python would be a
simple use case similar to switch statement.
command = "+"
match command:
case '+':
print('plus...')
case '-':
print('minus')
case '*':
print('multiply')
case '/':
print('divide')
© 2021 Haim Michael All Rights Reserved 5
The _ Wildcard
 We can use the _ character as a wild card. We shall place the
wild card as the last case possibility. If none of the other
cases matches, the wild card will.
command = "%"
match command:
case '+':
print('plus...')
case '-':
print('minus')
case '/':
print('divide')
case _:
print('soething else')
© 2021 Haim Michael All Rights Reserved 6
Matching Sequences
 We can match a sequence with possible sequences
composed of literals or variables.
operation = ("deposit",1200,123222)
operation = ("transfer",800,123222,101888)
match operation:
case operation,sum,account_id:
print("%s %f %d" % (operation,sum,account_id))
case operation, sum, from_id, to_id:
print("%s %f from %d to %d" % (operation, sum, from_id, to_id))
© 2021 Haim Michael All Rights Reserved 7
Matching Sequences
operation = ["playing","pacman"]
match operation:
case ["eat",food]:
print("eating %s is great" % (food,))
case ["watching",show]:
print("enjoy watching %s" % (show,))
case ["playing",game]:
print("playing %s is awesome" % (game,))
case _:
print("your operation cannot be recognized")
© 2021 Haim Michael All Rights Reserved 8
Matching Multiple Values
 We can use the packing capability similarly to the way we use
it in assignments.
© 2021 Haim Michael All Rights Reserved 9
Matching Multiple Values
operation = ("delete","Hello.py","Calcu.py","Demo.py")
match operation:
case "delete",*files:
print("the following files will be deleted:")
for file in files:
print(file)
case "makedir",*directories:
print("the following directories will be created:")
for directory in directories:
print(directory)
case _:
print("the requesed operation is not recognized")
© 2021 Haim Michael All Rights Reserved 10
Composing Patters
 We can compose new patterns composed from others.
Patterns that don't include other patterns can be any of the
following: capture patterns, literal patterns, and the wildcard
pattern _.
© 2021 Haim Michael All Rights Reserved 11
Composing Patters
operation = ("update_mark",("mosh",234234),103444,88)
match operation:
case "register_course",student,course_id:
print("registering to course")
print(student)
print(course_id)
case "update_mark",(student_name,student_id),course_id,mark:
print("updating mark in specific course")
print("student name: %s", student_name)
print("student id: %d" % (student_id,))
print("course id: %d" % (course_id,))
print("mark: %.2f" % (mark,))
© 2021 Haim Michael All Rights Reserved 12
Or Patterns
 We can use the | operator (AKA the or operator) in order to
create a case that includes more than one pattern. Matching
any of the patterns will be considered as matching the entire
case.
© 2021 Haim Michael All Rights Reserved 13
Or Patterns
command = ("del","temp.txt")
match command:
case ("delete",file) | ("remove",file) | ("del",file):
print("deleting file %s" % (file,))
case ("copy",file,newfile):
print("copying file %s to %s" % (file,newfile))
© 2021 Haim Michael All Rights Reserved 14
The as Pattern
 We can use the or operator for creating a sub pattern
composed of multiple possibilities. We can use the as pattern
for finding which is the exact possibility that match.
© 2021 Haim Michael All Rights Reserved 15
The as Pattern
data = ("del","temp.txt")
match data:
case (("delete" | "remove" | "del") as command, file):
print("deleting file %s" % (file,))
print("command=%s" % (command,))
case ("copy",file,newfile):
print("copying file %s to %s" % (file,newfile))
© 2021 Haim Michael All Rights Reserved 16
Adding Conditions
 We can add a condition to specific cases. The condition
includes the use of the if keyword followed by expression that
its value is of the type bool.
© 2021 Haim Michael All Rights Reserved 17
Adding Conditions
data = ("del","temp.txt")
files = ["readme.txt", "temp.txt", "index.txt"]
match data:
case (("delete" | "remove" | "del") as command, file) if file in files:
print("deleting file %s" % (file,))
print("command=%s" % (command,))
case (("delete" | "remove" | "del") as command, file) if file not in files:
print("the file %s cannot be deleted" % file)
case ("copy",file,newfile):
print("copying file %s to %s" % (file,newfile))
© 2021 Haim Michael All Rights Reserved 18
Matching Objects Types
 When having an object we need to examine its type and its
attributes we can avoid the use of isinstance() and we
can avoid checking its attributes.
© 2021 Haim Michael All Rights Reserved 19
Matching Objects Types
class Dog:
def hau(self):
return "hau hau"
class Cat:
def __init__(self,predator):
self.predator = predator
def miaou(self):
return "miaou miaou"
class Cow:
def moo(self):
return "moo moo"
ob = Cat(True)
© 2021 Haim Michael All Rights Reserved 20
Matching Objects Types
match ob:
case Dog():
print(ob.hau())
case Cat(predator=False):
print(ob.miaou())
case Cat(predator=True):
print("%s grrrrr" % ob.miaou())
case Cow():
print(ob.moo())
© 2021 Haim Michael All Rights Reserved 21
Matching Attributes By Position
 When using objects that were instantiated from a class
marked as dataclass we can describe the matched
attributes by position.
© 2021 Haim Michael All Rights Reserved 22
Matching Attributes By Position
from dataclasses import dataclass
from math import pow, sqrt
@dataclass
class Point:
x: int
y: int
@dataclass
class Line:
p1: Point
p2: Point
ob = Line(Point(3, 4), Point(6, 8))
match ob:
case Line(p1, p2):
print(sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2)))
case Point(x, y):
print(sqrt(pow(x, 2) + pow(y, 2)))
© 2021 Haim Michael All Rights Reserved 23
The __match_args__ Attribute
 When using objects that were not instantiated from a class
marked as dataclass and were not instantiated from the
tuple type we can still describe the matched attributes by
position if we add the __match_args__ attribute to the
class definition.
 The __match_args__ special attribute defines an explicit
order for the attributes.
© 2021 Haim Michael All Rights Reserved 24
The __match_args__ Attribute
from math import pow, sqrt
class Point:
__match_args__ = ("x", "y")
def __init__(self, x, y):
self.x = x
self.y = y
class Line:
__match_args__ = ("p1", "p2")
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
ob = Line(Point(3, 4), Point(6, 8))
© 2021 Haim Michael All Rights Reserved 25
The __match_args__ Attribute
match ob:
case Line(p1, p2):
print(sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2)))
case Point(x, y):
print(sqrt(pow(x, 2) + pow(y, 2)))
© 2021 Haim Michael All Rights Reserved 26
Enums Matching
 We can match agains possible values that were defined as
part of an Enum object.
from enum import Enum
class Color(Enum):
BLUE = 1
GREEN = 2
RED = 3
BLACK = 4
WHITE = 5
class Car:
__match_args__ = ("brand", "id", "color")
def __init__(self, brand, id, color):
self.brand = brand
self.id = id
self.color = color
© 2021 Haim Michael All Rights Reserved 27
Enums Matching
ob = Car("Mazda 6", 2342343, Color.GREEN)
match ob:
case Car(car_brand, car_id, Color.WHITE):
print("white cars are simpler to handle")
case Car(car_brand, car_id, Color.BLACK):
print("black cars aborb the heat")
case Car(car_brand, car_id, _):
print("colorful cars are more fun")
© 2021 Haim Michael All Rights Reserved 28
Mappings Matching
 We can match an expression against a dict object. The
matching will be based on the keys.
© 2021 Haim Michael All Rights Reserved 29
Mappings Matching
 We can match an expression against a dict object. The
matching will be based on the keys.
command = {
'action': 'getbalance',
'account': {
'accountid': 204714,
'owners_ids': (24537564, 51634545)
}}
© 2021 Haim Michael All Rights Reserved 30
Mappings Matching
match command:
case {'action': 'getbalance', 'account': account}:
print("getting balance")
print("account id: %s" % (account['accountid'],))
print("account owners are:")
print(account['owners_ids'])
case {'action': 'deposit', 'sum': sum, 'account': account}:
print("deposit %f" % sum)
print("account id: %s" % (account,))
print("account owners are:")
print(account['owners_ids'])
© 2021 Haim Michael All Rights Reserved 31
Mappings Matching
 We can use the dict packing capability when matching a dict
object with additional key value pairs that don't match any of
the pattern's parts.
command = {
'action': 'getbalance',
'account': {
'accountid': 204714,
'owners_ids': (24537564, 51634545)
},
'date': {'day':12,'month':1,'year':1980},
'time': {'hour': 14, 'minutes': 20}
}
© 2021 Haim Michael All Rights Reserved 32
Mappings Matching
match command:
case {'action': 'getbalance', 'account': account, **ob}:
print("getting balance")
print("account id: %s" % (account['accountid'],))
print("account owners are:")
print(account['owners_ids'])
print(ob)
case {'action': 'deposit', 'sum': sum, 'account': account, **ob}:
print("deposit %f" % sum)
print("account id: %s" % (account,))
print("account owners are:")
print(account['owners_ids'])
print(ob)
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)
}
}
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)
© 2021 Haim Michael All Rights Reserved 35
Questions & Answers
Thanks for Your Time!
Haim Michael
haim.michael@lifemichael.com
+972+3+3726013 ext:700
life
michael

More Related Content

PPTX
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
PDF
A Brief Overview of (Static) Program Query Languages
PDF
Luciano Ramalho - Fluent Python_ Clear, Concise, and Effective Programming-O'...
PPTX
Python For Data Science.pptx
PDF
Fluent Python Clear Concise And Effective Programming 2nd Edition 2nd Luciano...
PDF
Python basic
PDF
Object Orientation vs Functional Programming in Python
PDF
A tour of Python
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
A Brief Overview of (Static) Program Query Languages
Luciano Ramalho - Fluent Python_ Clear, Concise, and Effective Programming-O'...
Python For Data Science.pptx
Fluent Python Clear Concise And Effective Programming 2nd Edition 2nd Luciano...
Python basic
Object Orientation vs Functional Programming in Python
A tour of Python

Similar to Structural Pattern Matching in Python (20)

PDF
An overview of Python 2.7
PPTX
Introduction to Python , Overview
PPT
From Operators to Arrays – Power Up Your Python Skills for Real-World Coding!
PDF
Python_Programming_and_Numerical_Methods_A_Guide_for_Engineers_and.pdf
PPT
Python High Level Functions_Ch 11.ppt
PDF
Python CookBook 3rd ed learn bout the programming .pdf
PDF
Python's magic methods
PPTX
An Introduction : Python
PPTX
Complete Core Python with IPT Excel School
PDF
tutorial.pdf
PDF
Configurable Pattern Matching Semantics in openCypher: Defining Levels of Nod...
PDF
Advanced python
PDF
Python Cookbook Third Edition 3rd Edition David Beazley Brian Jones
PDF
Python lecture 05
PDF
python learn basic tutorial learn easy..
PPTX
Python programming workshop
PDF
Copy_of_python-journeyman.pdf
PPTX
Docketrun's Python Course for beginners.pptx
PPTX
Intro to Python (High School) Unit #2
PDF
Introduction to Python
An overview of Python 2.7
Introduction to Python , Overview
From Operators to Arrays – Power Up Your Python Skills for Real-World Coding!
Python_Programming_and_Numerical_Methods_A_Guide_for_Engineers_and.pdf
Python High Level Functions_Ch 11.ppt
Python CookBook 3rd ed learn bout the programming .pdf
Python's magic methods
An Introduction : Python
Complete Core Python with IPT Excel School
tutorial.pdf
Configurable Pattern Matching Semantics in openCypher: Defining Levels of Nod...
Advanced python
Python Cookbook Third Edition 3rd Edition David Beazley Brian Jones
Python lecture 05
python learn basic tutorial learn easy..
Python programming workshop
Copy_of_python-journeyman.pdf
Docketrun's Python Course for beginners.pptx
Intro to Python (High School) Unit #2
Introduction to Python
Ad

More from Haim Michael (20)

PDF
Prompt Engineering Jump Start [Free Meetup]
PDF
IntelliJ Debugging Essentials for Java Developers
PDF
The Visitor Classic Design Pattern [Free Meetup]
PDF
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
PDF
Introduction to Pattern Matching in Java [Free Meetup]
PDF
Mastering The Collections in JavaScript [Free Meetup]
PDF
Beyond Java - Evolving to Scala and Kotlin
PDF
JavaScript Promises Simplified [Free Meetup]
PDF
Scala Jump Start [Free Online Meetup in English]
PDF
The MVVM Architecture in Java [Free Meetup]
PDF
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
PDF
Anti Patterns
PDF
Virtual Threads in Java
PDF
MongoDB Design Patterns
PDF
Introduction to SQL Injections
PDF
Record Classes in Java
PDF
Microservices Design Patterns
PDF
Unit Testing in Python
PDF
OOP Best Practices in JavaScript
PDF
Java Jump Start
Prompt Engineering Jump Start [Free Meetup]
IntelliJ Debugging Essentials for Java Developers
The Visitor Classic Design Pattern [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]
Beyond Java - Evolving to Scala and Kotlin
JavaScript Promises Simplified [Free Meetup]
Scala Jump Start [Free Online Meetup in English]
The MVVM Architecture in Java [Free Meetup]
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Anti Patterns
Virtual Threads in Java
MongoDB Design Patterns
Introduction to SQL Injections
Record Classes in Java
Microservices Design Patterns
Unit Testing in Python
OOP Best Practices in JavaScript
Java Jump Start
Ad

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
Teaching material agriculture food technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Empathic Computing: Creating Shared Understanding
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Approach and Philosophy of On baking technology
PDF
Electronic commerce courselecture one. Pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Programs and apps: productivity, graphics, security and other tools
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Assigned Numbers - 2025 - Bluetooth® Document
Empathic Computing: Creating Shared Understanding
MYSQL Presentation for SQL database connectivity
Approach and Philosophy of On baking technology
Electronic commerce courselecture one. Pdf
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Building Integrated photovoltaic BIPV_UPV.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation_ Review paper, used for researhc scholars
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

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
  • 2. © 2021 Haim Michael All Rights Reserved 2 Introduction  As of Python 3.10 we can use patterns matching. There are three PEPs (Python Enhancement Proposals) describing this feature: PEP 634: The Specification https://www.python.org/dev/peps/pep-0634 PEP 635: Motivation and Rationale https://www.python.org/dev/peps/pep-0635 PEP 636: Patterns Matching Tutorial https://www.python.org/dev/peps/pep-0636
  • 3. © 2021 Haim Michael All Rights Reserved 3 Switch Statement on Steroids  We can consider patterns matching as a switch statement on steroids.
  • 4. © 2021 Haim Michael All Rights Reserved 4 Matching Specific Values  The simplest form of patterns matching in Python would be a simple use case similar to switch statement. command = "+" match command: case '+': print('plus...') case '-': print('minus') case '*': print('multiply') case '/': print('divide')
  • 5. © 2021 Haim Michael All Rights Reserved 5 The _ Wildcard  We can use the _ character as a wild card. We shall place the wild card as the last case possibility. If none of the other cases matches, the wild card will. command = "%" match command: case '+': print('plus...') case '-': print('minus') case '/': print('divide') case _: print('soething else')
  • 6. © 2021 Haim Michael All Rights Reserved 6 Matching Sequences  We can match a sequence with possible sequences composed of literals or variables. operation = ("deposit",1200,123222) operation = ("transfer",800,123222,101888) match operation: case operation,sum,account_id: print("%s %f %d" % (operation,sum,account_id)) case operation, sum, from_id, to_id: print("%s %f from %d to %d" % (operation, sum, from_id, to_id))
  • 7. © 2021 Haim Michael All Rights Reserved 7 Matching Sequences operation = ["playing","pacman"] match operation: case ["eat",food]: print("eating %s is great" % (food,)) case ["watching",show]: print("enjoy watching %s" % (show,)) case ["playing",game]: print("playing %s is awesome" % (game,)) case _: print("your operation cannot be recognized")
  • 8. © 2021 Haim Michael All Rights Reserved 8 Matching Multiple Values  We can use the packing capability similarly to the way we use it in assignments.
  • 9. © 2021 Haim Michael All Rights Reserved 9 Matching Multiple Values operation = ("delete","Hello.py","Calcu.py","Demo.py") match operation: case "delete",*files: print("the following files will be deleted:") for file in files: print(file) case "makedir",*directories: print("the following directories will be created:") for directory in directories: print(directory) case _: print("the requesed operation is not recognized")
  • 10. © 2021 Haim Michael All Rights Reserved 10 Composing Patters  We can compose new patterns composed from others. Patterns that don't include other patterns can be any of the following: capture patterns, literal patterns, and the wildcard pattern _.
  • 11. © 2021 Haim Michael All Rights Reserved 11 Composing Patters operation = ("update_mark",("mosh",234234),103444,88) match operation: case "register_course",student,course_id: print("registering to course") print(student) print(course_id) case "update_mark",(student_name,student_id),course_id,mark: print("updating mark in specific course") print("student name: %s", student_name) print("student id: %d" % (student_id,)) print("course id: %d" % (course_id,)) print("mark: %.2f" % (mark,))
  • 12. © 2021 Haim Michael All Rights Reserved 12 Or Patterns  We can use the | operator (AKA the or operator) in order to create a case that includes more than one pattern. Matching any of the patterns will be considered as matching the entire case.
  • 13. © 2021 Haim Michael All Rights Reserved 13 Or Patterns command = ("del","temp.txt") match command: case ("delete",file) | ("remove",file) | ("del",file): print("deleting file %s" % (file,)) case ("copy",file,newfile): print("copying file %s to %s" % (file,newfile))
  • 14. © 2021 Haim Michael All Rights Reserved 14 The as Pattern  We can use the or operator for creating a sub pattern composed of multiple possibilities. We can use the as pattern for finding which is the exact possibility that match.
  • 15. © 2021 Haim Michael All Rights Reserved 15 The as Pattern data = ("del","temp.txt") match data: case (("delete" | "remove" | "del") as command, file): print("deleting file %s" % (file,)) print("command=%s" % (command,)) case ("copy",file,newfile): print("copying file %s to %s" % (file,newfile))
  • 16. © 2021 Haim Michael All Rights Reserved 16 Adding Conditions  We can add a condition to specific cases. The condition includes the use of the if keyword followed by expression that its value is of the type bool.
  • 17. © 2021 Haim Michael All Rights Reserved 17 Adding Conditions data = ("del","temp.txt") files = ["readme.txt", "temp.txt", "index.txt"] match data: case (("delete" | "remove" | "del") as command, file) if file in files: print("deleting file %s" % (file,)) print("command=%s" % (command,)) case (("delete" | "remove" | "del") as command, file) if file not in files: print("the file %s cannot be deleted" % file) case ("copy",file,newfile): print("copying file %s to %s" % (file,newfile))
  • 18. © 2021 Haim Michael All Rights Reserved 18 Matching Objects Types  When having an object we need to examine its type and its attributes we can avoid the use of isinstance() and we can avoid checking its attributes.
  • 19. © 2021 Haim Michael All Rights Reserved 19 Matching Objects Types class Dog: def hau(self): return "hau hau" class Cat: def __init__(self,predator): self.predator = predator def miaou(self): return "miaou miaou" class Cow: def moo(self): return "moo moo" ob = Cat(True)
  • 20. © 2021 Haim Michael All Rights Reserved 20 Matching Objects Types match ob: case Dog(): print(ob.hau()) case Cat(predator=False): print(ob.miaou()) case Cat(predator=True): print("%s grrrrr" % ob.miaou()) case Cow(): print(ob.moo())
  • 21. © 2021 Haim Michael All Rights Reserved 21 Matching Attributes By Position  When using objects that were instantiated from a class marked as dataclass we can describe the matched attributes by position.
  • 22. © 2021 Haim Michael All Rights Reserved 22 Matching Attributes By Position from dataclasses import dataclass from math import pow, sqrt @dataclass class Point: x: int y: int @dataclass class Line: p1: Point p2: Point ob = Line(Point(3, 4), Point(6, 8)) match ob: case Line(p1, p2): print(sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2))) case Point(x, y): print(sqrt(pow(x, 2) + pow(y, 2)))
  • 23. © 2021 Haim Michael All Rights Reserved 23 The __match_args__ Attribute  When using objects that were not instantiated from a class marked as dataclass and were not instantiated from the tuple type we can still describe the matched attributes by position if we add the __match_args__ attribute to the class definition.  The __match_args__ special attribute defines an explicit order for the attributes.
  • 24. © 2021 Haim Michael All Rights Reserved 24 The __match_args__ Attribute from math import pow, sqrt class Point: __match_args__ = ("x", "y") def __init__(self, x, y): self.x = x self.y = y class Line: __match_args__ = ("p1", "p2") def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 ob = Line(Point(3, 4), Point(6, 8))
  • 25. © 2021 Haim Michael All Rights Reserved 25 The __match_args__ Attribute match ob: case Line(p1, p2): print(sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2))) case Point(x, y): print(sqrt(pow(x, 2) + pow(y, 2)))
  • 26. © 2021 Haim Michael All Rights Reserved 26 Enums Matching  We can match agains possible values that were defined as part of an Enum object. from enum import Enum class Color(Enum): BLUE = 1 GREEN = 2 RED = 3 BLACK = 4 WHITE = 5 class Car: __match_args__ = ("brand", "id", "color") def __init__(self, brand, id, color): self.brand = brand self.id = id self.color = color
  • 27. © 2021 Haim Michael All Rights Reserved 27 Enums Matching ob = Car("Mazda 6", 2342343, Color.GREEN) match ob: case Car(car_brand, car_id, Color.WHITE): print("white cars are simpler to handle") case Car(car_brand, car_id, Color.BLACK): print("black cars aborb the heat") case Car(car_brand, car_id, _): print("colorful cars are more fun")
  • 28. © 2021 Haim Michael All Rights Reserved 28 Mappings Matching  We can match an expression against a dict object. The matching will be based on the keys.
  • 29. © 2021 Haim Michael All Rights Reserved 29 Mappings Matching  We can match an expression against a dict object. The matching will be based on the keys. command = { 'action': 'getbalance', 'account': { 'accountid': 204714, 'owners_ids': (24537564, 51634545) }}
  • 30. © 2021 Haim Michael All Rights Reserved 30 Mappings Matching match command: case {'action': 'getbalance', 'account': account}: print("getting balance") print("account id: %s" % (account['accountid'],)) print("account owners are:") print(account['owners_ids']) case {'action': 'deposit', 'sum': sum, 'account': account}: print("deposit %f" % sum) print("account id: %s" % (account,)) print("account owners are:") print(account['owners_ids'])
  • 31. © 2021 Haim Michael All Rights Reserved 31 Mappings Matching  We can use the dict packing capability when matching a dict object with additional key value pairs that don't match any of the pattern's parts. command = { 'action': 'getbalance', 'account': { 'accountid': 204714, 'owners_ids': (24537564, 51634545) }, 'date': {'day':12,'month':1,'year':1980}, 'time': {'hour': 14, 'minutes': 20} }
  • 32. © 2021 Haim Michael All Rights Reserved 32 Mappings Matching match command: case {'action': 'getbalance', 'account': account, **ob}: print("getting balance") print("account id: %s" % (account['accountid'],)) print("account owners are:") print(account['owners_ids']) print(ob) case {'action': 'deposit', 'sum': sum, 'account': account, **ob}: print("deposit %f" % sum) print("account id: %s" % (account,)) print("account owners are:") print(account['owners_ids']) print(ob)
  • 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)
  • 35. © 2021 Haim Michael All Rights Reserved 35 Questions & Answers Thanks for Your Time! Haim Michael [email protected] +972+3+3726013 ext:700 life michael