Skip to content

Instantly share code, notes, and snippets.

@Hamatti
Last active March 11, 2021 11:46
Show Gist options
  • Save Hamatti/92a16db90db87721050c1bda95897932 to your computer and use it in GitHub Desktop.
Save Hamatti/92a16db90db87721050c1bda95897932 to your computer and use it in GitHub Desktop.
Nested pattern matching in Python 3.10a6
class Card:
def __init__(self, id, name, supertype, subtype):
self.id = id
self.name = name
self.supertype = supertype
self.subtype = subtype
def play_card(card):
match card:
case Card(supertype='Pokemon') as pokemon:
match pokemon:
case Card(subtype='Basic'):
print('Play to bench')
case (Card(subtype='Stage 1') | Card(subtype='Stage 2')):
print('Play on top of another Pokemon card')
case Card(supertype='Trainer') as trainer:
match trainer:
case Card(subtype='Item'):
print('Play as many items as you wish')
case Card(subtype='Supporter'):
print('Only one per turn')
charizard = Card('ex3-100', 'Charizard', 'Pokemon', 'Stage 2')
lapras = Card('ex3-103', 'Lapras', 'Pokemon', 'Basic')
muscle_band = Card('xy1-121', 'Muscle Band', 'Trainer', 'Item')
lysandre = Card('xy1-1', 'Lysandre', 'Trainer', 'Supporter')
play_card(charizard) # Prints Play on top of another Pokemon card
play_card(lapras) # Prints Play to bench
play_card(muscle_band) # Prints Play as many items as you want
play_card(lysandre) # Prints Only one per turn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment