Structural Pattern Matching in Python

Python 3.10 is adding pattern matching in Python. This is a good official tutorial around it.

Few interesting use cases:

command = input("What are you doing next? ")

# we want to use command.split()
# one of the options is to use: [action, obj] = command.split()
# but what if the input has multiple words


# pattern matching can come handy here
match command.split():
    case [action]:
        ... # interpret single-verb action
    case [action, obj]:
        ... # interpret action, obj

# we can use club it with specific values
match command.split():
    case ["quit"]:
        print("Goodbye!")
        quit_game()
    case ["look"]:
        current_room.describe()
    case ["get", obj]:
        character.get(obj, current_room)
    case ["go", direction]:
        current_room = current_room.neighbor(direction)
    # The rest of your commands go here

Looks interesting.

Pratyush Mittal @pratyush