Python match compares the value of an expression with patterns given in case blocks, when match exist value from that case block is returned, or command from that case block is executed.
Example:
brand = input('Please enter car brand: ')
def discount_level(brand):
match brand.upper():
case 'FORD':
return 0.1
case 'RENAULT':
return 0.05
case 'BMW':
return 0.08
case _:
return 0.03
print(f"Discount for {brand.upper()} is {discount_level(brand)} ")
#output sample
Please enter car brand: Renault
Discount for RENAULT is 0.05
Here we compare or match brand string (with capital, means brand.upper() ) with patterns from case (FORD, RENAULT, BMW ). Anything else that is different than FORD, RENAULT, BMW will match the last case i.e. block "case _:". Character "_" it is like a wildcard.
In case block we can have one command or more commands, case block is "delimited" bassically according with indentation. Thus, in previous example, I modify a little bit and in case 'FORD' block I put at same indentation level two commands, for example now there is:
brand = input('Please enter car brand: ')
def discount_level(brand):
match brand.upper():
case 'FORD':
print('Based on the brand')
return 0.1
case 'RENAULT':
return 0.05
case 'BMW':
return 0.08
case _:
return 0.03
print(f"Discount for {brand.upper()} is {discount_level(brand)} ")
#sample output for above:
Please enter car brand: Ford
Based on the brand
Discount for FORD is 0.1
In the previous sample it matches brand with one of value from case statement. Nevertheless, in case statement we can have multiple values. For example, if we want to give a discount of 10% (i.e. 0.1) for brands: Ford, Skoda, Opel we can use "|" operator and example become:
brand = input('Please enter car brand: ')
def discount_level(brand):
match brand.upper():
case 'FORD'|'SKODA'|'OPEL':
return 0.1
case 'RENAULT':
return 0.05
case 'BMW':
return 0.08
case _:
return 0.03
print(f"Discount for {brand.upper()} is {discount_level(brand)} ")
#sample output for above:
Please enter car brand: Opel
Discount for OPEL is 0.1
In examples till now we compared brand, i.e. a string, similar is working if we compare numbers, or even bool, but match statement can compare more complex structures. For classes or objects an example is:
class CarFeature:
numSpeed: int
maxSpeed: int
acc100: float
def __init__(self, numSpeed, maxSpeed, acc100):
self.numSpeed = numSpeed
self.maxSpeed = maxSpeed
self.acc100 = acc100
def car_q(carfeature):
match carfeature:
case CarFeature(numSpeed=6, maxSpeed = 250, acc100 = 9.9):
print("High class car")
case CarFeature(numSpeed=5, maxSpeed = 175, acc100 = 11.5):
print("Entry level class car")
case _:
print("Middle level class car")
carfeature1 = CarFeature(6, 260, 9.8)
print(f"car 1 with numSpeed: {carfeature1.numSpeed}, maxSpeed: {carfeature1.maxSpeed}, acc100: {carfeature1.acc100} is")
car_q(carfeature1)
#Output of code:
car 1 with numSpeed: 6, maxSpeed: 260, acc100: 9.8 is
Middle level class car
In this example we defined a class named CarFeature. Then we defined a function car_q that uses match statement which in this case compares or has in case "instances" of the CarFeature class.
Then we created a carfeature1 instance of class CarFeature, and used carfeature1 to test car_q function.
This match example compares instances of classes or objects. Basically, match can compare any pattern that is according with pattern matching PEP 636 specifications.