Python match...case-Anweisung
match...caseEs bietet eine leistungsfähigere Methode zum Musterabgleich.
Musterabgleich ist eine Methode zur Verarbeitung von Datenstrukturen in der Programmierung, die den Code prägnanter und lesbarer macht.
match...caseEs ist eine neue Syntax, die in Python 3.10 eingeführt wurde.
match...caseDie Syntaxstruktur ist wie folgt:
match expression:
case pattern1:
# 处理pattern1的逻辑
case pattern2 if condition:
# 处理pattern2并且满足condition的逻辑
case _:
# 处理其他情况的逻辑
Parameter description:
matchAuf die Anweisung folgt ein Ausdruck, dann verwendecaseAnweisung, um verschiedene Muster zu definieren.caseDarauf folgt ein Muster, das ein bestimmter Wert, eine Variable, eine Wildcard usw. sein kann.- Sie können
ifSchlüsselwort incaseum Bedingungen hinzuzufügen. _Es wird normalerweise als Wildcard verwendet, um einen beliebigen Wert abzugleichen.

Beispiel
1. Einfacher Wertabgleich
Beispiel
def match_example(value):
match value:
case 1:
print("Wert 1 stimmt überein")
case 2:
print("Wert 2 stimmt überein")
case _:
print("Andere Werte stimmen überein")
match_example(1) # Ausgabe: Wert 1 stimmt überein
match_example(2) # Ausgabe: Wert 2 stimmt überein
match_example(3) # Ausgabe: Andere Werte stimmen überein
match value:
case 1:
print("Wert 1 stimmt überein")
case 2:
print("Wert 2 stimmt überein")
case _:
print("Andere Werte stimmen überein")
match_example(1) # Ausgabe: Wert 1 stimmt überein
match_example(2) # Ausgabe: Wert 2 stimmt überein
match_example(3) # Ausgabe: Andere Werte stimmen überein
Im obigen Code wird diematch-Anweisung verwendet, umvaluemit verschiedenen Fällen abzugleichen, jedecase-Anweisung stellt einen möglichen übereinstimmenden Fall dar,_Der Platzhalter repräsentiert andere Fälle.
Das Ausgabeergebnis ist:
匹配到值为1 匹配到值为2 匹配到其他值
2. Verwenden von Variablen
Beispiel
def match_example(item):
match item:
case (x, y) if x == y:
print(f"Ein gleiches Tupel stimmte überein: {item}")
case (x, y):
print(f"Ein Tupel stimmte überein: {item}")
case _:
print("Andere Fälle stimmten überein")
match_example((1, 1)) #Ausgabe:Ein gleiches Tupel stimmte überein: (1, 1)
match_example((1, 2)) #Ausgabe:Ein Tupel stimmte überein: (1, 2)
match_example("other") #Ausgabe:Andere Fälle stimmten überein
match item:
case (x, y) if x == y:
print(f"Ein gleiches Tupel stimmte überein: {item}")
case (x, y):
print(f"Ein Tupel stimmte überein: {item}")
case _:
print("Andere Fälle stimmten überein")
match_example((1, 1)) #Ausgabe:Ein gleiches Tupel stimmte überein: (1, 1)
match_example((1, 2)) #Ausgabe:Ein Tupel stimmte überein: (1, 2)
match_example("other") #Ausgabe:Andere Fälle stimmten überein
Das Ausgabeergebnis ist:
匹配到相等的元组: (1, 1) 匹配到元组: (1, 2) 匹配到其他情况
3. Typabgleich
Beispiel
class Circle:
def __init__(self, radius):
self.radius = radius
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def match_shape(shape):
match shape:
case Circle(radius=1):
print("Kreis mit Radius 1 erkannt")
case Rectangle(width=1, height=2):
print("Rechteck mit Breite 1 und Höhe 2 erkannt")
case _:
print("Andere Formen erkannt")
match_shape(Circle(radius=1)) # Ausgabe: Kreis mit Radius 1 erkannt
match_shape(Rectangle(width=1, height=2)) # Ausgabe: Rechteck mit Breite 1 und Höhe 2 erkannt
match_shape("other") # Ausgabe: Andere Formen erkannt
def __init__(self, radius):
self.radius = radius
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def match_shape(shape):
match shape:
case Circle(radius=1):
print("Kreis mit Radius 1 erkannt")
case Rectangle(width=1, height=2):
print("Rechteck mit Breite 1 und Höhe 2 erkannt")
case _:
print("Andere Formen erkannt")
match_shape(Circle(radius=1)) # Ausgabe: Kreis mit Radius 1 erkannt
match_shape(Rectangle(width=1, height=2)) # Ausgabe: Rechteck mit Breite 1 und Höhe 2 erkannt
match_shape("other") # Ausgabe: Andere Formen erkannt
Das Ausgabeergebnis ist:
匹配到半径为1的圆 匹配到宽度为1,高度为2的矩形 匹配到其他形状Andere Erweiterungen
Python3 Bedingte Steuerung