Python will support Pattern Matching

After some discussions, the Python steering committee accepted the proposal to add pattern-matching to Python. The specific situation is to adopt PEP 634, 635, and 636, and reject PEP 640 and 642.

For a long time, the Python community has put forward various suggestions for the realization of specified multi-branch conditions in the Python language (similar to the switch statement in C/C++), but none of the proposals can be finally implemented. In the past 8 months or so, the Python community discussed a proposal that might solve the multi-branch condition problem (or even more) and was adopted the pattern matching suggestions (PEP 634, 635, and 636). Examples are as follows:

match command.split(): 

case ["quit"]:
print("Goodbye!")
quit_game()
case ["get", obj]:
character.get(obj, current_room)
case ["go", direction]:
current_room = current_room.neighbor(direction)
case ["drop", *objects]:
for obj in objects:
character.drop(obj, current_room)
case ["north"] | ["go", "north"]:
current_room = current_room.neighbor("north")
case ["go", ("north" | "south" | "east" | "west")]:
current_room = current_room.neighbor(...)
case ["go", direction] if direction in current_room.exits:
current_room = current_room.neighbor(direction)
case Click(position=(x, y)):
handle_click_at(x, y)
case Click((x, y)):
handle_click_at(x, y)
case Click((x, y), button=Button.LEFT): # This is a left click
handle_click_at(x, y)
case {"sleep": duration}:
ui.wait(duration)
case {"sleep": float(duration)}:
ui.wait(duration)
case _:
print(f"Sorry, I couldn't understand {command!r}")

In the email, the Python steering committee believes that this feature requires comprehensive documentation and specifications, including the tutorial part of the documentation and language reference, and the documentation must be sent with Python 3.10, so 3.10 will not be released until this content is completed. At the same time, the steering committee stated that it rejected PEP 640 and 642 that tried to improve pattern matching due to the lack of support from core developers.

In addition, although it has been determined so far, before the functional freezing point of Python 3.10 (see PEP 619 for the schedule of 3.10), developers can still submit changes to pattern matching, including accepted PEP 634 and rejected PEP 640, 642.